ServiceCreatorTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 12
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A invalidFactoryException() 0 7 1
A getResolvedFactory() 0 5 2
A isValidFactory() 0 5 1
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