Completed
Push — master ( 2ab084...c9073a )
by Gabriel
03:02
created

Mapper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 77.42%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 78
ccs 24
cts 31
cp 0.7742
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 2
A createOrReplaceEntity() 0 18 3
A createEntity() 0 8 1
A replace() 0 11 3
A getCollectionItems() 0 12 2
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
            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
            $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
                [$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
                unset($items[$data['id']]);
66
                $collection[$key] = (new $mapperClass())->map($item, $data);
67
                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