IncidentPusher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 47
rs 10
wmc 7
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
C push() 0 41 7
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 GuzzleHttp\Exception\BadResponseException;
19
use Hogosha\Monitor\Guesser\StatusGuesser;
20
use Hogosha\Monitor\Model\Result;
21
use Hogosha\Sdk\Resource\Factory\ResourceFactory;
22
23
/**
24
 * Class IncidentPusher.
25
 */
26
class IncidentPusher extends AbstractPusher
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function push(Result $result)
32
    {
33
        $statusGuesser = new StatusGuesser();
34
35
        $resourceFactory = new ResourceFactory($this->client);
36
        $incidentResource = $resourceFactory->get('incident');
37
38
        $serviceResource = $resourceFactory->get('service');
39
        $service = $serviceResource->getService($result->getUrl()->getServiceUuid());
40
41
        $alreadyInFailure = false;
42
43
        if ($service['status'] != StatusGuesser::RESOLVED) {
44
            $alreadyInFailure = true;
45
        }
46
47
        try {
48
            // If the service is not already in failure,
49
            // we check if there is a failure.
50
            if (false === $alreadyInFailure && $statusGuesser->isFailed($result)) {
51
                $incidentResource->createIncident([
52
                    'name' => str_replace('%service_name%', $service['name'], $this->getDefaultFailedIncidentMessage()),
53
                    'service' => $result->getUrl()->getServiceUuid(),
54
                    'status' => StatusGuesser::IDENTIFIED,
55
                    'error_log' => sprintf('%s %s', $result->getHandlerError(), $result->getValidatorError()),
56
                ]);
57
            }
58
59
            // If the service is already in failture,
60
            // we check if it's back to normal and create a incident resolver
61
            if ($alreadyInFailure && $statusGuesser->isOk($result)) {
62
                $incidentResource->createIncident([
63
                    'name' => str_replace('%service_name%', $service['name'], $this->getDefaultResolvedIncidentMessage()),
64
                    'service' => $result->getUrl()->getServiceUuid(),
65
                    'status' => StatusGuesser::RESOLVED,
66
                ]);
67
            }
68
        } catch (BadResponseException $e) {
69
            throw $e;
70
        }
71
    }
72
}
73