Failed Conditions
CANCELLED  
Pull Request — master (#7095)
by Benjamin
10:13
created

CompositeValueGenerationPlan::executeDeferred()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Sequencing\Planning;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\ORM\Mapping\FieldMetadata;
10
11
class CompositeValueGenerationPlan implements ValueGenerationPlan
12
{
13
    /** @var ClassMetadata */
14
    private $class;
15
16
    /** @var ValueGenerationExecutor[] */
17
    private $executors;
18
19
    /**
20
     * @param ValueGenerationExecutor[] $executors
21
     */
22
    public function __construct(ClassMetadata $metadata, array $executors)
23
    {
24
        $this->class     = $metadata;
25
        $this->executors = $executors;
26
    }
27
28
    public function executeImmediate(EntityManagerInterface $entityManager, object $entity) : void
29
    {
30
        foreach ($this->executors as $executor) {
31
            if ($executor->isDeferred()) {
32
                continue;
33
            }
34
35
            $this->dispatchExecutor($executor, $entity, $entityManager);
36
        }
37
    }
38
39
    public function executeDeferred(EntityManagerInterface $entityManager, object $entity) : void
40
    {
41
        foreach ($this->executors as $executor) {
42
            if (! $executor->isDeferred()) {
43
                continue;
44
            }
45
46
            $this->dispatchExecutor($executor, $entity, $entityManager);
47
        }
48
    }
49
50
    private function dispatchExecutor(
51
        ValueGenerationExecutor $executor,
52
        object $entity,
53
        EntityManagerInterface $entityManager
54
    ) : void {
55
        foreach ($executor->execute($entityManager, $entity) as $columnName => $value) {
56
            // TODO LocalColumnMetadata are currently shadowed and only exposed as FieldMetadata
57
            /** @var FieldMetadata $column */
58
            $column = $this->class->getColumn($columnName);
59
            $column->setValue($entity, $value);
60
        }
61
    }
62
63
    public function containsDeferred() : bool
64
    {
65
        foreach ($this->executors as $executor) {
66
            if ($executor->isDeferred()) {
67
                return true;
68
            }
69
        }
70
71
        return false;
72
    }
73
}
74