Completed
Push — master ( f84b1b...3c5fbc )
by Arman
14s
created

ConsoleAppTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 17
c 3
b 0
f 0
dl 0
loc 72
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validateCommand() 0 6 2
A registerCommands() 0 6 2
A loadEnvironment() 0 5 1
A registerCoreCommands() 0 6 1
A createApplication() 0 3 1
A registerAppCommands() 0 6 1
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 3.0.0
13
 */
14
15
namespace Quantum\App\Traits;
16
17
use Quantum\Environment\Exceptions\EnvException;
18
use Symfony\Component\Console\Application;
19
use Quantum\App\Exceptions\BaseException;
20
use Quantum\Di\Exceptions\DiException;
21
use Quantum\Console\CommandDiscovery;
22
use Quantum\Environment\Environment;
23
use Quantum\Loader\Setup;
24
use ReflectionException;
25
use Exception;
26
27
/**
28
 * Class ConsoleAppTrait
29
 * @package Quantum\App
30
 */
31
trait ConsoleAppTrait
32
{
33
    /**
34
     * @throws DiException
35
     * @throws ReflectionException
36
     * @throws EnvException
37
     * @throws BaseException
38
     */
39
    protected function loadEnvironment()
40
    {
41
        Environment::getInstance()
42
            ->setMutable(true)
43
            ->load(new Setup('config', 'env'));
44
    }
45
46
    /**
47
     * @param string $name
48
     * @param string $version
49
     * @return Application
50
     */
51
    public function createApplication(string $name, string $version): Application
52
    {
53
        return new Application($name, $version);
54
    }
55
56
    /**
57
     * @return void
58
     * @throws ReflectionException
59
     */
60
    private function registerCoreCommands()
61
    {
62
        $directory = framework_dir() . DS . 'Console' . DS . 'Commands';
63
        $namespace = '\\Quantum\\Console\\Commands\\';
64
65
        $this->registerCommands($directory, $namespace);
66
    }
67
68
    /**
69
     * @return void
70
     * @throws ReflectionException
71
     */
72
    private function registerAppCommands()
73
    {
74
        $directory = base_dir() . DS . 'shared' . DS . 'Commands';
75
        $namespace = '\\Shared\\Commands\\';
76
77
        $this->registerCommands($directory, $namespace);
78
    }
79
80
    /**
81
     * @param string $directory
82
     * @param string $namespace
83
     * @throws ReflectionException
84
     */
85
    private function registerCommands(string $directory, string $namespace)
86
    {
87
        $commands = CommandDiscovery::discover($directory, $namespace);
88
89
        foreach ($commands as $command) {
90
            $this->application->add(new $command['class']());
91
        }
92
    }
93
94
    /**
95
     * @throws Exception
96
     */
97
    private function validateCommand(): void
98
    {
99
        $commandName = $this->input->getFirstArgument();
100
101
        if (!$this->application->has($commandName)) {
102
            throw new Exception("Command `$commandName` is not defined");
103
        }
104
    }
105
}
106