Fragments   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 59
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B inLoop() 0 40 8
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Manipulator\Plugin;
12
13
use WebinoDraw\Dom\Document;
14
use WebinoDraw\Dom\Element;
15
use WebinoDraw\Dom\Locator;
16
use WebinoDraw\Exception;
17
18
/**
19
 * Class Fragments
20
 */
21
class Fragments implements InLoopPluginInterface
22
{
23
    /**
24
     * @var Locator
25
     */
26
    protected $locator;
27
28
    /**
29
     * @param Locator $locator
30
     */
31
    public function __construct(Locator $locator)
32
    {
33
        $this->locator = $locator;
34
    }
35
36
    /**
37
     * @param PluginArgument $arg
38
     */
39
    public function inLoop(PluginArgument $arg)
40
    {
41
        $spec = $arg->getSpec();
42
        if (empty($spec['fragments'])) {
43
            return;
44
        }
45
46
        $node = $arg->getNode();
47
        if (!($node instanceof Element)) {
48
            throw new Exception\LogicException('Expected node of type Dom\Element');
49
        }
50
        if (!($node->ownerDocument instanceof Document)) {
51
            throw new Exception\LogicException('Expects node ownerDocument of type Dom\Document');
52
        }
53
54
        $nodeXpath   = $node->ownerDocument->getXpath();
55
        $translation = $arg->getTranslation();
56
57
        foreach ($spec['fragments'] as $name => $fragmentLocator) {
58
59
            $subNodes = $nodeXpath->query($this->locator->set($fragmentLocator)->xpathMatchAny(), $node);
60
            if (0 === $subNodes->length) {
61
                // fragment not found
62
                // TODO profiler warning
63
                continue;
64
            }
65
66
            $subNode = $subNodes->item(0);
67
            if (!($subNode instanceof Element)) {
68
                throw new Exception\LogicException('Expected node of type Dom\Element');
69
            }
70
71
            $translation[$name . 'OuterHtml'] = $subNode->getOuterHtml();
72
            $translation[$name . 'InnerHtml'] = $subNode->getInnerHtml();
73
74
            foreach ($subNode->getProperties() as $propertyName => $property) {
75
                $translation[$name . ucfirst($propertyName)] = $property;
76
            }
77
        }
78
    }
79
}
80