Completed
Pull Request — master (#9)
by Nicolas
03:08
created

Pimple::computeWorkerServiceKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 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