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

RelationBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
eloc 10
c 3
b 0
f 1
dl 0
loc 18
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 16 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