Processor::push()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Rarst\Hugo\wprss2hugo;
5
6
use Rarst\Hugo\wprss2hugo\Handler\Handler;
7
8
/**
9
 * Main process class, what routes XML nodes to matching handlers.
10
 */
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
58
    {
59
        return array_filter($this->counts);
60
    }
61
}
62