Completed
Push — master ( b75a21...510a86 )
by Alexander
02:36
created

XmlIntersectionService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 113
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B intersect() 0 28 5
A compare() 0 14 4
A getSource() 0 4 1
A setSource() 0 6 1
A getTarget() 0 4 1
A setTarget() 0 6 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: horat1us
5
 * Date: 5/11/17
6
 * Time: 6:32 PM
7
 */
8
9
namespace Horat1us\Services;
10
11
12
use Horat1us\XmlConvertibleInterface;
13
use Horat1us\XmlConvertibleObject;
14
15
class XmlIntersectionService
16
{
17
    /**
18
     * @var XmlConvertibleInterface
19
     */
20
    protected $source;
21
22
    /**
23
     * @var XmlConvertibleInterface
24
     */
25
    protected $target;
26
27
28 4
    public function __construct(
29
        XmlConvertibleInterface $source,
30
        XmlConvertibleInterface $target
31
    )
32
    {
33
        $this
34 4
            ->setSource($source)
35 4
            ->setTarget($target);
36 4
    }
37
38
    /**
39
     * @return null|XmlConvertibleInterface
40
     */
41 4
    public function intersect()
42
    {
43 4
        $current = clone $this->getSource();
44 4
        $compared = clone $this->getTarget();
45
46
        if (
47 4
            $current->getXmlElementName() !== $compared->getXmlElementName()
48 3
            || array_reduce(
49 3
                $current->getXmlProperties(),
50 3
                function (bool $carry, string $property) use ($compared, $current) : bool {
51 3
                    return $carry
52 3
                        || (!property_exists($compared, $property))
53 3
                        || $current->{$property} !== $compared->{$property};
54 3
                },
55 4
                false
56
            )
57
        ) {
58 1
            return null;
59
        }
60
61 3
        $newChildren = array_uintersect(
62 3
            $compared->getXmlChildren() ?? [],
63 3
            $current->getXmlChildren() ?? [],
64 3
            [$this, 'compare']
65
        );
66
67 3
        return $current->setXmlChildren($newChildren);
68
    }
69
70
    /**
71
     * @param $comparedChild
72
     * @param $currentChild
73
     * @return int
74
     */
75 2
    public function compare($comparedChild, $currentChild)
76
    {
77 2
        $source = $currentChild instanceof XmlConvertibleInterface
78 2
            ? $currentChild
79 2
            : XmlConvertibleObject::fromXml($currentChild);
80
81 2
        $target = $comparedChild instanceof XmlConvertibleInterface
82 2
            ? $comparedChild
83 2
            : XmlConvertibleObject::fromXml($comparedChild);
84
85 2
        $diff = $source->xmlIntersect($target);
86
87 2
        return $diff === null ? -1 : 0;
88
    }
89
90
    /**
91
     * @return XmlConvertibleInterface
92
     */
93 4
    public function getSource(): XmlConvertibleInterface
94
    {
95 4
        return $this->source;
96
    }
97
98
    /**
99
     * @param XmlConvertibleInterface $source
100
     * @return $this
101
     */
102 4
    public function setSource(XmlConvertibleInterface $source)
103
    {
104 4
        $this->source = $source;
105
106 4
        return $this;
107
    }
108
109
    /**
110
     * @return XmlConvertibleInterface
111
     */
112 4
    public function getTarget(): XmlConvertibleInterface
113
    {
114 4
        return $this->target;
115
    }
116
117
    /**
118
     * @param XmlConvertibleInterface $target
119
     * @return $this
120
     */
121 4
    public function setTarget(XmlConvertibleInterface $target)
122
    {
123 4
        $this->target = $target;
124
125 4
        return $this;
126
    }
127
}