Passed
Push — ft/urls ( 5c631b...3903c5 )
by Ben
95:30 queued 50:10
created

Registration   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 84.38%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
eloc 31
c 2
b 1
f 0
dl 0
loc 88
ccs 27
cts 32
cp 0.8438
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A tags() 0 3 1
A fromArray() 0 3 1
A has() 0 11 3
A model() 0 3 1
A key() 0 3 1
A class() 0 3 1
A __construct() 0 8 1
A validate() 0 18 5
1
<?php
2
3
namespace Thinktomorrow\Chief\Management;
4
5
class Registration
6
{
7
    private $key;
8
    private $managerClass;
9
    private $modelClass;
10
    private $tags;
11
12 433
    public function __construct(string $managerClass, string $modelClass, array $tags = [])
13
    {
14 433
        $this->validate($managerClass, $modelClass);
15
16 433
        $this->key = $modelClass::managedModelKey();
17 433
        $this->managerClass = $managerClass;
18 433
        $this->modelClass = $modelClass;
19 433
        $this->tags = $tags;
20 433
    }
21
22
    public static function fromArray(array $registration)
23
    {
24
        return new static(...array_values($registration));
25
    }
26
27
    /**
28
     * Return the key of the first entry.
29
     *
30
     * @return string
31
     */
32 433
    public function key(): string
33
    {
34 433
        return $this->key;
35
    }
36
37
    /**
38
     * Return the manager class
39
     *
40
     * @return string
41
     */
42
    public function class(): string
43
    {
44
        return $this->managerClass;
45
    }
46
47
    /**
48
     * Return the model class
49
     *
50
     * @return string
51
     */
52 129
    public function model(): string
53
    {
54 129
        return $this->modelClass;
55
    }
56
57 16
    public function tags(): array
58
    {
59 16
        return $this->tags;
60
    }
61
62 124
    public function has(string $key, $value): bool
63
    {
64 124
        if ($key == 'tags') {
65 17
            if (is_array($value)) {
66 17
                return (count(array_intersect($this->tags, $value)) > 0);
67
            }
68
69
            return in_array($value, $this->tags);
70
        }
71
72 124
        return $this->$key == $value;
73
    }
74
75 433
    private function validate($managerClass, $modelClass)
76
    {
77 433
        if (!class_exists($managerClass)) {
78 2
            throw new \InvalidArgumentException('Manager class ['.$managerClass.'] is an invalid class reference. Please make sure the class exists.');
79
        }
80
81 433
        if (!class_exists($modelClass)) {
82 1
            throw new \InvalidArgumentException('Model class ['.$modelClass.'] is an invalid model reference. Please make sure the class exists.');
83
        }
84
85 433
        $manager = new \ReflectionClass($managerClass);
86 433
        if (! $manager->implementsInterface(Manager::class)) {
87
            throw new \InvalidArgumentException('Class ['.$managerClass.'] is expected to implement the ['.Manager::class.'] contract.');
88
        }
89
90 433
        $model = new \ReflectionClass($modelClass);
91 433
        if (! $model->implementsInterface(ManagedModel::class)) {
92
            throw new \InvalidArgumentException('Class ['.$modelClass.'] is expected to implement the ['.ManagedModel::class.'] contract.');
93
        }
94 433
    }
95
}
96