1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the hogosha-monitor package |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Feel free to edit as you please, and have fun. |
12
|
|
|
* |
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Hogosha\Monitor\Pusher; |
17
|
|
|
|
18
|
|
|
use Hogosha\Monitor\Guesser\StatusGuesser; |
19
|
|
|
use Hogosha\Monitor\Model\Result; |
20
|
|
|
use Hogosha\Sdk\Resource\Factory\ResourceFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class IncidentPusher. |
24
|
|
|
*/ |
25
|
|
|
class IncidentPusher extends AbstractPusher |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function push(Result $result) |
31
|
|
|
{ |
32
|
|
|
$statusGuesser = new StatusGuesser(); |
33
|
|
|
|
34
|
|
|
$resourceFactory = new ResourceFactory($this->getClient()); |
35
|
|
|
$incidentResource = $resourceFactory->get('incident'); |
36
|
|
|
|
37
|
|
|
$serviceResource = $resourceFactory->get('service'); |
38
|
|
|
$service = $serviceResource->getService($result->getUrl()->getServiceUuid()); |
39
|
|
|
|
40
|
|
|
$alreadyInFailure = false; |
41
|
|
|
|
42
|
|
|
if ($service['status'] != StatusGuesser::RESOLVED) { |
43
|
|
|
$alreadyInFailure = true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
// If the service is not already in failure, |
48
|
|
|
// we check if there is a failure. |
49
|
|
View Code Duplication |
if (false === $alreadyInFailure && $statusGuesser->isFailed($result)) { |
|
|
|
|
50
|
|
|
$incidentResource->createIncident([ |
51
|
|
|
'name' => str_replace('%service_name%', $service['name'], $this->getDefaultFailedIncidentMessage()), |
52
|
|
|
'service' => $result->getUrl()->getServiceUuid(), |
53
|
|
|
'status' => StatusGuesser::IDENTIFIED, |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// If the service is already in failture, |
58
|
|
|
// we check if it's back to normal and create a incident resolver |
59
|
|
View Code Duplication |
if ($alreadyInFailure && $statusGuesser->isOk($result)) { |
|
|
|
|
60
|
|
|
$incidentResource->createIncident([ |
61
|
|
|
'name' => str_replace('%service_name%', $service['name'], $this->getDefaultResolvedIncidentMessage()), |
62
|
|
|
'service' => $result->getUrl()->getServiceUuid(), |
63
|
|
|
'status' => StatusGuesser::RESOLVED, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
} catch (\Exception $e) { |
67
|
|
|
echo sprintf('An error has occured %s', $e->getMessage()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.