Completed
Push — 1.1 ( d166b0...e7f438 )
by Patrick
11:31 queued 07:46
created

NestedSet::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use Gedmo\Exception\InvalidMappingException;
6
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
7
use LaravelDoctrine\Fluent\Buildable;
8
use LaravelDoctrine\Fluent\Builders\Delay;
9
use LaravelDoctrine\Fluent\Extensions\Extension;
10
11
class NestedSet extends TreeStrategy implements Buildable, Extension, Delay
12
{
13
    const MACRO_METHOD = 'nestedSet';
14
15
    /**
16
     * @var string
17
     */
18
    protected $left;
19
20
    /**
21
     * @var string
22
     */
23
    protected $right;
24
25
    /**
26
     * @var string
27
     */
28
    protected $root;
29
30
    public static function enable()
31
    {
32
        parent::enable();
33
34
        TreeLeft::enable();
35
        TreeRight::enable();
36
        TreeSelfReference::enableRoot();
37
    }
38
39
    /**
40
     * @param string        $field
41
     * @param string        $type
42
     * @param callable|null $callback
43
     *
44
     * @throws InvalidMappingException
45
     * @return $this
46
     */
47 View Code Duplication
    public function left($field = 'left', $type = 'integer', callable $callback = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        $this->validateNumericField($type, $field);
50
51
        $this->mapField($type, $field, $callback);
52
53
        $this->left = $field;
54
55
        return $this;
56
    }
57
58
    /**
59
     * @param string        $field
60
     * @param string        $type
61
     * @param callable|null $callback
62
     *
63
     * @throws InvalidMappingException
64
     * @return $this
65
     */
66 View Code Duplication
    public function right($field = 'right', $type = 'integer', callable $callback = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $this->validateNumericField($type, $field);
69
70
        $this->mapField($type, $field, $callback);
71
72
        $this->right = $field;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string        $field
79
     * @param callable|null $callback
80
     *
81
     * @return $this
82
     */
83
    public function root($field = 'root', callable $callback = null)
84
    {
85
        $this->addSelfReferencingRelation($field, $callback);
86
87
        $this->root = $field;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Execute the build process
94
     */
95
    public function build()
96
    {
97
        $this->builder->entity()->setRepositoryClass(NestedTreeRepository::class);
98
99
        parent::build();
100
    }
101
102
    /**
103
     * Add default values to all required fields.
104
     *
105
     * @return void
106
     */
107
    protected function defaults()
108
    {
109
        parent::defaults();
110
111
        if ($this->isMissingLeft()) {
112
            $this->left();
113
        }
114
115
        if ($this->isMissingRight()) {
116
            $this->right();
117
        }
118
    }
119
120
    protected function getValues()
121
    {
122
        return array_merge(parent::getValues(), [
123
            'strategy' => 'nested',
124
            'left'     => $this->left,
125
            'right'    => $this->right,
126
            'root'     => $this->root,
127
        ]);
128
    }
129
130
    /**
131
     * @return bool
132
     */
133
    private function isMissingLeft()
134
    {
135
        return !$this->alreadyConfigured('left') && !$this->left;
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    private function isMissingRight()
142
    {
143
        return !$this->alreadyConfigured('right') && !$this->right;
144
    }
145
}
146