Passed
Push — master ( 7f6a2e...f50264 )
by Vladimir
07:00
created

GuzzleHttpService::performGuzzleRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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\http\GuzzleHttpService;
13
14
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
15
use GuzzleHttp\Message\Request as GuzzleRequest;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Message\Request 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...
16
use GuzzleHttp\Message\RequestInterface as GuzzleRequestInterface;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Message\RequestInterface 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...
17
use InvalidArgumentException;
18
use Psr\Http\Message\RequestInterface as PsrRequestInterface;
19
use ZendDiagnostics\Result\Failure;
20
use ZendDiagnostics\Result\Success;
21
22
class GuzzleHttpService extends \ZendDiagnostics\Check\GuzzleHttpService
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class GuzzleHttpService
Loading history...
23
{
24
    protected $setData = false;
25
26
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
     * @param string|PsrRequestInterface|GuzzleRequestInterface $requestOrUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
28
     *                                                                        The absolute url to check, or a
29
     *                                                                        fully-formed request instance
30
     * @param array                                             $headers      An array of headers used to create the
31
     *                                                                        request
32
     * @param array                                             $options      An array of guzzle options to use when
33
     *                                                                        sending the request
34
     * @param int                                               $statusCode   The response status code to check
35
     * @param null                                              $content      The response content to check
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $content is correct as it would always require null to be passed?
Loading history...
36
     * @param null|GuzzleClientInterface                        $guzzle       Instance of guzzle to use
37
     * @param string                                            $method       The method of the request
38
     * @param mixed                                             $body         The body of the request (used for POST, PUT
39
     *                                                                        and DELETE requests)
40
     * @param bool                                              $setData      set data to result
41
     *
42
     * @throws InvalidArgumentException
43
     */
44 2
    public function __construct(
45
        $requestOrUrl,
46
        array $headers = [],
47
        array $options = [],
48
        $statusCode = 200,
49
        $content = null,
50
        $guzzle = null,
51
        $method = 'GET',
52
        $body = null,
53
        $setData = false)
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...
54
    {
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...
55 2
        parent::__construct($requestOrUrl, $headers, $options, $statusCode, $content, $guzzle, $method, $body);
56
57 2
        $this->setData = $setData;
58 2
    }
59
60
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
61
     * @see ZendDiagnostics\CheckInterface::check()
62
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
63 2
    public function check()
64
    {
65
        // GuzzleHttp\Message\RequestInterface only exists in v4 and v5.
66 2
        return class_exists(GuzzleRequest::class)
67
            ? $this->performLegacyGuzzleRequest()
68 2
            : $this->performGuzzleRequest();
69
    }
70
71
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
72
     * @throws \GuzzleHttp\Exception\GuzzleException
73
     *
74
     * @return \ZendDiagnostics\Result\ResultInterface
75
     */
76 2
    protected function performGuzzleRequest()
77
    {
78 2
        $response = $this->guzzle->send($this->request, array_merge([
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
            'exceptions' => false,
80 2
        ], $this->options));
0 ignored issues
show
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...
81
82 2
        return $this->analyzeResponse($response);
83
    }
84
85
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
86
     * @param \GuzzleHttp\Message\ResponseInterface|Psr\Http\Message\ResponseInterface $response
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Message\ResponseInterface 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...
Bug introduced by
The type Tvi\MonitorBundle\Check\...ssage\ResponseInterface was not found. Did you mean Psr\Http\Message\ResponseInterface? If so, make sure to prefix the type with \.
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
87
     *
88
     * @return \ZendDiagnostics\Result\ResultInterface
89
     */
90 2
    protected function analyzeResponse($response)
91
    {
92 2
        $result = $this->analyzeStatusCode((int) $response->getStatusCode());
93 2
        if ($result instanceof Failure) {
94
            return $result;
95
        }
96
97 2
        $result = $this->analyzeResponseContent((string) $response->getBody());
98 2
        if ($result instanceof Failure) {
99
            return $result;
100
        }
101
102 2
        $data = null;
103 2
        if ($this->setData) {
104
            $data = (string) $response->getBody();
105
        }
106
107 2
        return new Success(null, $data);
108
    }
109
110
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
111
     * @param int $statusCode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
112
     *
113
     * @return bool|FailureInterface Returns boolean true when successful, and
0 ignored issues
show
Bug introduced by
The type Tvi\MonitorBundle\Check\...ervice\FailureInterface 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...
114
     *                               a FailureInterface instance otherwise
115
     */
116 2
    protected function analyzeStatusCode($statusCode)
117
    {
118 2
        return $this->statusCode === $statusCode
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->statusCode...Code, $this->getUri())) also could return the type ZendDiagnostics\Result\Failure which is incompatible with the documented return type Tvi\MonitorBundle\Check\...ailureInterface|boolean.
Loading history...
119 2
            ? true
120
            : new Failure(sprintf(
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...
121
                'Status code %s does not match %s in response from %s',
122
                $this->statusCode,
123
                $statusCode,
124 2
                $this->getUri()
125
            ));
0 ignored issues
show
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...
126
    }
127
128
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
129
     * @param string $content
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
130
     *
131
     * @return bool|FailureInterface Returns boolean true when successful, and
132
     *                               a FailureInterface instance otherwise
133
     */
134 2
    protected function analyzeResponseContent($content)
135
    {
136 2
        return !$this->content || false !== mb_strpos($content, $this->content)
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! $this->content ...tent, $this->getUri())) also could return the type ZendDiagnostics\Result\Failure which is incompatible with the documented return type Tvi\MonitorBundle\Check\...ailureInterface|boolean.
Loading history...
137 2
            ? true
138
            : new Failure(sprintf(
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...
139
                'Content %s not found in response from %s',
140
                $this->content,
141 2
                $this->getUri()
142
            ));
0 ignored issues
show
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...
143
    }
144
145
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
146
     * @return string
147
     */
148
    protected function getUri()
149
    {
150
        return $this->request instanceof PsrRequestInterface
151
            ? (string) $this->request->getUri() // guzzle 6
152
            : $this->request->getUrl();         // guzzle 4 and 5
153
    }
154
155
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
156
     * @throws \GuzzleHttp\Exception\GuzzleException
157
     *
158
     * @return \ZendDiagnostics\Result\ResultInterface
159
     */
160
    private function performLegacyGuzzleRequest()
0 ignored issues
show
Coding Style introduced by
Private method name "GuzzleHttpService::performLegacyGuzzleRequest" must be prefixed with an underscore
Loading history...
161
    {
162
        $response = $this->guzzle->send($this->request);
163
164
        return $this->analyzeResponse($response);
165
    }
166
}
167