Completed
Push — master ( 3c4917...c12914 )
by Gabriel
03:23
created

Mapper::replace()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 4
crap 3
1
<?php
2
3
namespace Waredesk;
4
5
abstract class Mapper
6
{
7
    /**
8
     * @param Collection $collection
9
     * @param array $data
10
     * @param string $entityClass
11
     * @param string $mapperClass
12
     * @return Collection|mixed
13
     */
14 4
    protected function create(Collection $collection, array $data, string $entityClass, string $mapperClass)
15
    {
16 4
        $collection->reset();
17 4
        foreach ($data as $element) {
18
            $this->createEntity($element, $collection, $entityClass, $mapperClass);
19
        }
20 4
        return $collection;
21
    }
22
23
    /**
24
     * @param Collection $collection
25
     * @param array $data
26
     * @param string $entityClass
27
     * @param string $mapperClass
28
     * @return Collection|mixed
29
     */
30 11
    protected function replace(Collection $collection, array $data, string $entityClass, string $mapperClass)
31
    {
32 11
        $items = $this->getCollectionItems($collection);
33 11
        foreach ($data as $element) {
34 11
            $items = $this->createOrReplaceEntity($element, $items, $collection, $entityClass, $mapperClass);
35
        }
36 11
        foreach ($items as $id => [$key, $item]) {
37 1
            unset($collection[$key]);
38
        }
39 11
        return $collection;
40
    }
41
42 11
    private function getCollectionItems(Collection $items): array
43
    {
44 11
        $finalItems = [];
45
        /**
46
         * @var string $key
47
         * @var ReplaceableEntity $item
48
         */
49 11
        foreach ($items as $key => $item) {
50 2
            $finalItems[$item->getId()] = [$key, $item];
51
        }
52 11
        return $finalItems;
53
    }
54
55 11
    private function createOrReplaceEntity(
56
        array $data, array $items, Collection $collection, string $entityClass, string $mapperClass
57
    ): array {
58 11
        if (isset($data['id'])) {
59 11
            if (isset($items[$data['id']])) {
60
                /**
61
                 * @var string $key
62
                 * @var Entity $item
63
                 */
64 1
                [$key, $item] = $items[$data['id']];
0 ignored issues
show
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $item does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
65 1
                unset($items[$data['id']]);
66 1
                $collection[$key] = (new $mapperClass())->map($item, $data);
67 1
                return $items;
68
            }
69
        }
70 11
        $this->createEntity($data, $collection, $entityClass, $mapperClass);
71 11
        return $items;
72
    }
73
74 11
    private function createEntity(
75
        array $data, Collection $collection, string $entityClass, string $mapperClass
76
    ) {
77
        /** @var Entity $item */
78 11
        $item = new $entityClass();
79 11
        $item = (new $mapperClass())->map($item, $data);
80 11
        $collection->add($item);
81 11
    }
82
}
83