Completed
Push — extensions-support ( 10d6ff...e9c539 )
by Guido
04:09
created

TreeStrategy::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use Gedmo\Exception\InvalidMappingException;
6
use Gedmo\Tree\Mapping\Driver\Fluent as FluentDriver;
7
use LaravelDoctrine\Fluent\Buildable;
8
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
9
use LaravelDoctrine\Fluent\Extensions\Extension;
10
use LaravelDoctrine\Fluent\Fluent;
11
12
abstract class TreeStrategy implements Buildable, Extension
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $parent;
18
19
    /**
20
     * @var string
21
     */
22
    protected $level;
23
24
    /**
25
     * @var Fluent
26
     */
27
    protected $builder;
28
29
    /**
30
     * @param Fluent $builder
31
     */
32
    public function __construct(Fluent $builder)
33
    {
34
        $this->builder = $builder;
35
    }
36
37
    /**
38
     * Enable extension
39
     */
40
    public static function enable()
41
    {
42
        TreeLevel::enable();
43
        TreeParent::enable();
44
    }
45
46
    /**
47
     * @param string        $field
48
     * @param string        $type
49
     * @param callable|null $callback
50
     *
51
     * @throws InvalidMappingException
52
     * @return $this
53
     */
54 View Code Duplication
    public function level($field = 'level', $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...
55
    {
56
        $this->validateNumericField($type, $field);
57
58
        $this->mapField($type, $field, $callback);
59
60
        $this->level = $field;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param string        $field
67
     * @param callable|null $callback
68
     *
69
     * @return $this
70
     */
71
    public function parent($field = 'parent', callable $callback = null)
72
    {
73
        $this->addSelfReferencingRelation($field, $callback);
74
75
        $this->parent = $field;
76
77
        return $this;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function build()
84
    {
85
        $this->getClassMetadata()->mergeExtension($this->getExtensionName(), $this->getValues());
86
    }
87
88
    /**
89
     * Return the name of the actual extension.
90
     *
91
     * @return string
92
     */
93
    protected function getExtensionName()
94
    {
95
        return FluentDriver::EXTENSION_NAME;
96
    }
97
98
    /**
99
     * @param string        $type
100
     * @param string        $field
101
     * @param callable|null $callback
102
     * @param bool|false    $nullable
103
     */
104
    protected function mapField($type, $field, callable $callback = null, $nullable = false)
105
    {
106
        $this->builder->field($type, $field, $callback)->nullable($nullable);
107
    }
108
109
    /**
110
     * @param string $type
111
     * @param string $field
112
     *
113
     * @throws InvalidMappingException
114
     */
115
    protected function validateNumericField($type, $field)
116
    {
117
        if (!in_array($type, ['integer', 'bigint', 'smallint'])) {
118
            throw new InvalidMappingException("Invalid type [$type] for the [$field] field. Must be a (small, big) integer type.");
119
        }
120
    }
121
122
    /**
123
     * Returns the name of the mapped class.
124
     *
125
     * @return string
126
     */
127
    protected function myself()
128
    {
129
        return $this->getClassMetadata()->name;
130
    }
131
132
    /**
133
     * @param string        $field
134
     * @param callable|null $callback
135
     */
136
    protected function addSelfReferencingRelation($field, callable $callback = null)
137
    {
138
        $this->builder->belongsTo($this->myself(), $field, $callback)->nullable();
139
    }
140
141
    /**
142
     * @return ExtensibleClassMetadata
143
     */
144
    protected function getClassMetadata()
145
    {
146
        return $this->builder->getBuilder()->getClassMetadata();
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    protected function getValues()
153
    {
154
        $values = [];
155
156
        if ($this->parent) {
157
            $values['parent'] = $this->parent;
158
        }
159
160
        if ($this->level) {
161
            $values['level'] = $this->level;
162
        }
163
164
        return $values;
165
    }
166
}
167