Completed
Push — master ( 466032...5da320 )
by Marco
02:49
created

Processor::setApplicationVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php namespace Comodojo\Foundation\Console;
2
3
use \Comodojo\Foundation\Base\Configuration;
4
use \Comodojo\Foundation\Base\ConfigurationTrait;
5
use \Symfony\Component\Console\Application;
6
use \Symfony\Component\EventDispatcher\EventDispatcher;
7
use \Symfony\Component\Console\ConsoleEvents;
8
9
10
/**
11
 * @package     Comodojo Foundation
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     MIT
14
 *
15
 * LICENSE:
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
class Processor {
27
28
    use ConfigurationTrait;
29
30
    protected $application_header;
31
32
    protected $application_version;
33
34
    protected $application;
35
36
    // $commands should be an array of classes
37
38
    public function __construct(
39
        Configuration $configuration,
40
        array $commands,
41
        $application_header = null,
42
        $application_version = null
43
    ) {
44
45
        $events = new EventDispatcher();
46
        $application = new Application();
47
        $application->setDispatcher($events);
48
        $this->setApplication($application);
49
        $this->setConfiguration($configuration);
50
51
        $events->addListener(ConsoleEvents::COMMAND, '\Comodojo\Foundation\Console\StartEventListener::listen');
52
        $events->addListener(ConsoleEvents::TERMINATE, '\Comodojo\Foundation\Console\StopEventListener::listen');
53
54
        if ( !empty($application_header) ) $this->setApplicationHeader($application_header);
55
        if ( !empty($application_version) ) $this->application->setVersion($application_version);
56
57
        foreach ($commands as $command) {
58
59
            $this->application->add(
60
                $command::init($configuration)
61
            );
62
63
        }
64
65
    }
66
67
    public function setApplicationHeader($header) {
68
        $this->application_header = $header;
69
        $this->application->setName($header);
70
        return $this;
71
    }
72
73
    public function getApplicationHeader() {
74
        return $this->application_header;
75
    }
76
77
    public function setApplicationVersion($version) {
78
        $this->application_version = $version;
79
        $this->application->setVersion($version);
80
        return $this;
81
    }
82
83
    public function getApplicationVersion() {
84
        return $this->application_version;
85
    }
86
87
    public function run() {
88
89
        $this->application->run();
90
91
    }
92
93
    public function setApplication(Application $application) {
94
95
        $this->application = $application;
96
        return $this;
97
98
    }
99
100
    public function getApplication() {
101
102
        return $this->application;
103
104
    }
105
106
    public static function init(
107
        Configuration $configuration,
108
        array $commands,
109
        $application_header = null,
110
        $application_version = null
111
    ) {
112
113
        return new Processor(
114
            $configuration,
115
            $commands,
116
            $application_header,
117
            $application_version
118
        );
119
120
    }
121
122
}
123