Completed
Push — extensions-support ( e1ac66...5004a5 )
by Guido
03:43
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 0
Metric Value
c 1
b 0
f 0
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
use LaravelDoctrine\Fluent\Extensions\Extension;
11
12
class SoftDeleteable implements Buildable, Extension
13
{
14
    const MACRO_METHOD = 'softDelete';
15
16
    /**
17
     * @var ExtensibleClassMetadata
18
     */
19
    protected $classMetadata;
20
21
    /**
22
     * @var string
23
     */
24
    protected $fieldName;
25
26
    /**
27
     * @var string
28
     */
29
    protected $value;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $timeAware = false;
35
36
    /**
37
     * @param ExtensibleClassMetadata $classMetadata
38
     * @param string                  $fieldName
39
     */
40 4
    public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName)
41
    {
42 4
        $this->classMetadata = $classMetadata;
43 4
        $this->fieldName     = $fieldName;
44 4
    }
45
46
    /**
47
     * Return the name of the actual extension.
48
     *
49
     * @return string
50
     */
51 2
    public function getExtensionName()
52
    {
53 2
        return FluentDriver::EXTENSION_NAME;
54
    }
55
56
    /**
57
     * @param  bool  $value
58
     * @return $this
59
     */
60 1
    public function timeAware($value = true)
61
    {
62 1
        $this->timeAware = $value;
63
64 1
        return $this;
65
    }
66
67
    /**
68
     * @return void
69
     */
70 80
    public static function enable()
71
    {
72
        Builder::macro(static::MACRO_METHOD, function (Builder $builder, $fieldName = 'deletedAt', $type = 'dateTime') {
73
74 1
            $builder->{$type}($fieldName)->nullable();
75
76 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...
77 80
        });
78
79 80
        Field::macro(static::MACRO_METHOD, function (Field $builder) {
80 1
            return new static($builder->getClassMetadata(), $builder->getName());
81 80
        });
82 80
    }
83
84
    /**
85
     * Execute the build process
86
     */
87 2
    public function build()
88
    {
89 2
        $this->classMetadata->addExtension($this->getExtensionName(), [
90 2
            'softDeleteable' => true,
91 2
            'fieldName'      => $this->fieldName,
92 2
            'timeAware'      => $this->timeAware
93 2
        ]);
94 2
    }
95
}
96