Passed
Push — master ( e8502e...677200 )
by Stephen
24:48
created

UserNotificationSubscriptionQuery::builder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Users\Queries;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Sfneal\Caching\Traits\Cacheable;
8
use Sfneal\PostOffice\Notifications\AbstractNotification;
9
use Sfneal\Queries\Query;
10
use Sfneal\Users\Builders\UserBuilder;
11
use Sfneal\Users\Builders\UserNotificationBuilder;
12
use Sfneal\Users\Models\User;
13
14
class UserNotificationSubscriptionQuery extends Query
15
{
16
    use Cacheable;
17
18
    /**
19
     * @var string
20
     */
21
    private $notification;
22
23
    /**
24
     * UserNotificationSubscriptionQuery constructor.
25
     *
26
     * @param AbstractNotification $notification
27
     */
28
    public function __construct(AbstractNotification $notification)
29
    {
30
        $this->notification = getClassName($notification);
31
    }
32
33
    /**
34
     * Retrieve a Query builder.
35
     *
36
     * @return UserBuilder
37
     */
38
    protected function builder(): UserBuilder
39
    {
40
        return User::query();
41
    }
42
43
    /**
44
     * Retrieve a Collection of User's that are subscribed to a Notification.
45
     *
46
     *  - only return user 'Stephen Neal' if environment is not 'production'
47
     *
48
     * @return Collection|int|mixed
49
     */
50
    public function execute()
51
    {
52
        // Production environment
53
        if (env('APP_ENV') == 'production') {
0 ignored issues
show
introduced by
The condition env('APP_ENV') == 'production' is always false.
Loading history...
54
            return $this->builder()
55
                ->whereHas('notificationSubscriptions', function (UserNotificationBuilder $builder) {
56
                    $builder->whereType($this->notification);
57
                })
58
                ->get();
59
        }
60
61
        // Development environment
62
        else {
63
            return $this->builder()
64
                ->whereUser(38)
65
                ->get();
66
        }
67
    }
68
69
    /**
70
     * Retrieve the Query cache key.
71
     *
72
     * @return string
73
     */
74
    public function cacheKey(): string
75
    {
76
        return "user:notification:subscription#{$this->notification}";
77
    }
78
}
79