Total Complexity | 8 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
11 | class Processor implements Store |
||
12 | { |
||
13 | /** @var Handler[] */ |
||
14 | private $handlers = []; |
||
15 | |||
16 | /** @var array */ |
||
17 | private $counts = []; |
||
18 | |||
19 | /** |
||
20 | * Add a Handler instance for a given XML node type. |
||
21 | */ |
||
22 | public function addHandler(string $type, Handler $handler): void |
||
23 | { |
||
24 | $this->handlers[$type] = $handler; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Process a XML node instance. |
||
29 | */ |
||
30 | public function push(\SimpleXMLElement $node): void |
||
31 | { |
||
32 | $type = $node->getName(); |
||
33 | |||
34 | if (isset($this->handlers[$type])) { |
||
35 | $this->handlers[$type]->handle($node); |
||
36 | |||
37 | if (!isset($this->counts[$type])) { |
||
38 | $this->counts[$type] = 0; |
||
39 | } |
||
40 | |||
41 | $this->counts[$type]++; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | public function store(): void |
||
46 | { |
||
47 | foreach ($this->handlers as $handler) { |
||
48 | if ($handler instanceof Store) { |
||
49 | $handler->store(); |
||
50 | } |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Retrieve counts of processed XML node instances, grouped by type. |
||
56 | */ |
||
57 | public function getCounts(): array |
||
60 | } |
||
61 | } |
||
62 |