1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PeriodicNotice; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
|
|
|
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); |
|
|
|
|
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(); |
|
|
|
|
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]); |
|
|
|
|
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)) { |
|
|
|
|
99
|
1 |
|
return Collection::make(); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
$query = call_user_func_array($this->queryToGetEntries, [$this->periodType, $this->group]) |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths