ServiceCreatorTrait::getResolvedFactory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Panamax\Traits;
4
5
use Panamax\Contracts\ServiceFactoryInterface;
6
use RuntimeException;
7
use UnexpectedValueException;
8
9
trait ServiceCreatorTrait
10
{
11
    protected function isValidFactory($factory): bool
12
    {
13
        return in_array(
14
            ServiceFactoryInterface::class,
15
            class_implements($factory)
16
        );
17
    }
18
19
    protected function getResolvedFactory($factory): ServiceFactoryInterface
20
    {
21
        return $factory instanceof ServiceFactoryInterface
22
            ? $factory
23
            : $factory::instance();
24
    }
25
26
    protected function invalidFactoryException(string $serviceId, $found): RuntimeException
27
    {
28
        $interface = ServiceFactoryInterface::class;
29
        $found = gettype($found);
30
31
        return new UnexpectedValueException(
32
            "Value of \"factory\" for service \"{$serviceId}\" must implement \"{$interface}\"; {$found} found."
33
        );
34
    }
35
}
36