GenerateModifiers::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Generator;
6
7
use Cycle\Schema\Definition\Entity;
8
use Cycle\Schema\Exception\SchemaException;
9
use Cycle\Schema\Exception\SchemaModifierException;
10
use Cycle\Schema\GeneratorInterface;
11
use Cycle\Schema\Registry;
12
use Cycle\Schema\SchemaModifierInterface;
13
14
/**
15
 * Generate modifiers based on their schematic definitions.
16
 */
17
final class GenerateModifiers implements GeneratorInterface
18
{
19 6
    public function run(Registry $registry): Registry
20
    {
21 6
        foreach ($registry as $entity) {
22 6
            $this->register($registry, $entity);
23
        }
24
25 2
        return $registry;
26
    }
27
28 6
    protected function register(Registry $registry, Entity $entity): void
29
    {
30 6
        $role = $entity->getRole();
31
        assert($role !== null);
32 6
        foreach ($entity->getSchemaModifiers() as $modifier) {
33
            \assert($modifier instanceof SchemaModifierInterface);
34
            try {
35 6
                $modifier->compute($registry);
36 4
            } catch (SchemaModifierException $e) {
37 4
                throw new SchemaException(
38 4
                    sprintf('Unable to compute modifier `%s` for the `%s` role.', $modifier::class, $role),
39 4
                    $e->getCode(),
40
                    $e,
41
                );
42
            }
43
        }
44 2
    }
45
}
46