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

CompositeIncidentHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Jabe\Engine\Impl\Incident;
4
5
use Jabe\Engine\ProcessEngineException;
6
use Jabe\Engine\Impl\Util\EnsureUtil;
7
use Jabe\Engine\Runtime\IncidentInterface;
8
9
class CompositeIncidentHandler implements IncidentHandlerInterface
10
{
11
    protected $mainIncidentHandler;
12
    protected $incidentHandlers = [];
13
14
    /**
15
     * Constructor that takes a varargs parameter {@link IncidentHandler} that
16
     * consume the incident.
17
     *
18
     * @param mainIncidentHandler the main incident handler {@link IncidentHandler} that consume the incident and return result.
19
     * @param incidentHandlers    the list of {@link IncidentHandler} that consume the incident.
20
     */
21
    public function __construct(IncidentHandlerInterface $mainIncidentHandler, array $incidentHandlers)
22
    {
23
        EnsureUtil::ensureNotNull("Incident handlers", "incidentHandlers", $incidentHandlers);
24
        $this->initializeIncidentsHandlers($mainIncidentHandler, $incidentHandlers);
25
    }
26
27
    /**
28
     * Initialize {@link #incidentHandlers} with data transfered from constructor
29
     *
30
     * @param incidentHandlers
31
     */
32
    protected function initializeIncidentsHandlers(IncidentHandlerInterface $mainIncidentHandler, array $incidentHandlers): void
33
    {
34
        EnsureUtil::ensureNotNull("Incident handler", "mainIncidentHandler", $mainIncidentHandler);
35
        $this->mainIncidentHandler = $mainIncidentHandler;
36
        EnsureUtil::ensureNotNull("Incident handlers", "incidentHandlers", $incidentHandlers);
37
        foreach ($incidentHandlers as $incidentHandler) {
38
            $this->add($incidentHandler);
39
        }
40
    }
41
42
    /**
43
     * Adds the {@link IncidentHandler} to the list of
44
     * {@link IncidentHandler} that consume the incident.
45
     *
46
     * @param incidentHandler the {@link IncidentHandler} that consume the incident.
47
     */
48
    public function add(IncidentHandlerInterface $incidentHandler): void
49
    {
50
        EnsureUtil::ensureNotNull("Incident handler", "incidentHandler", $incidentHandler);
51
        $incidentHandlerType = $this->getIncidentHandlerType();
52
        if ($incidentHandlerType != $incidentHandler->getIncidentHandlerType()) {
53
            throw new ProcessEngineException(
54
                "Incorrect incident type handler in composite handler with type: " . $incidentHandlerType
55
            );
56
        }
57
        $this->incidentHandlers->add($incidentHandler);
58
    }
59
60
    public function getIncidentHandlerType(): string
61
    {
62
        return $this->mainIncidentHandler->getIncidentHandlerType();
63
    }
64
65
    public function handleIncident(IncidentContext $context, string $message): IncidentInterface
66
    {
67
        $incident = $this->mainIncidentHandler->handleIncident($context, $message);
68
        foreach ($this->incidentHandlers as $incidentHandler) {
69
            $incidentHandler->handleIncident($context, $message);
70
        }
71
        return $incident;
72
    }
73
74
    public function resolveIncident(IncidentContext $context): void
75
    {
76
        $this->mainIncidentHandler->resolveIncident($context);
77
        foreach ($this->incidentHandlers as $incidentHandler) {
78
            $incidentHandler->resolveIncident($context);
79
        }
80
    }
81
82
    public function deleteIncident(IncidentContext $context): void
83
    {
84
        $this->mainIncidentHandler->deleteIncident($context);
85
        foreach ($this->incidentHandlers as $incidentHandler) {
86
            $incidentHandler->deleteIncident($context);
87
        }
88
    }
89
}
90