Passed
Push — main ( 8b0f26...0acacf )
by Yaroslav
03:02
created

NotificationTracker::getMapAlias()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
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
Bug Best Practice introduced by
The expression static::classMap of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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
Documentation Bug introduced by
The doc comment class-string<static> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<static>.
Loading history...
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
Documentation Bug introduced by
The doc comment class-string<Model|Track...ication|TrackedChannel> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Model|TrackedNotification|TrackedChannel>.
Loading history...
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