Application   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 97
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 11 1
A __construct() 0 9 4
A setConfiguration() 0 5 1
A setCommands() 0 5 1
A run() 0 12 1
1
<?php
2
3
namespace Equip\Console;
4
5
use Auryn\Injector;
6
use Equip\Configuration\ConfigurationSet;
7
use Equip\Console\Command\CommandSet;
8
use Symfony\Component\Console\Application as ConsoleApplication;
9
10
class Application
11
{
12
    /**
13
     * Create a new console application
14
     * 
15
     * @param Injector|null $injector
16
     * @param ConfigurationSet|null $configuration
17
     * @param CommandSet|null $commands
18
     * 
19
     * @return static
20
     */
21
    public static function build(
22
        Injector $injector = null,
23
        ConfigurationSet $configuration = null,
24
        CommandSet $commands = null
25
    ) {
26
        return new static(
27
            $injector,
28
            $configuration,
29
            $commands
30
        );
31
    }
32
33
    /**
34
     * @var Injector
35
     */
36
    private $injector;
37
38
    /**
39
     * @var ConfigurationSet
40
     */
41
    private $configuration;
42
43
    /**
44
     * @var CommandSet
45
     */
46
    private $commands;
47
48
    /**
49
     * @param Injector|null $injector
50
     * @param ConfigurationSet|null $configuration
51
     * @param CommandSet|null $commands
52
     */
53
    public function __construct(
54
        Injector $injector = null,
55
        ConfigurationSet $configuration = null,
56
        CommandSet $commands = null
57
    ) {
58
        $this->injector = $injector ?: new Injector;
59
        $this->configuration = $configuration ?: new ConfigurationSet;
60
        $this->commands = $commands ?: new CommandSet;
61
    }
62
63
    /**
64
     * Set configuration
65
     * 
66
     * @param array $configuration
67
     * 
68
     * @return $this
69
     */
70
    public function setConfiguration(array $configuration)
71
    {
72
        $this->configuration = $this->configuration->withValues($configuration);
73
        return $this;
74
    }
75
    
76
    /**
77
     * Set commands
78
     * 
79
     * @param array $commands
80
     * 
81
     * @return $this
82
     */
83
    public function setCommands(array $commands)
84
    {
85
        $this->commands = $this->commands->withValues($commands);
86
        return $this;
87
    }
88
89
    /**
90
     * Start the console application
91
     * 
92
     * @return int 0 if everything went fine, or an error code
93
     */
94
    public function run()
95
    {
96
        $this->configuration->apply($this->injector);
97
        
98
        $application = $this->injector->make(ConsoleApplication::class);
99
        
100
        array_map(function ($command) use ($application) {
101
            $application->add($this->injector->make($command));
102
        }, $this->commands->toArray());
103
        
104
        return $application->run();
105
    }
106
}
107