ServiceCreatorTrait::invalidFactoryException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
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