Completed
Push — extensions-support ( f580fa...b18681 )
by Guido
06:46
created

TreeSelfReference::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use Gedmo\Tree\Mapping\Driver\Fluent as FluentDriver;
6
use LaravelDoctrine\Fluent\Buildable;
7
use LaravelDoctrine\Fluent\Builders\Field;
8
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
9
use LaravelDoctrine\Fluent\Relations\ManyToOne;
10
11
class TreeSelfReference implements Buildable
12
{
13
    /**
14
     * @var ExtensibleClassMetadata
15
     */
16
    protected $classMetadata;
17
18
    /**
19
     * @var string
20
     */
21
    protected $fieldName;
22
23
    /**
24
     * @var string
25
     */
26
    private $key;
27
28
    /**
29
     * @param ExtensibleClassMetadata $classMetadata
30
     * @param string                  $fieldName
31
     * @param string                  $key
32
     */
33 13
    public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName, $key)
34
    {
35 13
        $this->classMetadata = $classMetadata;
36 13
        $this->fieldName     = $fieldName;
37 13
        $this->key           = $key;
38 13
    }
39
40
    /**
41
     * Enable TreeRoot and TreeParent macros.
42
     *
43
     * @return void
44
     */
45 8
    public static function enable()
46
    {
47 8
        static::enableRoot();
48 8
        static::enableParent();
49 8
    }
50
51
    /**
52
     * Enable only the TreeRoot macro.
53
     *
54
     * @return void
55
     */
56 92
    public static function enableRoot()
57
    {
58 92
        static::addMacro('treeRoot', 'root');
59 92
    }
60
61
    /**
62
     * Enable only the TreeParent macro.
63
     *
64
     * @return void
65
     */
66 93
    public static function enableParent()
67
    {
68 93
        static::addMacro('treeParent', 'parent');
69 93
    }
70
71
    /**
72
     * @param string $method
73
     * @param string $key
74
     *
75
     * @return void
76
     */
77 93
    protected static function addMacro($method, $key)
78
    {
79
        Field::macro($method, function(Field $field) use ($key) {
80 4
            $field->nullable();
81
82 4
            return new static($field->getClassMetadata(), $field->getName(), $key);
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...
83 93
        });
84
85 93
        ManyToOne::macro($method, function (ManyToOne $relation) use ($key) {
86 7
            $relation->nullable();
87
88 7
            return new static($relation->getClassMetadata(), $relation->getRelation(), $key);
0 ignored issues
show
Compatibility introduced by
$relation->getClassMetadata() of type object<Doctrine\ORM\Mapping\ClassMetadata> is not a sub-type of object<LaravelDoctrine\F...xtensibleClassMetadata>. It seems like you assume a child class of the class Doctrine\ORM\Mapping\ClassMetadata 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...
89 93
        });
90 93
    }
91
92
    /**
93
     * Execute the build process
94
     */
95 9
    public function build()
96
    {
97 9
        $this->classMetadata->mergeExtension($this->getExtensionName(), [
98 9
            $this->key => $this->fieldName,
99 9
        ]);
100 9
    }
101
102
    /**
103
     * Return the name of the actual extension.
104
     *
105
     * @return string
106
     */
107 9
    public function getExtensionName()
108
    {
109 9
        return FluentDriver::EXTENSION_NAME;
110
    }
111
}
112