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

Softdeleteable   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 84
rs 10
ccs 24
cts 24
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getExtensionName() 0 4 1
A timeAware() 0 6 1
A enable() 0 13 1
A build() 0 8 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