ConsoleApplication   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

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

5 Methods

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