GenerateModifiers::register()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 9.9
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