1 | <?php |
||
2 | |||
3 | namespace NotificationTracker; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | use NotificationTracker\Models\TrackedChannel; |
||
7 | use NotificationTracker\Models\TrackedNotification; |
||
8 | |||
9 | class NotificationTracker |
||
10 | { |
||
11 | public static bool $runsMigrations = true; |
||
12 | |||
13 | public static bool $registersRoutes = true; |
||
14 | |||
15 | public static array $classMap = []; |
||
16 | |||
17 | protected static array $models = [ |
||
18 | 'notification' => TrackedNotification::class, |
||
19 | 'channel' => TrackedChannel::class, |
||
20 | ]; |
||
21 | |||
22 | |||
23 | public static function ignoreMigrations(): static |
||
24 | { |
||
25 | static::$runsMigrations = false; |
||
26 | |||
27 | return new static; |
||
28 | } |
||
29 | |||
30 | public static function ignoreRoutes(): static |
||
31 | { |
||
32 | static::$registersRoutes = false; |
||
33 | |||
34 | return new static; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param array|null $map |
||
39 | * @param bool $merge Merge or replace class map. |
||
40 | * @return array |
||
41 | */ |
||
42 | 14 | public static function classMap(array $map = null, bool $merge = true): array |
|
43 | { |
||
44 | 14 | if (is_array($map)) { |
|
45 | 14 | static::$classMap = $merge && static::$classMap |
|
0 ignored issues
–
show
|
|||
46 | 14 | ? $map + static::$classMap : $map; |
|
47 | } |
||
48 | |||
49 | 14 | return static::$classMap; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string $alias |
||
54 | * @return string|null |
||
55 | */ |
||
56 | 1 | public static function getMappedClass(string $alias): ?string |
|
57 | { |
||
58 | 1 | return static::$classMap[$alias] ?? null; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string $class |
||
63 | * @return string |
||
64 | */ |
||
65 | 6 | public static function getMapAlias(string $class): string |
|
66 | { |
||
67 | 6 | $morphMap = static::$classMap; |
|
68 | |||
69 | if ( |
||
70 | 6 | !empty($morphMap) && |
|
71 | 6 | ($found = array_search($class, $morphMap, true)) |
|
72 | ) { |
||
73 | 1 | return $found; |
|
74 | } |
||
75 | |||
76 | 5 | return $class; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param string $key |
||
81 | * @param string $modelClass |
||
82 | * @return class-string<static> |
||
0 ignored issues
–
show
|
|||
83 | * @throws \Exception |
||
84 | */ |
||
85 | public static function useModel(string $key, string $modelClass): string |
||
86 | { |
||
87 | if (!in_array($key, array_keys(static::$models))) { |
||
88 | throw new \Exception( |
||
89 | "Incorrect model key [{$key}], allowed keys are: " . implode(', ', array_keys(static::$models)) |
||
90 | ); |
||
91 | } |
||
92 | if (!is_subclass_of($modelClass, Model::class)) { |
||
93 | throw new \Exception("Class should be a model [{$modelClass}]"); |
||
94 | } |
||
95 | |||
96 | static::$models[$key] = $modelClass; |
||
97 | |||
98 | return static::class; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param string $key |
||
103 | * @return class-string<Model|TrackedNotification|TrackedChannel> |
||
0 ignored issues
–
show
|
|||
104 | * @throws \Exception |
||
105 | */ |
||
106 | 11 | public static function modelClass(string $key): string |
|
107 | { |
||
108 | 11 | return static::$models[$key] ?? throw new \Exception( |
|
109 | 11 | "Incorrect model key [{$key}], allowed keys are: " . implode(', ', array_keys(static::$models)) |
|
110 | 11 | ); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param string $key |
||
115 | * @param array $attributes |
||
116 | * @return Model|TrackedNotification|TrackedChannel |
||
117 | * @throws \Exception |
||
118 | */ |
||
119 | 5 | public static function model(string $key, array $attributes = []): Model |
|
120 | { |
||
121 | 5 | $modelClass = static::modelClass($key); |
|
122 | |||
123 | /** @var Model $model */ |
||
124 | 5 | $model = new $modelClass($attributes); |
|
125 | |||
126 | 5 | return $model; |
|
127 | } |
||
128 | |||
129 | 5 | public static function trackHeaderName(): string |
|
130 | { |
||
131 | 5 | return 'X-Notification-Track'; |
|
132 | } |
||
133 | |||
134 | 2 | public static function clickTrackerUrlParameterName(): string |
|
135 | { |
||
136 | 2 | return 'u'; |
|
137 | } |
||
138 | |||
139 | 2 | public static function pixelFilePath(): string |
|
140 | { |
||
141 | 2 | return __DIR__ . '/../resources/images/pixel.gif'; |
|
142 | } |
||
143 | } |
||
144 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.