Completed
Push — extensions-support ( dec598...10d6ff )
by Guido
05:03 queued 01:20
created

TreeStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
     * Return the name of the actual extension.
82
     *
83
     * @return string
84
     */
85
    protected function getExtensionName()
86
    {
87
        return FluentDriver::EXTENSION_NAME;
88
    }
89
90
    /**
91
     * @param string        $type
92
     * @param string        $field
93
     * @param callable|null $callback
94
     * @param bool|false    $nullable
95
     */
96
    protected function mapField($type, $field, callable $callback = null, $nullable = false)
97
    {
98
        $this->builder->field($type, $field, $callback)->nullable($nullable);
99
    }
100
101
    /**
102
     * @param string $type
103
     * @param string $field
104
     *
105
     * @throws InvalidMappingException
106
     */
107
    protected function validateNumericField($type, $field)
108
    {
109
        if (!in_array($type, ['integer', 'bigint', 'smallint'])) {
110
            throw new InvalidMappingException("Invalid type [$type] for the [$field] field. Must be a (small, big) integer type.");
111
        }
112
    }
113
114
    /**
115
     * Returns the name of the mapped class.
116
     *
117
     * @return string
118
     */
119
    protected function myself()
120
    {
121
        return $this->getClassMetadata()->name;
122
    }
123
124
    /**
125
     * @param string        $field
126
     * @param callable|null $callback
127
     */
128
    protected function addSelfReferencingRelation($field, callable $callback = null)
129
    {
130
        $this->builder->belongsTo($this->myself(), $field, $callback)->nullable();
131
    }
132
133
    /**
134
     * @return ExtensibleClassMetadata
135
     */
136
    protected function getClassMetadata()
137
    {
138
        return $this->builder->getBuilder()->getClassMetadata();
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    protected function getValues()
145
    {
146
        $values = [];
147
148
        if ($this->parent) {
149
            $values['parent'] = $this->parent;
150
        }
151
152
        if ($this->level) {
153
            $values['level'] = $this->level;
154
        }
155
156
        return $values;
157
    }
158
}
159