XmlIntersectionService   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
eloc 30
c 3
b 0
f 0
dl 0
loc 108
ccs 33
cts 33
cp 1
rs 10

8 Methods

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