Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
6 / 18
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
InAppNotificationCampaignRepository
33.33% covered (danger)
33.33%
6 / 18
83.33% covered (warning)
83.33%
5 / 6
16.67
0.00% covered (danger)
0.00%
0 / 1
 getAll
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getById
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActiveCampaigns
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Repositories;
4
5use App\Http\Models\InAppNotificationCampaign;
6use App\Http\Repositories\interfaces\IInAppNotificationCampaignRepository;
7use Carbon\Carbon;
8use Illuminate\Database\Eloquent\Collection;
9
10/**
11 * Repository for in-app notification campaign data access operations.
12 */
13class InAppNotificationCampaignRepository implements IInAppNotificationCampaignRepository
14{
15    /**
16     * {@inheritDoc}
17     */
18    public function getAll(): Collection
19    {
20        return InAppNotificationCampaign::orderBy('priority')->get();
21    }
22
23    /**
24     * {@inheritDoc}
25     */
26    public function getById(string $id): ?InAppNotificationCampaign
27    {
28        return InAppNotificationCampaign::find($id);
29    }
30
31    /**
32     * {@inheritDoc}
33     */
34    public function getActiveCampaigns(): Collection
35    {
36        $now = Carbon::now();
37
38        return InAppNotificationCampaign::where('is_active', true)
39            ->where(function ($query) use ($now) {
40                $query->whereNull('start_date')
41                    ->orWhere('start_date', '<=', $now);
42            })
43            ->where(function ($query) use ($now) {
44                $query->whereNull('end_date')
45                    ->orWhere('end_date', '>=', $now);
46            })
47            ->orderBy('priority')
48            ->get();
49    }
50
51    /**
52     * {@inheritDoc}
53     */
54    public function create(array $data): InAppNotificationCampaign
55    {
56        return InAppNotificationCampaign::create($data);
57    }
58
59    /**
60     * {@inheritDoc}
61     */
62    public function update(InAppNotificationCampaign $campaign, array $data): InAppNotificationCampaign
63    {
64        $campaign->update($data);
65
66        return $campaign->fresh();
67    }
68
69    /**
70     * {@inheritDoc}
71     */
72    public function delete(InAppNotificationCampaign $campaign): bool
73    {
74        return $campaign->delete();
75    }
76}