Passed
Pull Request — master (#190)
by Arman
02:54
created

ConsoleAppAdapter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A start() 0 27 3
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\App\Adapters;
16
17
use Quantum\Libraries\Config\Exceptions\ConfigException;
18
use Quantum\Libraries\Lang\Exceptions\LangException;
19
use Symfony\Component\Console\Output\ConsoleOutput;
20
use Quantum\Environment\Exceptions\EnvException;
21
use Quantum\Exceptions\StopExecutionException;
22
use Symfony\Component\Console\Input\ArgvInput;
23
use Symfony\Component\Console\Application;
24
use Quantum\App\Traits\ConsoleAppTrait;
25
use Quantum\App\Contracts\AppInterface;
26
use Quantum\Di\Exceptions\DiException;
27
use Quantum\Exceptions\BaseException;
28
use ReflectionException;
29
30
if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
31
32
/**
33
 * Class ConsoleAppAdapter
34
 * @package Quantum\App
35
 */
36
class ConsoleAppAdapter extends AppAdapter implements AppInterface
37
{
38
39
    use ConsoleAppTrait;
40
41
    /**
42
     * Console application name
43
     * @var string
44
     */
45
    private $name = 'Qt Console Application';
46
47
    /**
48
     * Console application version
49
     * @var string
50
     */
51
    private $version = '2.x';
52
53
    /**
54
     * @var Application
55
     */
56
    protected $application;
57
58
    /**
59
     * @return int|null
60
     * @throws DiException
61
     * @throws EnvException
62
     * @throws BaseException
63
     * @throws ConfigException
64
     * @throws LangException
65
     * @throws ReflectionException
66
     */
67
    public function start(): ?int
68
    {
69
        $input = new ArgvInput();
70
        $output = new ConsoleOutput();
71
72
        try {
73
            $this->application = $this->createApplication($this->name, $this->version);
74
75
            if ($this->application->getName() !== 'core:env') {
76
                $this->loadEnvironment();
77
            }
78
79
            $this->loadConfig();
80
            $this->loadLanguage();
81
82
            $this->registerCoreCommands();
83
            $this->registerAppCommands();
84
85
            $this->setupErrorHandler();
86
87
            $this->validateCommand($input);
88
89
            $exitCode = $this->application->run($input, $output);
90
91
            stop(null, $exitCode);
92
        } catch (StopExecutionException $exception) {
93
            return $exception->getCode();
94
        }
95
    }
96
}