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

SortableGroup::__construct()   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 2
crap 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use Gedmo\Sortable\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\ManyToMany;
10
use LaravelDoctrine\Fluent\Relations\ManyToOne;
11
12
class SortableGroup implements Buildable
13
{
14
    const MACRO_METHOD = 'sortableGroup';
15
16
    /**
17
     * @var ExtensibleClassMetadata
18
     */
19
    protected $classMetadata;
20
21
    /**
22
     * @var string
23
     */
24
    protected $fieldName;
25
26
    /**
27
     * @param ExtensibleClassMetadata $classMetadata
28
     * @param string                  $fieldName
29
     */
30 4
    public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName)
31
    {
32 4
        $this->classMetadata = $classMetadata;
33 4
        $this->fieldName     = $fieldName;
34 4
    }
35
36
    /**
37
     * Return the name of the actual extension.
38
     *
39
     * @return string
40
     */
41 1
    public function getExtensionName()
42
    {
43 1
        return FluentDriver::EXTENSION_NAME;
44
    }
45
46
    /**
47
     * @return void
48
     */
49 4
    public static function enable()
50
    {
51
        Field::macro(self::MACRO_METHOD, function (Field $builder) {
52 1
            return new static($builder->getClassMetadata(), $builder->getName());
53 4
        });
54
55
        ManyToOne::macro(self::MACRO_METHOD, function (ManyToOne $builder) {
56 1
            return new static($builder->getClassMetadata(), $builder->getRelation());
0 ignored issues
show
Compatibility introduced by
$builder->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...
57 4
        });
58
59 4
        ManyToMany::macro(self::MACRO_METHOD, function (ManyToMany $builder) {
60 1
            return new static($builder->getClassMetadata(), $builder->getRelation());
0 ignored issues
show
Compatibility introduced by
$builder->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...
61 4
        });
62 4
    }
63
64
    /**
65
     * Execute the build process
66
     */
67 1
    public function build()
68
    {
69 1
        $this->classMetadata->appendExtension($this->getExtensionName(), [
70
            'groups' => [
71 1
                $this->fieldName
72 1
            ]
73 1
        ]);
74 1
    }
75
}
76