Completed
Push — master ( 355f41...200862 )
by Mickael
06:25
created

GearmanParser::loadNamespaceMap()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 44
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 44
ccs 16
cts 16
cp 1
rs 8.439
cc 6
eloc 18
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * Gearman Bundle for Symfony2 / Symfony3
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * Feel free to edit as you please, and have fun.
10
 *
11
 * @author Marc Morera <[email protected]>
12
 */
13
14
namespace Mkk\GearmanBundle\Service;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Symfony\Component\Finder\Finder;
18
use Symfony\Component\HttpKernel\KernelInterface;
19
use Mkk\GearmanBundle\Driver\Gearman\Work as WorkAnnotation;
20
use Mkk\GearmanBundle\Module\WorkerClass as Worker;
21
use Mkk\GearmanBundle\Module\WorkerCollection;
22
23
/**
24
 * Gearman parsing methods
25
 *
26
 * This class has responability of parsing, if needed, all defined bundle files
27
 * looking for some Workers.
28
 */
29
class GearmanParser
30
{
31
32
    /**
33
     * @var array
34
     *
35
     * Bundles loaded by kernel
36
     */
37
    private $kernelBundles;
38
39
    /**
40
     * @var KernelInterface
41
     *
42
     * Kernel object
43
     */
44
    private $kernel;
45
46
    /**
47
     * @var Reader
48
     *
49
     * Annotation Reader
50
     */
51
    private $reader;
52
53
    /**
54
     * @var Finder
55
     *
56
     * Finder
57
     */
58
    private $finder;
59
60
    /**
61
     * @var array
62
     *
63
     * Bundles available to perform search
64
     */
65
    private $bundles;
66
67
    /**
68
     * @var array
69
     *
70
     * Collection of servers to connect
71
     */
72
    private $servers;
73
74
    /**
75
     * @var array
76
     *
77
     * Default settings defined by user in config.yml
78
     */
79
    private $defaultSettings;
80
81
    /**
82
     * Construct method
83
     *
84
     * @param KernelInterface $kernel          Kernel instance
85
     * @param Reader          $reader          Reader
86
     * @param Finder          $finder          Finder
87
     * @param array           $bundles         Bundle array where to parse workers, defined on condiguration
88
     * @param array           $servers         Server list defined on configuration
89
     * @param array           $defaultSettings Default settings defined on configuration
90
     */
91 5
    public function __construct(
92
        KernelInterface $kernel,
93
        Reader $reader,
94
        Finder $finder,
95
        array $bundles,
96
        array $servers,
97
        array $defaultSettings
98
    )
99
    {
100 5
        $this->kernelBundles = $kernel->getBundles();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
101 5
        $this->kernel = $kernel;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
102 5
        $this->reader = $reader;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
103 5
        $this->finder = $finder;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
104 5
        $this->bundles = $bundles;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
105 5
        $this->servers = $servers;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
106 5
        $this->defaultSettings = $defaultSettings;
107 5
    }
108
109
    /**
110
     * Loads Worker Collection from parsed files
111
     *
112
     * @return WorkerCollection collection of all info
113
     */
114 1
    public function load()
115
    {
116 1
        list($paths, $excludedPaths) = $this->loadNamespaceMap($this->kernelBundles, $this->bundles);
117
118 1
        return $this->parseNamespaceMap($this->finder, $this->reader, $paths, $excludedPaths);
119
    }
120
121
    /**
122
     * Return Gearman bundle settings, previously loaded by method load()
123
     *
124
     * If settings are not loaded, a SettingsNotLoadedException Exception is thrown
125
     *
126
     * @param array $kernelBundles Kernel bundles
127
     * @param array $bundles       Bundle array of settings
128
     *
129
     * @return array Return an array containing paths and ignore paths
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
130
     */
131 6
    public function loadNamespaceMap(array $kernelBundles, array $bundles)
132
    {
133 6
        $paths = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
134 6
        $excludedPaths = array();
135
136
        /**
137
         * Iteratinc all bundle settings
138
         */
139 6
        foreach ($bundles as $bundleSettings) {
140
141 5
            if (!$bundleSettings['active']) {
142
143 1
                break;
144
            }
145
146 4
            $bundleNamespace = $bundleSettings['name'];
147 4
            $bundlePath = $kernelBundles[$bundleNamespace]->getPath();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
148
149 4
            if (!empty($bundleSettings['include'])) {
150
151 2
                foreach ($bundleSettings['include'] as $include) {
152
153 2
                    $paths[] = rtrim(rtrim($bundlePath, '/') . '/' . $include, '/') . '/';
154
                }
155
156
            } else {
157
158
                /**
159
                 * If no include is set, include all namespace
160
                 */
161 2
                $paths[] = rtrim($bundlePath, '/') . '/';
162
            }
163
164 4
            foreach ($bundleSettings['ignore'] as $ignore) {
165
166 4
                $excludedPaths[] = trim($ignore, '/');
167
            }
168
        }
169
170
        return array(
171 6
            $paths,
172 6
            $excludedPaths,
173
        );
174
    }
175
176
    /**
177
     * Perform a parsing inside all namespace map
178
     *
179
     * Creates an empty worker collection and, if exist some parseable files
180
     * parse them, filling this object
181
     *
182
     * @param Finder $finder        Finder
183
     * @param Reader $reader        Reader
184
     * @param array  $paths         Paths where to look for
185
     * @param array  $excludedPaths Paths to ignore
186
     *
187
     * @return WorkerCollection collection of all info
188
     */
189 3
    public function parseNamespaceMap(
190
        Finder $finder,
191
        Reader $reader,
192
        array $paths,
193
        array $excludedPaths
194
    )
195
    {
196 3
        $workerCollection = new WorkerCollection;
197
198 3
        if (!empty($paths)) {
199
200
            $finder
201 1
                ->files()
202 1
                ->followLinks()
203 1
                ->exclude($excludedPaths)
204 1
                ->in($paths)
205 1
                ->name('*.php');
206
207 1
            $this->parseFiles($finder, $reader, $workerCollection);
208
        }
209
210 3
        return $workerCollection;
211
    }
212
213
    /**
214
     * Load all workers with their jobs
215
     *
216
     * @param Finder           $finder           Finder
217
     * @param Reader           $reader           Reader
218
     * @param WorkerCollection $workerCollection Worker collection
219
     *
220
     * @return GearmanParser self Object
221
     */
222
    public function parseFiles(
223
        Finder $finder,
224
        Reader $reader,
225
        WorkerCollection $workerCollection
226
    )
227
    {
228
229
        /**
230
         * Every file found is parsed
231
         */
232
        foreach ($finder as $file) {
233
234
            /**
235
             * File is accepted to be parsed
236
             */
237
            $classNamespace = $this->getFileClassNamespace($file->getRealpath());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
238
            $reflectionClass = new \ReflectionClass($classNamespace);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
239
            $classAnnotations = $reader->getClassAnnotations($reflectionClass);
240
241
            /**
242
             * Every annotation found is parsed
243
             */
244
            foreach ($classAnnotations as $annotation) {
245
246
                /**
247
                 * Annotation is only laoded if is typeof WorkAnnotation
248
                 */
249
                if ($annotation instanceof WorkAnnotation) {
250
251
                    /**
252
                     * Creates new Worker element with all its Job data
253
                     */
254
                    $worker = new Worker($annotation, $reflectionClass, $reader, $this->servers, $this->defaultSettings);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
255
                    $workerCollection->add($worker);
256
                }
257
            }
258
        }
259
260
        return $this;
261
    }
262
263
    /**
264
     * Returns file class namespace, if exists
265
     *
266
     * @param string $file A PHP file path
267
     *
268
     * @return string|false Full class namespace if found, false otherwise
269
     */
270 2
    public function getFileClassNamespace($file)
271
    {
272 2
        $filenameBlock = explode(DIRECTORY_SEPARATOR, $file);
273 2
        $filename = explode('.', end($filenameBlock), 2);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
274 2
        $filename = reset($filename);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
275
276 2
        preg_match('/\snamespace\s+(.+?);/s', file_get_contents($file), $match);
277
278 2
        return    is_array($match) && isset($match[1])
279 2
                ? $match[1] . '\\' . $filename
280 2
                : false;
281
    }
282
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
283