SimpleArrayResolver::instantiate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace NilPortugues\MessageBus\QueryBus\Resolver;
4
5
use InvalidArgumentException;
6
use NilPortugues\MessageBus\QueryBus\Contracts\QueryHandler;
7
use NilPortugues\MessageBus\QueryBus\Contracts\QueryHandlerResolver;
8
9 View Code Duplication
class SimpleArrayResolver implements QueryHandlerResolver
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    /** @var array */
12
    protected $handlers = [];
13
14
    /**
15
     * ArrayResolver constructor.
16
     *
17
     * @param array $handlers
18
     */
19
    public function __construct(array $handlers)
20
    {
21
        $this->handlers = $handlers;
22
    }
23
24
    /**
25
     * Given a string to identify the Query Handler, return the instance.
26
     *
27
     * @param string $handler
28
     *
29
     * @return QueryHandler
30
     *
31
     * @throws InvalidArgumentException
32
     */
33
    public function instantiate(string $handler) : QueryHandler
34
    {
35
        if (false === isset($this->handlers[$handler])) {
36
            throw new InvalidArgumentException(
37
                sprintf('Handler %s could not be found. Did you register it?', $handler)
38
            );
39
        }
40
41
        $callable = $this->handlers[$handler];
42
43
        return ($callable instanceof \Closure) ? $callable() : new $callable();
44
    }
45
}
46