Passed
Push — master ( 855459...a29176 )
by Jonas
10:46
created

RecursiveRelationsHook::addRelationship()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 19
ccs 15
cts 15
cp 1
rs 9.7666
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\IdeHelper;
4
5
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
6
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
7
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Str;
10
use Staudenmeir\LaravelAdjacencyList\Eloquent\Collection;
11
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasGraphRelationships;
12
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
13
14
/**
15
 * @template RT { name: string, manyRelation: boolean, comment: string }
16
 */
17
class RecursiveRelationsHook implements ModelHookInterface
18
{
19
    /**
20
     * @var array<array<RT>>
21
     */
22
    protected static array $treeRelationships = [
23
        [
24
            'name' => 'ancestors',
25
            'manyRelation' => true,
26
            'comment' => "The model's recursive parents.",
27
        ],
28
        [
29
            'name' => 'ancestorsAndSelf',
30
            'manyRelation' => true,
31
            'comment' => "The model's recursive parents and itself.",
32
        ],
33
        [
34
            'name' => 'bloodline',
35
            'manyRelation' => true,
36
            'comment' => "The model's ancestors, descendants and itself.",
37
        ],
38
        [
39
            'name' => 'children',
40
            'manyRelation' => true,
41
            'comment' => "The model's direct children.",
42
        ],
43
        [
44
            'name' => 'childrenAndSelf',
45
            'manyRelation' => true,
46
            'comment' => "The model's direct children and itself.",
47
        ],
48
        [
49
            'name' => 'descendants',
50
            'manyRelation' => true,
51
            'comment' => "The model's recursive children.",
52
        ],
53
        [
54
            'name' => 'descendantsAndSelf',
55
            'manyRelation' => true,
56
            'comment' => "The model's recursive children and itself.",
57
        ],
58
        [
59
            'name' => 'parent',
60
            'manyRelation' => false,
61
            'comment' => "The model's direct parent.",
62
        ],
63
        [
64
            'name' => 'parentAndSelf',
65
            'manyRelation' => true,
66
            'comment' => "The model's direct parent and itself.",
67
        ],
68
        [
69
            'name' => 'rootAncestor',
70
            'manyRelation' => false,
71
            'comment' => "The model's topmost parent.",
72
        ],
73
        [
74
            'name' => 'siblings',
75
            'manyRelation' => true,
76
            'comment' => "The parent's other children.",
77
        ],
78
        [
79
            'name' => 'siblingsAndSelf',
80
            'manyRelation' => true,
81
            'comment' => "All the parent's children.",
82
        ]
83
    ];
84
85
    /**
86
     * @var array<array<RT>>
87
     */
88
    protected static array $graphRelationships = [
89
        [
90
            'name' => 'ancestors',
91
            'manyRelation' => true,
92
            'comment' => "The node's recursive parents.",
93
        ],
94
        [
95
            'name' => 'ancestorsAndSelf',
96
            'manyRelation' => true,
97
            'comment' => "The node's recursive parents and itself.",
98
        ],
99
        [
100
            'name' => 'children',
101
            'manyRelation' => true,
102
            'comment' => "The node's direct children.",
103
        ],
104
        [
105
            'name' => 'childrenAndSelf',
106
            'manyRelation' => true,
107
            'comment' => "The node's direct children and itself.",
108
        ],
109
        [
110
            'name' => 'descendants',
111
            'manyRelation' => true,
112
            'comment' => "The node's recursive children.",
113
        ],
114
        [
115
            'name' => 'descendantsAndSelf',
116
            'manyRelation' => true,
117
            'comment' => "The node's recursive children and itself.",
118
        ],
119
        [
120
            'name' => 'parents',
121
            'manyRelation' => true,
122
            'comment' => "The node's direct parents.",
123
        ],
124
        [
125
            'name' => 'parentsAndSelf',
126
            'manyRelation' => true,
127
            'comment' => "The node's direct parents and itself.",
128
        ],
129
    ];
130
131 12
    public function run(ModelsCommand $command, Model $model): void
132
    {
133 12
        $traits = class_uses_recursive($model);
134
135 12
        if (in_array(HasRecursiveRelationships::class, $traits)) {
136 6
            foreach (static::$treeRelationships as $relationship) {
137
                $type = $relationship['manyRelation']
138
                    ? '\\' . Collection::class . '|\\' . $model::class . '[]'
139 12
                    : '\\' . $model::class;
140 6
141
                $this->addRelationship($command, $relationship, $type);
142
            }
143
        }
144
145 6
        if (in_array(HasGraphRelationships::class, $traits)) {
146
            foreach (static::$graphRelationships as $relationship) {
147 6
                $type = '\\' . EloquentCollection::class . '|\\' . $model::class . '[]';
148 6
149 6
                $this->addRelationship($command, $relationship, $type);
150 6
            }
151
        }
152 6
    }
153 6
154 6
    protected function addRelationship(ModelsCommand $command, array $relationship, string $type): void
155 6
    {
156 6
        $command->setProperty(
157 6
            $relationship['name'],
158 6
            $type,
159 6
            true,
160
            false,
161 6
            $relationship['comment'],
162 6
            !$relationship['manyRelation']
163 6
        );
164 6
165 6
        if ($relationship['manyRelation']) {
166 6
            $command->setProperty(
167 6
                Str::snake($relationship['name']) . '_count',
168 6
                'int',
169 6
                true,
170
                false,
171
                null,
172
                true
173
            );
174 6
        }
175
    }
176
}
177