Completed
Push — master ( c5d53f...d2b8ac )
by Sebastien
02:26
created

Cerbere::getParser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Cerbere;
4
5
use Cerbere\Action\ActionInterface;
6
use Cerbere\Event\CerbereEvents;
7
use Cerbere\Event\CerbereFileDiscoverEvent;
8
use Cerbere\Event\CerbereLoggerListener;
9
use Cerbere\Model\Job;
10
use Cerbere\Model\Project;
11
use Cerbere\Parser\ParserInterface;
12
use Symfony\Component\EventDispatcher\EventDispatcher;
13
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14
15
/**
16
 * Class Cerbere
17
 *
18
 * @package Cerbere
19
 */
20
class Cerbere
21
{
22
    /**
23
     * @var ParserInterface[]
24
     */
25
    protected $parsers = array();
26
27
    /**
28
     * @var EventDispatcherInterface
29
     */
30
    protected $dispatcher;
31
32
    /**
33
     */
34
    public function __construct()
35
    {
36
    }
37
38
    /**
39
     * @param CerbereLoggerListener $listener
40
     *
41
     * @return $this
42
     */
43
    public function addLoggerListener(CerbereLoggerListener $listener)
44
    {
45
        $this->getDispatcher()->addSubscriber($listener);
46
47
        return $this;
48
    }
49
50
    /**
51
     * Gets the dispatcher used by this library to dispatch events.
52
     *
53
     * @return EventDispatcherInterface
54
     */
55
    public function getDispatcher()
56
    {
57
        if (!isset($this->dispatcher)) {
58
            $this->dispatcher = new EventDispatcher();
59
        }
60
61
        return $this->dispatcher;
62
    }
63
64
    /**
65
     * Sets the dispatcher used by this library to dispatch events.
66
     *
67
     * @param EventDispatcherInterface $dispatcher
68
     *   The Symfony event dispatcher object.
69
     *
70
     * @return $this
71
     */
72
    public function setDispatcher(EventDispatcherInterface $dispatcher)
73
    {
74
        $this->dispatcher = $dispatcher;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param ParserInterface $parser
81
     *
82
     * @return $this
83
     */
84
    public function addParser(ParserInterface $parser)
85
    {
86
        $this->parsers[$parser->getCode()] = $parser;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param string $code
93
     * @return ParserInterface|null
94
     */
95
    public function getParser($code)
96
    {
97
        if (isset($this->parsers[$code])) {
98
            return $this->parsers[$code];
99
        }
100
101
        return null;
102
    }
103
104
    /**
105
     * @param Job $job
106
     * @param ActionInterface $action
107
     * @param array $options
108
     *
109
     * @return array
110
     */
111
    public function run(Job $job, ActionInterface $action, $options = array())
112
    {
113
        // Download remote project if remote.
114
        $dir = $job->checkoutRepository();
115
116
        // Move to project folder.
117
        $currentDirectory = getcwd();
118
        chdir($dir);
119
120
        // Load projects from repository.
121
        $projects = $this->getProjectsFromPatterns($job->getPatterns());
122
123
        // Do cerbere action.
124
        $report = $action->process($projects, $options);
125
126
        // Restore initial directory.
127
        chdir($currentDirectory);
128
129
        return $report;
130
    }
131
132
    /**
133
     * @return Project[]
134
     */
135
    public function getProjectsFromPatterns($patterns)
136
    {
137
        $projects = array();
138
139
        foreach ($patterns as $pattern) {
140
            $projects = array_merge($projects, $this->getProjectsFromPattern($pattern));
141
        }
142
143
        return $projects;
144
    }
145
146
    /**
147
     * @param string $pattern
148
     *
149
     * @return Project[]
150
     */
151
    public function getProjectsFromPattern($pattern)
152
    {
153
        $projects = array();
154
        $dispatcher = $this->getDispatcher();
155
        $files = glob($pattern);
156
157
        foreach ($files as $file) {
158
            foreach ($this->getParsers() as $parser) {
159
                if ($parser->supportedFile($file)) {
160
                    $event = new CerbereFileDiscoverEvent($this, $file, $parser);
161
                    $dispatcher->dispatch(CerbereEvents::APPLICATION_FILE_DISCOVERED, $event);
162
                    $parser->processFile($file);
163
                    $projects = array_merge($projects, $parser->getProjects());
164
                }
165
            }
166
        }
167
168
        return $projects;
169
    }
170
171
    /**
172
     * @return ParserInterface[]
173
     */
174
    public function getParsers()
175
    {
176
        return $this->parsers;
177
    }
178
}
179