PheanstalkDataCollector::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Pyrowman\PheanstalkBundle\DataCollector;
4
5
use Pheanstalk\Structure\Queue;
0 ignored issues
show
Bug introduced by
The type Pheanstalk\Structure\Queue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Pheanstalk\Structure\Tube;
7
use Pyrowman\PheanstalkBundle\PheanstalkLocator;
8
use Pheanstalk\Exception\ServerException;
9
use Pheanstalk\PheanstalkInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
13
14
/**
15
 * This is the data collector for PheanstalkBundle.
16
 *
17
 * @see    http://symfony.com/doc/current/cookbook/profiler/data_collector.html
18
 *
19
 * @author Maxime Aoustin <[email protected]>
20
 */
21
class PheanstalkDataCollector extends DataCollector
22
{
23
    /**
24
     * @var PheanstalkLocator
25
     */
26
    protected $pheanstalkLocator;
27
28
    /**
29
     * @param PheanstalkLocator $pheanstalkLocator
30
     */
31 2
    public function __construct(PheanstalkLocator $pheanstalkLocator)
32
    {
33 2
        $this->pheanstalkLocator = $pheanstalkLocator;
34 2
        $this->data              = [
35
            'pheanstalks' => [],
36
            'tubes'       => [],
37
            'jobCount'    => 0,
38
            'jobs'        => [],
39
        ];
40
    }
41
42 2
    public function reset()
43
    {
44 2
        $this->data = [
45
            'pheanstalks' => [],
46
            'tubes'       => [],
47
            'jobCount'    => 0,
48
            'jobs'        => [],
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 2
    public function collect(Request $request, Response $response, \Throwable $exception = null)
56
    {
57 2
        $this->reset();
58 2
        $defaultPheanstalk = $this->pheanstalkLocator->getDefaultPheanstalk();
59
60
        // Collect the information
61 2
        foreach ($this->pheanstalkLocator->getPheanstalks() as $name => $pheanstalk) {
62
            // Get information about this connection
63 2
            $this->data['pheanstalks'][$name] = [
64 2
                'name'      => $name,
65 2
                'host'      => $pheanstalk->getConnection()->getHost(),
66 2
                'port'      => $pheanstalk->getConnection()->getPort(),
67 2
                'timeout'   => $pheanstalk->getConnection()->getConnectTimeout(),
68 2
                'default'   => $defaultPheanstalk === $pheanstalk,
69
                'stats'     => [],
70 2
                'listening' => $pheanstalk->getConnection()->isServiceListening(),
71
            ];
72
73
            // If connection is not listening, there is a connection problem.
74
            // Skip next steps which require an established connection
75 2
            if (!$pheanstalk->getConnection()->isServiceListening()) {
76 1
                continue;
77
            }
78
79 1
            $pheanstalkStatistics = $pheanstalk->stats();
80
81
            // Get information about this connection
82 1
            $this->data['pheanstalks'][$name]['stats'] = $pheanstalkStatistics['statistics']['@attributes'];
83
84
85
            // Increment the number of jobs
86 1
            $this->data['jobCount'] += $pheanstalkStatistics['statistics']['@attributes']['workflow_queries'];
87
88
            // Get information about the tubes of this connection
89 1
            $tubes = $pheanstalk->listTubes();
90 1
            $this->fetchJobs($pheanstalk);
91
            /** @var Tube $tube */
92 1
            foreach ($tubes as $tube) {
93
                // Fetch next ready job and next buried job for this tube
94 1
                $stats = $pheanstalk->statsTube($tube);
95 1
                $this->data['tubes'][] = [
96 1
                    'pheanstalk' => $name,
97 1
                    'name'       => $tube->getName(),
98 1
                    'stats'      => $stats['queue']['@attributes'],
99
                ];
100
            }
101
        }
102
    }
103
104
    /**
105
     * @return array
106
     */
107 2
    public function getPheanstalks()
108
    {
109 2
        return $this->data['pheanstalks'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['pheanstalks'] also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type array.
Loading history...
110
    }
111
112
    /**
113
     * @return array
114
     */
115 2
    public function getTubes()
116
    {
117 2
        return $this->data['tubes'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['tubes'] also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type array.
Loading history...
118
    }
119
120
    /**
121
     * @return int
122
     */
123 1
    public function getJobCount()
124
    {
125 1
        return $this->data['jobCount'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['jobCount'] also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type integer.
Loading history...
126
    }
127
128
    /**
129
     * @return array
130
     */
131 2
    public function getJobs()
132
    {
133 2
        return $this->data['jobs'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['jobs'] also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type array.
Loading history...
134
    }
135
136
    /**
137
     * @return string
138
     */
139 1
    public function getName()
140
    {
141 1
        return 'pheanstalk';
142
    }
143
144
    /**
145
     * Get the next job ready and buried in the specified tube and connection.
146
     *
147
     * @param PheanstalkInterface $pheanstalk
148
     */
149 1
    private function fetchJobs(PheanstalkInterface $pheanstalk)
150
    {
151
        try {
152 1
            $nextJobReady = isset($pheanstalk->peek()[1]) ? $pheanstalk->peek()[1] : null;
153 1
            $this->data['jobs']['ready'] = [
154 1
                'id'   => isset($nextJobReady['id']) ? $nextJobReady['id'] : null,
155 1
                'data' => $nextJobReady,
156
            ];
157 1
        } catch (ServerException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
158
        }
159
    }
160
}
161