Passed
Pull Request — master (#2)
by Serhii
05:12 queued 02:33
created

RecordDeployment::createRecordDeploymentRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
c 0
b 0
f 0
rs 9.9
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\NewRelic\Business\Model;
9
10
use Guzzle\Http\Client;
0 ignored issues
show
Bug introduced by
The type Guzzle\Http\Client 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...
11
use Psr\Http\Message\ResponseInterface;
12
use SprykerEco\Zed\NewRelic\Business\Exception\RecordDeploymentException;
13
14
class RecordDeployment implements RecordDeploymentInterface
15
{
16
    const NEW_RELIC_DEPLOYMENT_API_URL = 'https://api.newrelic.com/deployments.xml';
17
    const STATUS_CODE_SUCCESS = 200;
18
    const STATUS_CODE_REDIRECTION = 300;
19
20
    /**
21
     * @var string
22
     */
23
    protected $newRelicDeploymentApiUrl;
24
25
    /**
26
     * @var string
27
     */
28
    protected $newRelicApiKey;
29
30
    /**
31
     * @param string $newRelicDeploymentApiUrl
32
     * @param string $newRelicApiKey
33
     */
34
    public function __construct(string $newRelicDeploymentApiUrl, string $newRelicApiKey)
35
    {
36
        $this->newRelicDeploymentApiUrl = $newRelicDeploymentApiUrl;
37
        $this->newRelicApiKey = $newRelicApiKey;
38
    }
39
40
    /**
41
     * @param array $arguments
42
     *
43
     * @throws \SprykerEco\Zed\NewRelic\Business\Exception\RecordDeploymentException
44
     *
45
     * @return $this
46
     */
47
    public function recordDeployment(array $arguments = []): RecordDeploymentInterface
48
    {
49
        $response = $this->createRecordDeploymentRequest($arguments);
50
        $statusCode = $response->getStatusCode();
51
        if ($statusCode < static::STATUS_CODE_SUCCESS || $statusCode >= static::STATUS_CODE_REDIRECTION) {
52
            throw new RecordDeploymentException(sprintf(
53
                'Record deployment to New Relic request failed with code %d. %s',
54
                $response->getStatusCode(),
55
                $response->getBody()
56
            ));
57
        }
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param array $params
64
     *
65
     * @return \Psr\Http\Message\ResponseInterface
66
     */
67
    protected function createRecordDeploymentRequest(array $params): ResponseInterface
68
    {
69
        $options = [
70
            'headers' => [
71
                'x-api-key' => $this->newRelicApiKey,
72
            ],
73
        ];
74
75
        $data = [];
76
        $data['deployment'] = [];
77
        foreach ($params as $key => $value) {
78
            $data['deployment'][$key] = $value;
79
        }
80
        $options['form_params'] = $data;
81
82
        $httpClient = new Client();
83
84
        $request = $httpClient->post($this->newRelicDeploymentApiUrl, $options);
85
86
        return $request;
87
    }
88
}
89