Completed
Push — extensions-support ( 1dda5d...e16b25 )
by Guido
03:56 queued 49s
created

Loggable::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
ccs 10
cts 10
cp 1
cc 2
eloc 8
nc 2
nop 0
crap 2
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use Gedmo\Loggable\Mapping\Driver\Fluent;
6
use LaravelDoctrine\Fluent\Buildable;
7
use LaravelDoctrine\Fluent\Builders\Entity;
8
use LaravelDoctrine\Fluent\Builders\Field;
9
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
10
use LaravelDoctrine\Fluent\Relations\ManyToOne;
11
use LaravelDoctrine\Fluent\Relations\OneToOne;
12
13
class Loggable implements Buildable
14
{
15
    /**
16
     * @var ExtensibleClassMetadata
17
     */
18
    private $classMetadata;
19
20
    /**
21
     * @var string|null
22
     */
23
    private $logEntry;
24
25
    /**
26
     * @param ExtensibleClassMetadata $classMetadata
27
     * @param string|null             $logEntry
28
     */
29 7
    public function __construct(ExtensibleClassMetadata $classMetadata, $logEntry = null)
30
    {
31 7
        $this->classMetadata = $classMetadata;
32 7
        $this->logEntry      = $logEntry;
33 7
    }
34
35
    /**
36
     * @return void
37
     */
38 4
    public static function enable()
39
    {
40
        Entity::macro('loggable', function (Entity $builder, $logEntry = null) {
41 1
            $loggable = new static($builder->getClassMetadata(), $logEntry);
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...
42 1
            $loggable->build();
43 4
        });
44
45
        Field::macro('versioned', function (Field $builder) {
46 1
            return new Versioned($builder->getClassMetadata(), $builder->getName());
47 4
        });
48
49
        ManyToOne::macro('versioned', function (ManyToOne $builder) {
50 1
            return new Versioned($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...
51 4
        });
52
53 4
        OneToOne::macro('versioned', function (OneToOne $builder) {
54 1
            return new Versioned($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...
55 4
        });
56 4
    }
57
58
    /**
59
     * Execute the build process
60
     */
61 4
    public function build()
62
    {
63
        $config = [
64 4
            'loggable' => true,
65 4
        ];
66
67 4
        if ($this->logEntry !== null) {
68 1
            $config['logEntryClass'] = $this->logEntry;
69 1
        }
70
71 4
        $this->classMetadata->addExtension(Fluent::EXTENSION_NAME, array_merge(
72 4
            $this->classMetadata->getExtension(Fluent::EXTENSION_NAME),
73
            $config
74 4
        ));
75 4
    }
76
}
77