Completed
Push — master ( fe8dc9...ddccce )
by Andrey
02:35
created

CollectionDiffService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getHashElementBuilder() 0 7 2
A setHashElementBuilder() 0 6 1
A buildDiff() 0 11 1
A buildHashForCollectionElements() 0 15 2
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/form-comparator
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\FormComparator\Comparator;
7
8
9
use Nnx\FormComparator\Comparator\CollectionDiffService\HashElementBuilder;
10
use Webmozart\Assert\Assert;
11
use Zend\Form\Element\Collection;
12
use Zend\Form\ElementInterface;
13
14
15
/**
16
 * Class CollectionDiffService
17
 *
18
 * @package Nnx\FormComparator\Comparator
19
 */
20
class CollectionDiffService
21
{
22
    /**
23
     * Компоент отвечающий за подготовку хеша элемента
24
     *
25
     * @var HashElementBuilder
26
     */
27
    private $hashElementBuilder;
28
29
    /**
30
     * Возвращает компоент отвечающий за подготовку хеша элемента
31
     *
32
     * @return HashElementBuilder
33
     */
34
    public function getHashElementBuilder()
35
    {
36
        if (null === $this->hashElementBuilder) {
37
            $this->hashElementBuilder = new HashElementBuilder();
38
        }
39
        return $this->hashElementBuilder;
40
    }
41
42
    /**
43
     * Устанавливает компоент отвечающий за подготовку хеша элемента
44
     *
45
     * @param HashElementBuilder $hashElementBuilder
46
     *
47
     * @return $this
48
     */
49
    public function setHashElementBuilder(HashElementBuilder $hashElementBuilder)
50
    {
51
        $this->hashElementBuilder = $hashElementBuilder;
52
53
        return $this;
54
    }
55
56
57
    /**
58
     * Получение расхождения элементов меджу двумя коллекциями
59
     *
60
     * @param Collection    $sourceCollection
61
     * @param Collection    $targetCollection
62
     *
63
     * @param               $prefixPath
64
     *
65
     * @return mixed
66
     * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException
67
     */
68
    public function buildDiff(Collection $sourceCollection, Collection $targetCollection, $prefixPath)
69
    {
70
        Assert::string($prefixPath);
71
        Assert::notEmpty($prefixPath);
72
73
        $sourceCollectionElementsHash = $this->buildHashForCollectionElements($sourceCollection);
0 ignored issues
show
Unused Code introduced by
$sourceCollectionElementsHash is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
        $targetCollectionElementsHash = $this->buildHashForCollectionElements($targetCollection);
0 ignored issues
show
Unused Code introduced by
$targetCollectionElementsHash is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
76
77
        return;
78
    }
79
80
    /**
81
     * @param Collection $collection
82
     *
83
     *
84
     * @return array
85
     * @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException
86
     */
87
    protected function buildHashForCollectionElements(Collection $collection)
88
    {
89
        $order = 0;
90
        $hashForCollectionElements = [];
91
        $hashElementBuilder = $this->getHashElementBuilder();
92
93
        foreach ($collection->getIterator() as $elementOrFieldset) {
94
            /** @var ElementInterface $elementOrFieldset */
95
            Assert::isInstanceOf($elementOrFieldset, ElementInterface::class);
96
            $hashForCollectionElements[$order] = $hashElementBuilder->hash($elementOrFieldset);
97
            $order++;
98
        }
99
100
        return $hashForCollectionElements;
101
    }
102
103
104
}
105