Completed
Push — 1.1 ( d166b0...e7f438 )
by Patrick
11:31 queued 07:46
created

TreeSelfReference   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 101
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A enable() 0 5 1
A enableRoot() 0 4 1
A enableParent() 0 4 1
A addMacro() 0 14 1
A build() 0 6 1
A getExtensionName() 0 4 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
    public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName, $key)
34
    {
35
        $this->classMetadata = $classMetadata;
36
        $this->fieldName     = $fieldName;
37
        $this->key           = $key;
38
    }
39
40
    /**
41
     * Enable TreeRoot and TreeParent macros.
42
     *
43
     * @return void
44
     */
45
    public static function enable()
46
    {
47
        static::enableRoot();
48
        static::enableParent();
49
    }
50
51
    /**
52
     * Enable only the TreeRoot macro.
53
     *
54
     * @return void
55
     */
56
    public static function enableRoot()
57
    {
58
        static::addMacro('treeRoot', 'root');
59
    }
60
61
    /**
62
     * Enable only the TreeParent macro.
63
     *
64
     * @return void
65
     */
66
    public static function enableParent()
67
    {
68
        static::addMacro('treeParent', 'parent');
69
    }
70
71
    /**
72
     * @param string $method
73
     * @param string $key
74
     *
75
     * @return void
76
     */
77
    protected static function addMacro($method, $key)
78
    {
79
        Field::macro($method, function (Field $field) use ($key) {
80
            $field->nullable();
81
82
            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
        });
84
85
        ManyToOne::macro($method, function (ManyToOne $relation) use ($key) {
86
            $relation->nullable();
87
88
            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
        });
90
    }
91
92
    /**
93
     * Execute the build process
94
     */
95
    public function build()
96
    {
97
        $this->classMetadata->mergeExtension($this->getExtensionName(), [
98
            $this->key => $this->fieldName,
99
        ]);
100
    }
101
102
    /**
103
     * Return the name of the actual extension.
104
     *
105
     * @return string
106
     */
107
    public function getExtensionName()
108
    {
109
        return FluentDriver::EXTENSION_NAME;
110
    }
111
}
112