PeriodicNoticeDirector::allowedPeriodTypes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace PeriodicNotice;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Collection;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Database\Eloquent\Model;
8
9
class PeriodicNoticeDirector
10
{
11
    protected ?string $periodType = null;
12
13
    protected array $periodTypesMap = [];
14
15
    protected ?\Closure $queryToGetEntries = null;
16
17
    protected \Closure|string|null $notificationClass = null;
18
19 5
    public function __construct(
20
        protected string $group = 'default'
21
    ) {
22 5
    }
23
24 5
    public static function defaults(...$attributes): static
25
    {
26 5
        return new static(...$attributes);
0 ignored issues
show
Bug introduced by
$attributes is expanded, but the parameter $group of PeriodicNotice\PeriodicN...Director::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        return new static(/** @scrutinizer ignore-type */ ...$attributes);
Loading history...
27
    }
28
29 4
    public function useQueryToGetEntries(\Closure|string $callback): static
30
    {
31 4
        if (is_callable($callback)) {
32 1
            $this->queryToGetEntries = $callback;
33 3
        } elseif (is_a($callback, Model::class, true)) {
34 3
            $this->queryToGetEntries = fn () => $callback::query();
0 ignored issues
show
Bug introduced by
The method query() does not exist on Closure. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
            $this->queryToGetEntries = fn () => $callback::/** @scrutinizer ignore-call */ query();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
        }
36
37 4
        return $this;
38
    }
39
40 5
    public function usePeriodType(?string $periodType): static
41
    {
42 5
        $this->periodType = $periodType;
43
44 5
        return $this;
45
    }
46
47 3
    public function usePeriodTypesMap(array $periodTypesMap): static
48
    {
49 3
        $this->periodTypesMap = $periodTypesMap;
50
51 3
        return $this;
52
    }
53
54 1
    public function useNotificationClass(\Closure|string $callback): static
55
    {
56 1
        $this->notificationClass = $callback;
57
58 1
        return $this;
59
    }
60
61 2
    public function periodTypesMap(): array
62
    {
63 2
        return $this->periodTypesMap ?? [];
64
    }
65
66 2
    public function allowedPeriodTypes(): array
67
    {
68 2
        return array_keys($this->periodTypesMap());
69
    }
70
71 3
    public function periodStartDateTime(): ?\DateTimeInterface
72
    {
73 3
        if ($this->periodType
74 3
            && ($periodInSeconds = $this->periodTypesMap[$this->periodType] ?? 0)) {
75 2
            return Carbon::now()->subSeconds($periodInSeconds);
76
        }
77
78 1
        return null;
79
    }
80
81 2
    public function notificationClass(): string
82
    {
83 2
        if (is_callable($this->notificationClass)) {
84 1
            return call_user_func_array($this->notificationClass, [$this->periodType, $this->group]);
0 ignored issues
show
Bug introduced by
It seems like $this->notificationClass can also be of type null; however, parameter $callback of call_user_func_array() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            return call_user_func_array(/** @scrutinizer ignore-type */ $this->notificationClass, [$this->periodType, $this->group]);
Loading history...
85
        }
86
87 2
        if (is_a($this->notificationClass, \Illuminate\Notifications\Notification::class, true)) {
88 1
            return $this->notificationClass;
89
        }
90
91 1
        return config('periodic-notice.defaults.notification');
92
    }
93
94 3
    public function findEntries(Model $user)
95
    {
96 3
        $publicationStartDate = $this->periodStartDateTime();
97
98 3
        if (!$publicationStartDate || !is_callable($this->queryToGetEntries)) {
0 ignored issues
show
introduced by
$publicationStartDate is of type Carbon\Carbon, thus it always evaluated to true.
Loading history...
99 1
            return Collection::make();
100
        }
101
102 2
        $query = call_user_func_array($this->queryToGetEntries, [$this->periodType, $this->group])
0 ignored issues
show
Bug introduced by
It seems like $this->queryToGetEntries can also be of type null; however, parameter $callback of call_user_func_array() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
        $query = call_user_func_array(/** @scrutinizer ignore-type */ $this->queryToGetEntries, [$this->periodType, $this->group])
Loading history...
103 2
            ->doesntSentInPeriodicNotice($user, $this->group)
104 2
            ->releasedAfter($publicationStartDate, $this->group);
105
106 2
        return $query->get();
107
    }
108
109
    /**
110
     * @param  \Illuminate\Database\Eloquent\Model|\PeriodicNotice\Contracts\NotificationReceiver  $user
111
     * @return void
112
     */
113 1
    public function sendPeriodicalNotification(Model $user)
114
    {
115 1
        $entries = $this->findEntries($user);
116
117 1
        if ($entries->count() > 0) {
118 1
            $notificationClass = $this->notificationClass();
119 1
            $user->notify(new $notificationClass($entries));
120
121 1
            $data = [
122 1
                'group'   => $this->group,
123 1
                'sent_at' => Carbon::now(),
124 1
                'meta'    => [
125 1
                    'type'         => $this->periodType,
126 1
                    'notification' => $notificationClass,
127 1
                ],
128 1
            ];
129
130
            /** @var \Illuminate\Database\Eloquent\Model $entry */
131 1
            foreach ($entries as $entry) {
132 1
                $user->periodicSentEntries()->create(array_merge([
133 1
                    'sendable_type' => $entry->getMorphClass(),
134 1
                    'sendable_id'   => $entry->getKey(),
135 1
                ], $data));
136
            }
137
        }
138
    }
139
}
140