Passed
Push — dev_2x ( fa57a5...f00e1a )
by Adrian
01:48
created

RelationBuilder::build()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 4
nc 6
nop 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Relation;
5
6
use Sirius\Orm\Helpers\Str;
7
use Sirius\Orm\Mapper;
8
use Sirius\Orm\Orm;
9
10
class RelationBuilder
11
{
12
    public function build(Orm $orm, Mapper $mapper, string $name, array $options): Relation
13
    {
14
        $foreignMapper = $options[RelationConfig::FOREIGN_MAPPER];
15
        if ($orm->has($foreignMapper)) {
16
            if (! $foreignMapper instanceof Mapper) {
17
                $foreignMapper = $orm->get($foreignMapper);
18
            }
19
        }
20
        $type          = $options[RelationConfig::TYPE];
21
        $relationClass = 'Sirius\\Orm\\Relation\\' . Str::className($type);
22
23
        if (! class_exists($relationClass)) {
24
            throw new InvalidArgumentException("{$relationClass} does not exist");
0 ignored issues
show
Bug introduced by
The type Sirius\Orm\Relation\InvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
25
        }
26
27
        return new $relationClass($name, $mapper, $foreignMapper, $options);
28
    }
29
}
30