Passed
Branch master (372b2a)
by Filipe
01:32
created

ConsoleApplication   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 24
c 1
b 0
f 0
dl 0
loc 65
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A commandLine() 0 6 2
A useCommandLine() 0 3 1
A __construct() 0 5 1
A createCommandLine() 0 7 1
A run() 0 19 3
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Infrastructure\Console;
13
14
use Composer\Autoload\ClassLoader;
15
use Exception;
16
use JsonException;
17
use Slick\Configuration\ConfigurationInterface;
18
use Slick\ModuleApi\Infrastructure\Console\ConsoleModuleInterface;
19
use Slick\WebStack\ConsoleModule;
20
use Slick\WebStack\FrontControllerModule;
21
use Slick\WebStack\Infrastructure\AbstractApplication;
22
use Slick\WebStack\Infrastructure\ApplicationSettingsInterface;
0 ignored issues
show
Bug introduced by
The type Slick\WebStack\Infrastru...cationSettingsInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Slick\WebStack\Infrastructure\ComposerParser;
24
use Symfony\Component\Console\Application;
25
use function Slick\ModuleApi\constantValue;
26
27
/**
28
 * ConsoleApplication
29
 *
30
 * @package Slick\WebStack\Infrastructure\Console
31
 */
32
final class ConsoleApplication extends AbstractApplication
33
{
34
    
35
    private ?Application $commandLine = null;
36
    
37
    public function __construct(string $rootPath, ?ClassLoader $classLoader = null)
38
    {
39
        parent::__construct($rootPath, $classLoader);
40
        $this->modules[] = new ConsoleModule();
41
        $this->modules[] = new FrontControllerModule();
42
    }
43
44
45
    /**
46
     * @throws JsonException
47
     * @throws Exception
48
     */
49
    public function run(): null
50
    {
51
        $container = $this->prepareContainer();
52
        $commandsDir = $container->get(ConfigurationInterface::class)->get('console.commands_dir');
53
54
        $loader = new ConsoleCommandLoader($container, APP_ROOT . $commandsDir);
55
        $cli = $this->commandLine();
56
57
        $cli->setCatchExceptions(true);
58
        $cli->setCommandLoader($loader);
59
60
        foreach ($this->modules as $module) {
61
            if ($module instanceof ConsoleModuleInterface) {
62
                $module->configureConsole($cli, $container);
63
            }
64
        }
65
66
        $cli->run();
67
        return null;
68
    }
69
70
    /**
71
     * @throws JsonException
72
     */
73
    public function commandLine(): Application
74
    {
75
        if (null === $this->commandLine) {
76
            $this->commandLine = $this->createCommandLine();
77
        }
78
        return $this->commandLine;
79
    }
80
81
    public function useCommandLine(?Application $commandLine): void
82
    {
83
        $this->commandLine = $commandLine;
84
    }
85
86
    /**
87
     * @return Application
88
     * @throws JsonException
89
     */
90
    public function createCommandLine(): Application
91
    {
92
        $composerParser = new ComposerParser(APP_ROOT . '/composer.json');
93
94
        return new Application(
95
            constantValue('APP_NAME', $composerParser->appName()),
96
            constantValue('APP_VERSION', $composerParser->version())
97
        );
98
    }
99
}
100