Passed
Push — fix-FRAM-43-cast-id-generator ( b810e9 )
by Vincent
08:26
created

AbstractGenerator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 12
eloc 24
c 1
b 1
f 0
dl 0
loc 139
ccs 31
cts 33
cp 0.9394
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A mapper() 0 3 1
A connection() 0 3 1
A generate() 0 9 2
A __construct() 0 4 1
A setCurrentConnection() 0 3 1
A doGenerate() 0 2 1
A postProcess() 0 15 3
A getPropertyToHydrate() 0 3 1
A lastGeneratedId() 0 3 1
1
<?php
2
3
namespace Bdf\Prime\IdGenerators;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Exception\PrimeException;
7
use Bdf\Prime\Mapper\Mapper;
8
use Bdf\Prime\ServiceLocator;
9
10
/**
11
 * AbstractGenerator
12
 *
13
 * A basic property generator. Let inheritance set its generation algorithm.
14
 *
15
 * @template C as ConnectionInterface
16
 * @implements GeneratorInterface<C>
17
 */
18
abstract class AbstractGenerator implements GeneratorInterface
19
{
20
    /**
21
     * The associated mapper
22
     *
23
     * @var Mapper
24
     */
25
    private $mapper;
26
    
27
    /**
28
     * The active connection
29
     *
30
     * @var C
31
     */
32
    private $connection;
33
34
    /**
35
     * The last generated id
36
     *
37
     * @var string
38
     */
39
    protected $lastGeneratedId;
40
41
    /**
42
     * Le primary attribute n'est effacé que s'il est vide.
43
     * La valeur du last insert ID sera injecté dans entity que si
44
     * son attribut primary aura été vidé
45
     *
46
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
47
     */
48
    protected $hasBeenErased = true;
49
    
50
    /**
51
     * @param Mapper $mapper
52
     */
53 94
    public function __construct(Mapper $mapper = null)
54
    {
55
        // TODO: reference cyclique
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
56 94
        $this->mapper = $mapper;
57 94
    }
58
    
59
    /**
60
     * Get the mapper
61
     * 
62
     * @return Mapper
63
     */
64 19
    public function mapper()
65
    {
66 19
        return $this->mapper;
67
    }
68
    
69
    /**
70
     * Get connection
71
     * 
72
     * @return C
73
     */
74 98
    public function connection()
75
    {
76 98
        return $this->connection;
77
    }
78
    
79
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connection should have a doc-comment as per coding-style.
Loading history...
80
     * {@inheritdoc}
81
     */
82 426
    public function setCurrentConnection(ConnectionInterface $connection)
83
    {
84 426
        $this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $connection of type Bdf\Prime\Connection\ConnectionInterface is incompatible with the declared type Bdf\Prime\IdGenerators\C of property $connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
85 426
    }
86
    
87
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $serviceLocator should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $data should have a doc-comment as per coding-style.
Loading history...
88
     * {@inheritdoc}
89
     */
90 363
    public function generate(array &$data, ServiceLocator $serviceLocator)
91
    {
92 363
        $this->hasBeenErased = false;
93
94 363
        $property = $this->getPropertyToHydrate();
95
96 363
        if (empty($data[$property])) {
97 100
            $this->lastGeneratedId = $this->doGenerate($property, $data, $serviceLocator);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->lastGeneratedId is correct as $this->doGenerate($prope...$data, $serviceLocator) targeting Bdf\Prime\IdGenerators\A...Generator::doGenerate() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
98 100
            $this->hasBeenErased = true;
99
        }
100 363
    }
101
102
    /**
103
     * Do the ID generation
104
     *
105
     * @param string           $property        The aimed property
106
     * @param array            $data            By reference
107
     * @param ServiceLocator   $serviceLocator
108
     *
109
     * @return string|null   Returns the generated id
0 ignored issues
show
introduced by
Function return type is not void, but function has no return statement
Loading history...
110
     * @throws PrimeException
111
     */
112
    protected function doGenerate($property, array &$data, ServiceLocator $serviceLocator)
0 ignored issues
show
Unused Code introduced by
The parameter $property is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

112
    protected function doGenerate(/** @scrutinizer ignore-unused */ $property, array &$data, ServiceLocator $serviceLocator)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
        // to overload
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
115
    }
116
117
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $entity should have a doc-comment as per coding-style.
Loading history...
118
     * {@inheritdoc}
119
     */
120 361
    public function postProcess($entity)
121
    {
122 361
        if (!$this->hasBeenErased) {
123 273
            return;
124
        }
125
126 98
        $propertyName = $this->getPropertyToHydrate();
127 98
        $propertyMetadata = $this->mapper->metadata()->attributes[$propertyName];
128 98
        $value = $this->lastGeneratedId();
129
130 98
        if (empty($propertyMetadata['phpOptions']['ignore_generator'])) {
131 97
            $value = $this->connection->fromDatabase($value, $propertyMetadata['type'], $propertyMetadata['phpOptions'] ?? []);
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
132
        }
133
134 98
        $this->mapper->hydrateOne($entity, $propertyName, $value);
135 98
    }
136
137
    /**
138
     * Get the last generated id
139
     *
140
     * @return string
141
     */
142 20
    protected function lastGeneratedId()
143
    {
144 20
        return $this->lastGeneratedId;
145
    }
146
147
    /**
148
     * Get the property name to hydrate
149
     *
150
     * Returns by default the primary property
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
151
     *
152
     * @return string
153
     */
154 363
    protected function getPropertyToHydrate()
155
    {
156 363
        return $this->mapper->metadata()->primary['attributes'][0];
157
    }
158
}
159