Passed
Push — master ( 983928...8bea99 )
by Paweł
02:09
created

CommandsLoader::runCommandFromName()   B

Complexity

Conditions 6
Paths 51

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
eloc 28
c 4
b 0
f 0
nc 51
nop 2
dl 0
loc 39
rs 8.8497
1
<?php
2
3
namespace pjpawel\LightApi\Command;
4
5
use Exception;
6
use pjpawel\LightApi\Command\Input\Stdin;
7
use pjpawel\LightApi\Command\Output\Stdout;
8
use pjpawel\LightApi\Container\ClassDefinition;
9
use pjpawel\LightApi\Container\ContainerLoader;
10
use pjpawel\LightApi\Container\LazyServiceInterface;
11
use ReflectionClass;
12
use ReflectionNamedType;
13
14
class CommandsLoader
15
{
16
17
    /**
18
     * @var array<string,string>
19
     */
20
    public array $command = [];
21
    public bool $loaded = true;
22
23
24
    public function registerCommand(string $commandName, string $className): void
25
    {
26
        $this->command[$commandName] = $className;
27
    }
28
29
    /**
30
     * @param string $commandName
31
     * @param ContainerLoader $container
32
     * @return int
33
     */
34
    public function runCommandFromName(string $commandName, ContainerLoader $container): int
35
    {
36
        $stdout = new Stdout();
37
        try {
38
            $className = $this->command[$commandName];
39
            $reflectionClass = new ReflectionClass($className);
40
            $constructor = $reflectionClass->getConstructor();
41
            if ($constructor !== null) {
42
                /** @var ClassDefinition $classDefinition */
43
                $classDefinition = $container->get($className);
44
                $args = $classDefinition->arguments;
45
                foreach ($constructor->getParameters() as $parameter) {
46
                    $parameterType = $parameter->getType();
47
                    if ($parameterType instanceof ReflectionNamedType) {
48
                        $args[] = $container->get($parameterType->getName());
49
                    }
50
                }
51
            } else {
52
                $args = [];
53
            }
54
            $stdin = new Stdin();
55
            /** @var Command $command */
56
            $command = $reflectionClass->newInstanceArgs($args);
57
            $command->prepare($stdin);
58
            /* Prepare input */
59
            $stdin->load();
60
            /* Inject services */
61
            if (is_subclass_of($command, LazyServiceInterface::class)) {
62
                $command->setContainer($container->prepareContainerBag($command::getAllServices()));
0 ignored issues
show
Bug introduced by
The method setContainer() does not exist on pjpawel\LightApi\Command\Command. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
                $command->/** @scrutinizer ignore-call */ 
63
                          setContainer($container->prepareContainerBag($command::getAllServices()));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAllServices() does not exist on pjpawel\LightApi\Command\Command. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
                $command->setContainer($container->prepareContainerBag($command::/** @scrutinizer ignore-call */ getAllServices()));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
            }
64
            return $command->execute($stdin, $stdout);
65
        } catch (Exception $e) {
66
            $stdout->writeln([
67
                'Exception thrown during command',
68
                $e->getMessage(),
69
                'file: ' . $e->getFile(),
70
                'line: ' . $e->getLine()
71
            ]);
72
            return Command::FAILURE;
73
        }
74
    }
75
76
    public function getCommandNameFromServer(): string
77
    {
78
        return $_SERVER['argv'][0];
79
    }
80
}