Passed
Pull Request — master (#9)
by Nicolas
03:16
created

Pimple   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 81
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getWorker() 0 12 2
A computeWorkerServiceKey() 0 7 1
A listAll() 0 6 1
A listWithRegexFilter() 0 8 1
A listWorkers() 0 20 3
A extractWorkers() 0 7 1
A formatWorkerName() 0 4 1
1
<?php
2
3
namespace Puzzle\AMQP\Workers\Providers;
4
5
use Puzzle\AMQP\Workers\WorkerProvider;
6
use Puzzle\AMQP\Workers\WorkerContext;
7
8
class Pimple implements WorkerProvider
9
{
10
    private
11
        $container;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $container.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
12
13 3
    public function __construct(\Pimple\Container $container)
14
    {
15 3
        $this->container = $container;
16 3
    }
17
18 1
    public function getWorker($workerName)
19
    {
20 1
        $workerContext = null;
21 1
        $key = $this->computeWorkerServiceKey($workerName);
22
23 1
        if(isset($this->container[$key]))
24 1
        {
25 1
            $workerContext = $this->container[$key];
26 1
        }
27
28 1
        return $workerContext;
29
    }
30
31 1
    private function computeWorkerServiceKey($workerName)
32
    {
33 1
        return sprintf(
34 1
            'worker.%s',
35
            $workerName
36 1
        );
37
    }
38
39 1
    public function listAll()
40
    {
41 1
        $workers = $this->extractWorkers();
42
43 1
        return $this->listWorkers($workers);
44
    }
45
46 1
    public function listWithRegexFilter($workerNamePattern)
47
    {
48 1
        $workers = $this->extractWorkers();
49 1
        $workers = new \RegexIterator(new \ArrayIterator($workers), sprintf('~^worker\.%s~', $workerNamePattern));
50 1
        $workers = iterator_to_array($workers);
51
52 1
        return $this->listWorkers($workers);
53
    }
54
55 2
    private function listWorkers(array $extractedWorkers)
56
    {
57 2
        $workers = array();
58
59 2
        foreach($extractedWorkers as $worker)
60
        {
61 2
            $key = $this->formatWorkerName($worker);
62 2
            $worker = $this->container[$worker];
63
64 2
            if($worker instanceof WorkerContext)
65 2
            {
66 2
                $workers[$key] = [
67 2
                    'queue' => $worker->getQueue(),
68 2
                    'description' => $worker->getDescription(),
69
                ];
70 2
            }
71 2
        }
72
73 2
        return $workers;
74
    }
75
76 2
    private function extractWorkers()
77
    {
78 2
        $services = new \ArrayIterator($this->container->keys());
79 2
        $services = new \RegexIterator($services, '~^worker\..+~');
80
81 2
        return iterator_to_array($services);
82
    }
83
84 2
    private function formatWorkerName($workerServiceName)
85
    {
86 2
        return str_replace('worker.', '', $workerServiceName);
87
    }
88
}
89