Passed
Push — 0.5 ( 53ba8a...29d487 )
by Philippe
58:35 queued 55:54
created

Managers::findByUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Management;
6
7
use Illuminate\Support\Collection;
8
use Thinktomorrow\Chief\Concerns\Morphable\Morphables;
9
use Thinktomorrow\Chief\Urls\UrlRecord;
10
11
class Managers
12
{
13
    /** @var Register */
14 210
    private $register;
15
16 210
    public function __construct(Register $register)
17 210
    {
18
        $this->register = $register;
19 202
    }
20
21 202
    public function findByKey($key, $id = null): Manager
22
    {
23 202
        $registration = $this->register->filterByKey($key)->first();
24
25
        return $this->instance($registration, $id);
26 11
    }
27
28
    public function findByModel($model, $id = null): Manager
29
    {
30
        /**
31
         * If an instance is passed as model, we try to extract the class and id
32 11
         * ourselves and assume we can read the id value via the id property
33 11
         */
34
        if (null === $id && !is_string($model)) {
35 11
            $modelClass = get_class($model);
36
37 9
            $registration = $this->register->filterByModel($modelClass)->first();
38
39
            return $this->instanceModel($registration, $model);
40
        }
41
42
        $registration = $this->register->filterByModel($model)->first();
43
44
        return $this->instance($registration, $id);
45
    }
46
47
    public function findByUrl(string $slug, string $locale): Manager
48
    {
49
        $urlRecord = UrlRecord::findBySlug($slug, $locale);
50
51 25
        $model = Morphables::instance($urlRecord->model_type)->find($urlRecord->model_id);
52
53 25
        return $this->findByModel($model);
54
    }
55
56 23
    /**
57 25
     * Enlist all details for a certain tag group of managed models
58
     *
59
     * @param $tag
60
     * @return Collection of Managers
61
     */
62
    public function findByTag($tag): Collection
63
    {
64
        $registrations = collect($this->register->filterByTag($tag)->all());
65
66
        return $registrations->map(function ($registration) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $registrations->m...ion(...) { /* ... */ }) returns the type Tightenco\Collect\Support\Collection which is incompatible with the type-hinted return Illuminate\Support\Collection.
Loading history...
67
            return $this->instance($registration);
68
        });
69
    }
70
71
    /**
72
     * Enlist all details for a certain tag group of managed models
73
     *
74
     * @param $tag
75
     * @return Collection of ManagedModelDetails
76
     */
77
    public function findDetailsByTag($tag): Collection
78
    {
79
        $registrations = collect($this->register->filterByTag($tag)->all());
80
81
        return $registrations->map(function ($registration) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $registrations->m...ion(...) { /* ... */ }) returns the type Tightenco\Collect\Support\Collection which is incompatible with the type-hinted return Illuminate\Support\Collection.
Loading history...
82
            return $this->instance($registration)->details();
83
        });
84
    }
85
86
    public function findByTagForSelect($tag)
87
    {
88
        $managers = $this->findByTag($tag);
89
        //return array with group name and values
90
        $grouped = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $grouped is dead and can be removed.
Loading history...
91
92 25
        $managers = $managers->map(function (Manager $item) {
93
            return [
94 25
                '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...
95
                '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...
96
            ];
97 25
        })->toArray();
98 25
99
        // We remove the group key as we need to have non-assoc array for the multiselect options.
100
        return collect(array_values($managers));
101
    }
102
103
    public function all(): Collection
104
    {
105
        $registrations = collect($this->register->all());
106
107
        return $registrations->map(function ($registration) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $registrations->m...ion(...) { /* ... */ }) returns the type Tightenco\Collect\Support\Collection which is incompatible with the type-hinted return Illuminate\Support\Collection.
Loading history...
108
            return $this->instance($registration);
109
        });
110
    }
111
112 24
    public function allAssistantClassNames(): Collection
113
    {
114 24
        $assistants = collect();
115
116
        foreach ($this->all() as $manager) {
117
            $assistants = $assistants->merge($manager->assistants(false));
118
        }
119
120
        return $assistants->unique();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $assistants->unique() returns the type Tightenco\Collect\Support\Collection which is incompatible with the type-hinted return Illuminate\Support\Collection.
Loading history...
121
    }
122 202
123
    public function anyRegisteredByTag($tag)
124 202
    {
125
        return !empty($this->register->filterByTag($tag)->all());
126 202
    }
127
128 202
    /**
129 140
     * @param $registration
130 202
     * @param $id
131
     * @return mixed
132
     */
133
    private function instance(Registration $registration, $id = null)
134
    {
135
        $managerClass = $registration->class();
136
137
        $manager = new $managerClass($registration);
138 9
139
        return $id
140 9
            ? $manager->findManaged($id)
141
            : $manager;
142 9
    }
143
144 9
    /**
145
     * @param $registration
146
     * @param $model
147
     * @return mixed
148
     */
149
    private function instanceModel(Registration $registration, $model)
150
    {
151
        $managerClass = $registration->class();
152
153
        $manager = new $managerClass($registration);
154
155
        return $manager->manage($model);
156
    }
157
}
158