Test Failed
Push — main ( 9083af...30c6a0 )
by Bingo
05:18
created

DefaultIncidentHandler::createIncident()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Jabe\Engine\Impl\Incident;
4
5
use Jabe\Engine\Impl\Context\Context;
6
use Jabe\Engine\Impl\Persistence\Entity\IncidentEntity;
7
use Jabe\Engine\Runtime\IncidentInterface;
8
9
class DefaultIncidentHandler implements IncidentHandlerInterface
10
{
11
    protected $type;
12
13
    public function __construct(string $type)
14
    {
15
        $this->type = $type;
16
    }
17
18
    public function getIncidentHandlerType(): string
19
    {
20
        return $this->type;
21
    }
22
23
    public function handleIncident(IncidentContext $context, string $message): IncidentInterface
24
    {
25
        return $this->createIncident($context, $message);
26
    }
27
28
    public function createIncident(IncidentContext $context, string $message): IncidentInterface
29
    {
30
        $newIncident = IncidentEntity::createAndInsertIncident($this->type, $context, $message);
31
32
        if ($context->getExecutionId() !== null) {
0 ignored issues
show
introduced by
The condition $context->getExecutionId() !== null is always true.
Loading history...
33
            $newIncident->createRecursiveIncidents();
34
        }
35
36
        return $newIncident;
37
    }
38
39
    public function resolveIncident(IncidentContext $context): void
40
    {
41
        $this->removeIncident($context, true);
42
    }
43
44
    public function deleteIncident(IncidentContext $context): void
45
    {
46
        $this->removeIncident($context, false);
47
    }
48
49
    protected function removeIncident(IncidentContext $context, bool $incidentResolved): void
50
    {
51
        $incidents = Context::getCommandContext()
52
            ->getIncidentManager()
53
            ->findIncidentByConfiguration($context->getConfiguration());
54
55
        foreach ($incidents as $currentIncident) {
56
            $incident = $currentIncident;
57
            if ($incidentResolved) {
58
                $incident->resolve();
59
            } else {
60
                $incident->delete();
61
            }
62
        }
63
    }
64
}
65