Passed
Push — main ( 06b87c...8c7008 )
by Stanislav
02:19
created

findArgumentResolverClassName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.5923

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 16
ccs 6
cts 9
cp 0.6667
crap 4.5923
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Diezz\YamlToObjectMapper\Resolver;
4
5
use phpDocumentor\Reflection\Types\ClassString;
6
use RuntimeException;
7
8
class ArgumentResolverFactory
9
{
10
    private array $registeredResolvers = [
11
        'format'    => FormatArgumentResolver::class,
12
        'substring' => SubstringArgumentResolver::class,
13
        'now'       => NowArgumentResolver::class,
14
        'self'      => SelfArgumentResolver::class,
15
        'env'       => EnvironmentArgumentResolver::class,
16
    ];
17
18 1
    public function addResolver(string $resolverAlias, string $resolverClassName): void
19
    {
20 1
        $interfaces = class_implements($resolverClassName);
21 1
        if ($interfaces && in_array(CustomArgumentResolver::class, $interfaces, true)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $interfaces of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
22
            throw new RuntimeException('Resolver must be an instance of ArgumentResolver class');
23
        }
24 1
        if (array_key_exists($resolverAlias, $this->registeredResolvers)) {
25
            throw new RuntimeException("Resolver with name $resolverAlias has already registered");
26
        }
27 1
        $this->registeredResolvers[$resolverAlias] = $resolverClassName;
28 1
    }
29
30
    /**
31
     * @throws ArgumentResolverException
32
     */
33 1
    public function create(string $provider, array $arguments): ArgumentResolver
34
    {
35 1
        $argumentResolverClassName = $this->findArgumentResolverClassName($provider);
36 1
        if (empty($arguments)) {
37
            $arguments[] = null;
38
        }
39
40 1
        return new $argumentResolverClassName(...$arguments);
41
    }
42
43
    /**
44
     * @throws ArgumentResolverException
45
     *
46
     * @return ClassString
47
     */
48 1
    private function findArgumentResolverClassName(string $argumentResolverName): string
49
    {
50 1
        if (!array_key_exists($argumentResolverName, $this->registeredResolvers)) {
51
            throw new ArgumentResolverException("Couldn't find suitable argument resolver for name $argumentResolverName");
52
        }
53
54 1
        $argumentResolverClassName = $this->registeredResolvers[$argumentResolverName];
55
56 1
        if (!class_exists($argumentResolverClassName)) {
57
            throw new ArgumentResolverException("Unable to find class $argumentResolverName");
58
        }
59 1
        if (!is_subclass_of($argumentResolverClassName, ArgumentResolver::class)) {
60
            throw new ArgumentResolverException("The argument resolver $argumentResolverClassName must extends ArgumentResolver abstract class");
61
        }
62
63 1
        return $argumentResolverClassName;
64
    }
65
}
66