Completed
Push — master ( 3592ed...983069 )
by Guillaume
02:39
created

IncidentPusher::push()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 40
Code Lines 22

Duplication

Lines 14
Ratio 35 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 40
rs 6.7272
cc 7
eloc 22
nc 20
nop 1
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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