Passed
Push — 2.0 ( 21259a...8b13d0 )
by Sébastien
19:00
created

AbstractGenerator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
eloc 24
dl 0
loc 139
ccs 27
cts 29
cp 0.931
rs 10
c 2
b 1
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
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 mapper() 0 3 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
47
     */
48
    protected $hasBeenErased = true;
49
50
    /**
51
     * @param Mapper $mapper
52
     */
53 315
    public function __construct(Mapper $mapper = null)
54
    {
55
        // TODO: reference cyclique
56 315
        $this->mapper = $mapper;
57
    }
58
59
    /**
60
     * Get the mapper
61
     *
62
     * @return Mapper
63
     */
64 21
    public function mapper()
65
    {
66 21
        return $this->mapper;
67
    }
68
69
    /**
70
     * Get connection
71
     *
72
     * @return C
73
     */
74 106
    public function connection()
75
    {
76 106
        return $this->connection;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 648
    public function setCurrentConnection(ConnectionInterface $connection): void
83
    {
84 648
        $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
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 585
    public function generate(array &$data, ServiceLocator $serviceLocator): void
91
    {
92 585
        $this->hasBeenErased = false;
93
94 585
        $property = $this->getPropertyToHydrate();
95
96 585
        if (empty($data[$property])) {
97 109
            $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 109
            $this->hasBeenErased = true;
99
        }
100
    }
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
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
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 583
    public function postProcess($entity): void
121
    {
122 583
        if (!$this->hasBeenErased) {
123 492
            return;
124
        }
125
126 107
        $propertyName = $this->getPropertyToHydrate();
127 107
        $propertyMetadata = $this->mapper->metadata()->attributes[$propertyName];
128 107
        $value = $this->lastGeneratedId();
129
130 107
        if (empty($propertyMetadata['phpOptions']['ignore_generator'])) {
131 106
            $value = $this->connection->fromDatabase($value, $propertyMetadata['type'], $propertyMetadata['phpOptions'] ?? []);
132
        }
133
134 107
        $this->mapper->hydrateOne($entity, $propertyName, $value);
135
    }
136
137
    /**
138
     * Get the last generated id
139
     *
140
     * @return string
141
     */
142 22
    protected function lastGeneratedId()
143
    {
144 22
        return $this->lastGeneratedId;
145
    }
146
147
    /**
148
     * Get the property name to hydrate
149
     *
150
     * Returns by default the primary property
151
     *
152
     * @return string
153
     */
154 585
    protected function getPropertyToHydrate()
155
    {
156 585
        return $this->mapper->metadata()->primary['attributes'][0];
157
    }
158
}
159