Completed
Push — extensions-support ( ec18bd...3011d4 )
by Patrick
14:12
created

Softdeleteable::__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 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
ccs 4
cts 4
cp 1
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Extensions\Gedmo;
4
5
use Gedmo\Softdeleteable\Mapping\Driver\Fluent as FluentDriver;
6
use LaravelDoctrine\Fluent\Buildable;
7
use LaravelDoctrine\Fluent\Builders\Builder;
8
use LaravelDoctrine\Fluent\Builders\Field;
9
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
10
11
class Softdeleteable implements Buildable
12
{
13
    const MACRO_METHOD = 'softDelete';
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 $value;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $timeAware = false;
34
35
    /**
36
     * @param ExtensibleClassMetadata $classMetadata
37
     * @param string                  $fieldName
38
     */
39 4
    public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName)
40
    {
41 4
        $this->classMetadata = $classMetadata;
42 4
        $this->fieldName     = $fieldName;
43 4
    }
44
45
    /**
46
     * Return the name of the actual extension.
47
     *
48
     * @return string
49
     */
50 2
    public function getExtensionName()
51
    {
52 2
        return FluentDriver::EXTENSION_NAME;
53
    }
54
55
    /**
56
     * @param  bool  $value
57
     * @return $this
58
     */
59 1
    public function timeAware($value = true)
60
    {
61 1
        $this->timeAware = $value;
62
63 1
        return $this;
64
    }
65
66
    /**
67
     * @return void
68
     */
69 2
    public static function enable()
70
    {
71
        Builder::macro(static::MACRO_METHOD, function (Builder $builder, $fieldName, $type = 'dateTime') {
72
73 1
            $builder->{$type}($fieldName)->nullable();
74
75 1
            return new static($builder->getClassMetadata(), $fieldName);
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...
76 2
        });
77
78 2
        Field::macro(static::MACRO_METHOD, function (Field $builder) {
79 1
            return new static($builder->getClassMetadata(), $builder->getName());
1 ignored issue
show
Compatibility introduced by
$builder->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...
80 2
        });
81 2
    }
82
83
    /**
84
     * Execute the build process
85
     */
86 2
    public function build()
87
    {
88 2
        $this->classMetadata->addExtension($this->getExtensionName(), [
89 2
            'softDeleteable' => true,
90 2
            'fieldName'      => $this->fieldName,
91 2
            'timeAware'      => $this->timeAware
92 2
        ]);
93 2
    }
94
}
95