Completed
Push — extensions-support ( 74d037...c14d66 )
by Patrick
03:15
created

TreePath::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
ccs 4
cts 4
cp 1
cc 1
eloc 3
nc 1
nop 0
crap 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\Builders\Field;
9
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
10
11
class TreePath implements Buildable
12
{
13
    const MACRO_METHOD = 'treePath';
14
15
    /**
16
     * @var ExtensibleClassMetadata
17
     */
18
    protected $classMetadata;
19
20
    /**
21
     * @var string
22
     */
23
    protected $fieldName;
24
25
    /**
26
     * @var string
27
     */
28
    protected $separator;
29
30
    /**
31
     * @var mixed
32
     */
33
    protected $appendId = null;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $startsWithSeparator = false;
39
40
    /**
41
     * @var bool
42
     */
43
    protected $endsWithSeparator = true;
44
45
    /**
46
     * @param ExtensibleClassMetadata $classMetadata
47
     * @param string                  $fieldName
48
     * @param string                  $separator
49
     */
50 4
    public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName, $separator = '|')
51
    {
52 4
        $this->classMetadata = $classMetadata;
53 4
        $this->fieldName     = $fieldName;
54 4
        $this->separator     = $separator;
55 4
    }
56
57
    /**
58
     * Enable TreePath
59
     */
60
    public static function enable()
61
    {
62 2
        Field::macro(self::MACRO_METHOD, function (Field $field, $separator = '|') {
63 1
            return new static($field->getClassMetadata(), $field->getName(), $separator);
0 ignored issues
show
Compatibility introduced by
$field->getClassMetadata() of type object<Doctrine\ORM\Mapping\ClassMetadataInfo> is not a sub-type of object<LaravelDoctrine\F...xtensibleClassMetadata>. It seems like you assume a child class of the class Doctrine\ORM\Mapping\ClassMetadataInfo to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
64 2
        });
65 2
    }
66
67
    /**
68
     * Execute the build process
69
     */
70 3 View Code Duplication
    public function build()
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...
71
    {
72 3
        if (strlen($this->separator) > 1) {
73 1
            throw new InvalidMappingException("Tree Path field - [{$this->fieldName}] Separator {$this->separator} is invalid. It must be only one character long.");
74
        }
75
76 2
        $this->classMetadata->appendExtension($this->getExtensionName(), [
77 2
            'path'                       => $this->fieldName,
78 2
            'path_separator'             => $this->separator,
79 2
            'path_append_id'             => $this->appendId,
80 2
            'path_starts_with_separator' => $this->startsWithSeparator,
81 2
            'path_ends_with_separator'   => $this->endsWithSeparator
82 2
        ]);
83 2
    }
84
85
    /**
86
     * Return the name of the actual extension.
87
     *
88
     * @return string
89
     */
90 2
    public function getExtensionName()
91
    {
92 2
        return FluentDriver::EXTENSION_NAME;
93
    }
94
95
    /**
96
     * @param  string   $separator
97
     * @return TreePath
98
     */
99 2
    public function separator($separator)
100
    {
101 2
        $this->separator = $separator;
102
103 2
        return $this;
104
    }
105
106
    /**
107
     * @param  mixed    $appendId
108
     * @return TreePath
109
     */
110 1
    public function appendId($appendId)
111
    {
112 1
        $this->appendId = $appendId;
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * @param  bool     $startsWithSeparator
119
     * @return TreePath
120
     */
121 1
    public function startsWithSeparator($startsWithSeparator = true)
122
    {
123 1
        $this->startsWithSeparator = $startsWithSeparator;
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * @param  bool     $endsWithSeparator
130
     * @return TreePath
131
     */
132 1
    public function endsWithSeparator($endsWithSeparator = true)
133
    {
134 1
        $this->endsWithSeparator = $endsWithSeparator;
135
136 1
        return $this;
137
    }
138
}
139