GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

NativeCommandHandlerResolver::resolve()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 9
nop 1
1
<?php
2
3
namespace Chief\Resolvers;
4
5
use Chief\Command;
6
use Chief\CommandHandler;
7
use Chief\CommandHandlerResolver;
8
use Chief\Container;
9
use Chief\Exceptions\UnresolvableCommandHandlerException;
10
use Chief\Handlers\CallableCommandHandler;
11
use Chief\Handlers\LazyLoadingCommandHandler;
12
use Chief\Containers\NativeContainer;
13
14
class NativeCommandHandlerResolver implements CommandHandlerResolver
15
{
16
    protected $container;
17
18
    protected $handlers = [];
19
20
    public function __construct(Container $container = null)
21
    {
22
        $this->container = $container ?: new NativeContainer;
23
    }
24
25
    /**
26
     * Retrieve a CommandHandler for a given Command
27
     *
28
     * @param Command $command
29
     * @return CommandHandler
30
     * @throws UnresolvableCommandHandlerException
31
     */
32
    public function resolve(Command $command)
33
    {
34
        $commandName = get_class($command);
35
36
        // Find the CommandHandler if it has been manually defined using pushHandler()
37
        foreach ($this->handlers as $handlerCommand => $handler) {
38
            if ($handlerCommand == $commandName) {
39
                return $handler;
40
            }
41
        }
42
43
        // If the Command also implements CommandHandler, then it can handle() itself
44
        if ($command instanceof CommandHandler) {
45
            return $command;
46
        }
47
48
        // Try and guess the handler's name in the same namespace with suffix "Handler"
49
        $class = $commandName . 'Handler';
50
        if (class_exists($class)) {
51
            return $this->container->make($class);
52
        }
53
54
        // Try and guess the handler's name in nested "Handlers" namespace with suffix "Handler"
55
        $classParts = explode('\\', $commandName);
56
        $commandNameWithoutNamespace = array_pop($classParts);
57
        $class = implode('\\', $classParts) . '\\Handlers\\' . $commandNameWithoutNamespace . 'Handler';
58
        if (class_exists($class)) {
59
            return $this->container->make($class);
60
        }
61
62
        throw new UnresolvableCommandHandlerException('Could not resolve a handler for [' . get_class($command) . ']');
63
    }
64
65
    /**
66
     * Bind a handler to a command. These bindings should overrule the default
67
     * resolution behaviour for this resolver
68
     *
69
     * @param string $commandName
70
     * @param CommandHandler|callable|string $handler
71
     * @return void
72
     * @throws \InvalidArgumentException
73
     */
74
    public function bindHandler($commandName, $handler)
75
    {
76
        // If the $handler given is an instance of CommandHandler, simply bind that
77
        if ($handler instanceof CommandHandler) {
78
            $this->handlers[$commandName] = $handler;
79
            return;
80
        }
81
82
        // If the handler given is callable, wrap it up in a CallableCommandHandler for executing later
83
        if (is_callable($handler)) {
84
            return $this->bindHandler($commandName, new CallableCommandHandler($handler));
85
        }
86
87
        // If the handler given is a string, wrap it up in a LazyLoadingCommandHandler for loading later
88
        if (is_string($handler)) {
89
            return $this->bindHandler($commandName, new LazyLoadingCommandHandler($handler, $this->container));
90
        }
91
92
        throw new \InvalidArgumentException('Could not push handler. Command Handlers should be an
93
            instance of Chief\CommandHandler, a callable, or a string representing a CommandHandler class');
94
    }
95
}
96