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 GuzzleHttp\Client; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use SprykerEco\Zed\NewRelic\Business\Exception\RecordDeploymentException; |
13
|
|
|
|
14
|
|
|
class RecordDeployment implements RecordDeploymentInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
public const STATUS_CODE_SUCCESS = 200; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
public const STATUS_CODE_REDIRECTION = 300; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
* |
29
|
|
|
* @example https://api.eu.newrelic.com/v2/applications/12345/deployments.json |
30
|
|
|
* @example https://api.eu.newrelic.com/v2/applications/%s/deployments.json |
31
|
|
|
* |
32
|
|
|
* @see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/record-deployments |
33
|
|
|
*/ |
34
|
|
|
protected $newRelicDeploymentApiUrl; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $newRelicApiKey; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $newRelicApplicationIds; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $newRelicDeploymentApiUrl |
48
|
|
|
* @param string $newRelicApiKey |
49
|
|
|
* @param array $newRelicApplicationIds |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
string $newRelicDeploymentApiUrl, |
53
|
|
|
string $newRelicApiKey, |
54
|
|
|
array $newRelicApplicationIds = [] |
55
|
|
|
) { |
56
|
|
|
$this->newRelicDeploymentApiUrl = $newRelicDeploymentApiUrl; |
57
|
|
|
$this->newRelicApiKey = $newRelicApiKey; |
58
|
|
|
$this->newRelicApplicationIds = $newRelicApplicationIds; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $arguments |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function recordDeployment(array $arguments = []) |
67
|
|
|
{ |
68
|
|
|
if (!$this->newRelicApplicationIds) { |
|
|
|
|
69
|
|
|
return $this->recordSingleDeployment($arguments); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach ($this->newRelicApplicationIds as $singleApplicationId) { |
73
|
|
|
$arguments['application_id'] = $singleApplicationId; |
74
|
|
|
$this->recordSingleDeployment($arguments); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $params |
82
|
|
|
* |
83
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
84
|
|
|
*/ |
85
|
|
|
protected function createRecordDeploymentRequest(array $params): ResponseInterface |
86
|
|
|
{ |
87
|
|
|
$applicationId = $params['application_id'] ?? null; |
88
|
|
|
|
89
|
|
|
unset($params['app_name']); |
90
|
|
|
unset($params['application_id']); |
91
|
|
|
|
92
|
|
|
$options = [ |
93
|
|
|
'headers' => [ |
94
|
|
|
'X-Api-Key' => $this->newRelicApiKey, |
95
|
|
|
], |
96
|
|
|
'json' => [ |
97
|
|
|
'deployment' => $params, |
98
|
|
|
], |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
$httpClient = new Client(); |
102
|
|
|
|
103
|
|
|
$deploymentUrl = $this->newRelicDeploymentApiUrl; |
104
|
|
|
if ($applicationId) { |
105
|
|
|
$deploymentUrl = sprintf($this->newRelicDeploymentApiUrl, $applicationId); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$request = $httpClient->post($deploymentUrl, $options); |
109
|
|
|
|
110
|
|
|
return $request; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $arguments |
115
|
|
|
* |
116
|
|
|
* @throws \SprykerEco\Zed\NewRelic\Business\Exception\RecordDeploymentException |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
private function recordSingleDeployment(array $arguments = []) |
121
|
|
|
{ |
122
|
|
|
$response = $this->createRecordDeploymentRequest($arguments); |
123
|
|
|
$statusCode = $response->getStatusCode(); |
124
|
|
|
if ($statusCode < static::STATUS_CODE_SUCCESS || $statusCode >= static::STATUS_CODE_REDIRECTION) { |
125
|
|
|
throw new RecordDeploymentException(sprintf( |
126
|
|
|
'Record deployment to New Relic request failed with code %d. %s', |
127
|
|
|
$response->getStatusCode(), |
128
|
|
|
$response->getBody(), |
129
|
|
|
)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.