Completed
Push — master ( 200862...c6a168 )
by Mickael
05:18
created

GearmanParser   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 246
ccs 45
cts 55
cp 0.8182
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A load() 0 6 1
B loadNamespaceMap() 0 44 6
A parseNamespaceMap() 0 23 2
B parseFiles() 0 40 4
A getFileClassNamespace() 0 12 3
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 Reader
41
     *
42
     * Annotation Reader
43
     */
44
    private $reader;
45
46
    /**
47
     * @var Finder
48
     *
49
     * Finder
50
     */
51
    private $finder;
52
53
    /**
54
     * @var array
55
     *
56
     * Bundles available to perform search
57
     */
58
    private $bundles;
59
60
    /**
61
     * @var array
62
     *
63
     * Collection of servers to connect
64
     */
65
    private $servers;
66
67
    /**
68
     * @var array
69
     *
70
     * Default settings defined by user in config.yml
71
     */
72
    private $defaultSettings;
73
74
    /**
75
     * Construct method
76
     *
77
     * @param KernelInterface $kernel          Kernel instance
78
     * @param Reader          $reader          Reader
79
     * @param Finder          $finder          Finder
80
     * @param array           $bundles         Bundle array where to parse workers, defined on condiguration
81
     * @param array           $servers         Server list defined on configuration
82
     * @param array           $defaultSettings Default settings defined on configuration
83
     */
84 5
    public function __construct(
85
        KernelInterface $kernel,
86
        Reader $reader,
87
        Finder $finder,
88
        array $bundles,
89
        array $servers,
90
        array $defaultSettings
91
    )
92
    {
93 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...
94 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...
95 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...
96 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...
97 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...
98 5
        $this->defaultSettings = $defaultSettings;
99 5
    }
100
101
    /**
102
     * Loads Worker Collection from parsed files
103
     *
104
     * @return WorkerCollection collection of all info
105
     */
106 1
    public function load()
107
    {
108 1
        list($paths, $excludedPaths) = $this->loadNamespaceMap($this->kernelBundles, $this->bundles);
109
110 1
        return $this->parseNamespaceMap($this->finder, $this->reader, $paths, $excludedPaths);
111
    }
112
113
    /**
114
     * Return Gearman bundle settings, previously loaded by method load()
115
     *
116
     * If settings are not loaded, a SettingsNotLoadedException Exception is thrown
117
     *
118
     * @param array $kernelBundles Kernel bundles
119
     * @param array $bundles       Bundle array of settings
120
     *
121
     * @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...
122
     */
123 6
    public function loadNamespaceMap(array $kernelBundles, array $bundles)
124
    {
125 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...
126 6
        $excludedPaths = array();
127
128
        /**
129
         * Iteratinc all bundle settings
130
         */
131 6
        foreach ($bundles as $bundleSettings) {
132
133 5
            if (!$bundleSettings['active']) {
134
135 1
                break;
136
            }
137
138 4
            $bundleNamespace = $bundleSettings['name'];
139 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...
140
141 4
            if (!empty($bundleSettings['include'])) {
142
143 2
                foreach ($bundleSettings['include'] as $include) {
144
145 2
                    $paths[] = rtrim(rtrim($bundlePath, '/') . '/' . $include, '/') . '/';
146
                }
147
148
            } else {
149
150
                /**
151
                 * If no include is set, include all namespace
152
                 */
153 2
                $paths[] = rtrim($bundlePath, '/') . '/';
154
            }
155
156 4
            foreach ($bundleSettings['ignore'] as $ignore) {
157
158 4
                $excludedPaths[] = trim($ignore, '/');
159
            }
160
        }
161
162
        return array(
163 6
            $paths,
164 6
            $excludedPaths,
165
        );
166
    }
167
168
    /**
169
     * Perform a parsing inside all namespace map
170
     *
171
     * Creates an empty worker collection and, if exist some parseable files
172
     * parse them, filling this object
173
     *
174
     * @param Finder $finder        Finder
175
     * @param Reader $reader        Reader
176
     * @param array  $paths         Paths where to look for
177
     * @param array  $excludedPaths Paths to ignore
178
     *
179
     * @return WorkerCollection collection of all info
180
     */
181 3
    public function parseNamespaceMap(
182
        Finder $finder,
183
        Reader $reader,
184
        array $paths,
185
        array $excludedPaths
186
    )
187
    {
188 3
        $workerCollection = new WorkerCollection;
189
190 3
        if (!empty($paths)) {
191
192
            $finder
193 1
                ->files()
194 1
                ->followLinks()
195 1
                ->exclude($excludedPaths)
196 1
                ->in($paths)
197 1
                ->name('*.php');
198
199 1
            $this->parseFiles($finder, $reader, $workerCollection);
200
        }
201
202 3
        return $workerCollection;
203
    }
204
205
    /**
206
     * Load all workers with their jobs
207
     *
208
     * @param Finder           $finder           Finder
209
     * @param Reader           $reader           Reader
210
     * @param WorkerCollection $workerCollection Worker collection
211
     *
212
     * @return GearmanParser self Object
213
     */
214
    public function parseFiles(
215
        Finder $finder,
216
        Reader $reader,
217
        WorkerCollection $workerCollection
218
    )
219
    {
220
221
        /**
222
         * Every file found is parsed
223
         */
224
        foreach ($finder as $file) {
225
226
            /**
227
             * File is accepted to be parsed
228
             */
229
            $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...
230
            $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...
231
            $classAnnotations = $reader->getClassAnnotations($reflectionClass);
232
233
            /**
234
             * Every annotation found is parsed
235
             */
236
            foreach ($classAnnotations as $annotation) {
237
238
                /**
239
                 * Annotation is only laoded if is typeof WorkAnnotation
240
                 */
241
                if ($annotation instanceof WorkAnnotation) {
242
243
                    /**
244
                     * Creates new Worker element with all its Job data
245
                     */
246
                    $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...
247
                    $workerCollection->add($worker);
248
                }
249
            }
250
        }
251
252
        return $this;
253
    }
254
255
    /**
256
     * Returns file class namespace, if exists
257
     *
258
     * @param string $file A PHP file path
259
     *
260
     * @return string|false Full class namespace if found, false otherwise
261
     */
262 2
    public function getFileClassNamespace($file)
263
    {
264 2
        $filenameBlock = explode(DIRECTORY_SEPARATOR, $file);
265 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...
266 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...
267
268 2
        preg_match('/\snamespace\s+(.+?);/s', file_get_contents($file), $match);
269
270 2
        return    is_array($match) && isset($match[1])
271 2
                ? $match[1] . '\\' . $filename
272 2
                : false;
273
    }
274
}
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...
275