Completed
Push — extensions-support ( cafd05...735378 )
by Guido
05:02
created

NestedSet::left()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 10
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
crap 1
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 84
    public static function enable()
31
    {
32 84
        parent::enable();
33
34 84
        TreeLeft::enable();
35 84
        TreeRight::enable();
36 84
        TreeRoot::enable();
37 84
    }
38
39
    /**
40
     * @param string        $field
41
     * @param string        $type
42
     * @param callable|null $callback
43
     *
44
     * @throws InvalidMappingException
45
     * @return $this
46
     */
47 25 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 25
        $this->validateNumericField($type, $field);
50
51 24
        $this->mapField($type, $field, $callback);
52
53 24
        $this->left = $field;
54
55 24
        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 25 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 25
        $this->validateNumericField($type, $field);
69
70 24
        $this->mapField($type, $field, $callback);
71
72 24
        $this->right = $field;
73
74 24
        return $this;
75
    }
76
77
    /**
78
     * @param string        $field
79
     * @param callable|null $callback
80
     *
81
     * @return $this
82
     */
83 3
    public function root($field = 'root', callable $callback = null)
84
    {
85 3
        $this->addSelfReferencingRelation($field, $callback);
86
87 3
        $this->root = $field;
88
89 3
        return $this;
90
    }
91
92
    /**
93
     * Execute the build process
94
     */
95 23
    public function build()
96
    {
97 23
        $this->addDefaults();
98
99 23
        $this->builder->entity()->setRepositoryClass(NestedTreeRepository::class);
100
101 23
        parent::build();
102 23
    }
103
104
    /**
105
     * Add default values to all required fields.
106
     *
107
     * @return void
108
     */
109 23
    protected function addDefaults()
110
    {
111 23
        if (!$this->parent) {
112 20
            $this->parent();
113 20
        }
114
115 23
        if (!$this->left) {
116 17
            $this->left();
117 17
        }
118
119 23
        if (!$this->right) {
120 17
            $this->right();
121 17
        }
122 23
    }
123
124 23
    protected function getValues()
125
    {
126 23
        return array_merge(parent::getValues(), [
127 23
            'strategy' => 'nested',
128 23
            'left'     => $this->left,
129 23
            'right'    => $this->right,
130 23
            'root'     => $this->root,
131 23
        ]);
132
    }
133
}
134