Completed
Push — master ( f78d72...c54b8c )
by Vladimir
05:53
created

Check   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 6
eloc 28
dl 0
loc 89
ccs 15
cts 25
cp 0.6
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 25 5
A __construct() 0 21 1
1
<?php
2
3
/**
4
 * This file is part of the `tvi/monitor-bundle` project.
5
 *
6
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
11
12
namespace Tvi\MonitorBundle\Check\rabbitmq\QueueSize;
13
14
use JMS\Serializer\Annotation as JMS;
15
use Tvi\MonitorBundle\Check\rabbitmq\RabbitMQClient;
16
use Tvi\MonitorBundle\Check\CheckAbstract;
17
use ZendDiagnostics\Result\Failure;
18
use ZendDiagnostics\Result\Success;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @JMS\ExclusionPolicy("all")
22
 *
23
 * @author Vladimir Turnaev <[email protected]>
24
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
25
class Check extends CheckAbstract
26
{
27
    /**
28
     * Count that will cause a warning.
29
     *
30
     * @var int
31
     */
32
    protected $warningThreshold;
33
34
    /**
35
     * Count that will cause a fail.
36
     *
37
     * @var int
38
     */
39
    protected $criticalThreshold;
40
41
    /**
42
     * check queue name.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
43
     *
44
     * @var string
45
     */
46
    protected $queue;
47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @var RabbitMQClient
49
     */
50
    private $client;
0 ignored issues
show
Coding Style introduced by
Private member variable "client" must be prefixed with an underscore
Loading history...
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
53
     * @param ?string $loading
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
     * @param ?int    $criticalThreshold
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
55
     * @param ?int    $warningThreshold
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
     * @param ?string $host
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
57
     * @param ?int    $port
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
58
     * @param ?string $user
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
59
     * @param ?string $password
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     * @param ?string $vhost
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     * @param ?string $dsn
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     */
63 2
    public function __construct(
64
        $loading,
65
        $criticalThreshold,
66
        $warningThreshold = null,
67
        $host = 'localhost',
68
        $port = 5672,
69
        $user = 'guest',
70
        $password = 'guest',
71
        $vhost = '/',
72
        $dsn = null)
0 ignored issues
show
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
73
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
74 2
        $this->loading = $loading;
0 ignored issues
show
Bug Best Practice introduced by
The property loading does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75 2
        $this->criticalThreshold = $criticalThreshold;
76 2
        $this->warningThreshold = $warningThreshold;
77
78 2
        $this->client = new RabbitMQClient($host,
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
79 2
            $port,
80 2
            $user,
81 2
            $password,
82 2
            $vhost,
83 2
            $dsn);
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
84 2
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
89 2
    public function check()
90
    {
91
        try {
92 2
            $conn = $this->client->getConnect();
93
            $conn->channel();
94
95
            $channel = $conn->channel();
96
97
            list($queue, $messageCount, $consumerCount) = $channel->queue_declare($this->queue, true);
98
99
            if ($messageCount >= $this->criticalThreshold) {
100
                $msg = sprintf('Message(s) %d in queue higher them critical level %d.', $messageCount, $this->criticalThreshold);
101
102
                return new Failure($msg, $messageCount);
103
            }
104
105
            if (null !== $this->warningThreshold && $messageCount >= $this->warningThreshold) {
106
                $msg = sprintf('Message(s) %d in queue higher them warning level %d.', $messageCount, $this->warningThreshold);
107
108
                return new Warning($msg, $messageCount);
0 ignored issues
show
Bug introduced by
The type Tvi\MonitorBundle\Check\rabbitmq\QueueSize\Warning 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...
109
            }
110
111
            return new Success(sprintf('Message(s) %d in queue.', $messageCount), $messageCount);
112 2
        } catch (\Exception $e) {
113 2
            return new Failure($e->getMessage());
114
        }
115
    }
116
}
117