Test Failed
Push — ft/states ( d4f7ca...0e1fde )
by Ben
09:10
created

Managers   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 15
eloc 41
dl 0
loc 135
ccs 38
cts 44
cp 0.8636
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 6 1
A findByTag() 0 6 1
A findByTagForSelect() 0 14 1
A __construct() 0 3 1
A findByKey() 0 5 1
A findByModel() 0 17 3
A findDetailsByTag() 0 6 1
A instanceModel() 0 7 1
A anyRegisteredByTag() 0 3 1
A instance() 0 9 2
A allAssistantClassNames() 0 9 2
1
<?php
2
3
namespace Thinktomorrow\Chief\Management;
4
5
use Illuminate\Support\Collection;
6
7
class Managers
8
{
9
    /** @var Register */
10
    private $register;
11
12 169
    public function __construct(Register $register)
13
    {
14 169
        $this->register = $register;
15 169
    }
16
17 162
    public function findByKey($key, $id = null): Manager
18
    {
19 162
        $registration = $this->register->filterByKey($key)->first();
20
21 162
        return $this->instance($registration, $id);
22
    }
23
24 11
    public function findByModel($model, $id = null): Manager
25
    {
26
        /**
27
         * If an instance is passed as model, we try to extract the class and id
28
         * ourselves and assume we can read the id value via the id property
29
         */
30 11
        if (null === $id && !is_string($model)) {
31 11
            $modelClass = get_class($model);
32
33 11
            $registration = $this->register->filterByModel($modelClass)->first();
34
35 9
            return $this->instanceModel($registration, $model);
36
        }
37
38
        $registration = $this->register->filterByModel($model)->first();
39
40
        return $this->instance($registration, $id);
41
    }
42
43
    /**
44
     * Enlist all details for a certain tag group of managed models
45
     *
46
     * @param $tag
47
     * @return Collection of Managers
48
     */
49 23
    public function findByTag($tag): Collection
50
    {
51 23
        $registrations = collect($this->register->filterByTag($tag)->all());
52
53
        return $registrations->map(function ($registration) {
54 21
            return $this->instance($registration);
55 23
        });
56
    }
57
58
    /**
59
     * Enlist all details for a certain tag group of managed models
60
     *
61
     * @param $tag
62
     * @return Collection of ManagedModelDetails
63
     */
64
    public function findDetailsByTag($tag): Collection
65
    {
66
        $registrations = collect($this->register->filterByTag($tag)->all());
67
68
        return $registrations->map(function ($registration) {
69
            return $this->instance($registration)->details();
70
        });
71
    }
72
73 3
    public function findByTagForSelect($tag)
74
    {
75 3
        $managers = $this->findByTag($tag);
76
        //return array with group name and values
77 3
        $grouped = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $grouped is dead and can be removed.
Loading history...
78
79
        $managers = $managers->map(function (Manager $item) {
80
            return [
81 3
                'id'    => $item->details()->id,
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Thinktomorrow\Chief\Management\Details\Details. Since you implemented __get, consider adding a @property annotation.
Loading history...
82 3
                'group' => $item->details()->plural,
0 ignored issues
show
Bug Best Practice introduced by
The property plural does not exist on Thinktomorrow\Chief\Management\Details\Details. Since you implemented __get, consider adding a @property annotation.
Loading history...
83
            ];
84 3
        })->toArray();
85
        // We remove the group key as we need to have non-assoc array for the multiselect options.
86 3
        return collect(array_values($managers));
87
    }
88
89 23
    public function all(): Collection
90
    {
91 23
        $registrations = collect($this->register->all());
92
93
        return $registrations->map(function ($registration) {
94 23
            return $this->instance($registration);
95 23
        });
96
    }
97
98 22
    public function allAssistantClassNames(): Collection
99
    {
100 22
        $assistants = collect();
101
102
        foreach($this->all() as $manager) {
103
            $assistants = $assistants->merge($manager->assistants(false));
104
        }
105
106
        return $assistants->unique();
107
    }
108 162
109
    public function anyRegisteredByTag($tag)
110 162
    {
111
        return ! empty($this->register->filterByTag($tag)->all());
112 162
    }
113
114 162
    /**
115 104
     * @param $registration
116 162
     * @param $id
117
     * @return mixed
118
     */
119
    private function instance(Registration $registration, $id = null)
120
    {
121
        $managerClass = $registration->class();
122
123
        $manager = new $managerClass($registration);
124 9
125
        return $id
126 9
            ? $manager->findManaged($id)
127
            : $manager;
128 9
    }
129
130 9
    /**
131
     * @param $registration
132
     * @param $model
133
     * @return mixed
134
     */
135
    private function instanceModel(Registration $registration, $model)
136
    {
137
        $managerClass = $registration->class();
138
139
        $manager = new $managerClass($registration);
140
141
        return $manager->manage($model);
142
    }
143
}
144