Completed
Push — master ( 07c194...4e457c )
by Gabriel
02:39
created

Mapper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 80
ccs 26
cts 31
cp 0.8387
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 2
A replace() 0 11 3
A getCollectionItems() 0 12 2
A createOrReplaceEntity() 0 19 3
A createEntity() 0 9 1
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 3
    protected function create(Collection $collection, array $data, string $entityClass, string $mapperClass)
15
    {
16 3
        $collection->reset();
17 3
        foreach ($data as $element) {
18
            $this->createEntity($element, $collection, $entityClass, $mapperClass);
19
        }
20 3
        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 5
    protected function replace(Collection $collection, array $data, string $entityClass, string $mapperClass)
31
    {
32 5
        $items = $this->getCollectionItems($collection);
33 5
        foreach ($data as $element) {
34 5
            $items = $this->createOrReplaceEntity($element, $items, $collection, $entityClass, $mapperClass);
35
        }
36 5
        foreach ($items as $id => [$key, $item]) {
37 1
            unset($collection[$key]);
38
        }
39 5
        return $collection;
40
    }
41
42 5
    private function getCollectionItems(Collection $items): array
43
    {
44 5
        $finalItems = [];
45
        /**
46
         * @var string $key
47
         * @var ReplaceableEntity $item
48
         */
49 5
        foreach ($items as $key => $item) {
50 1
            $finalItems[$item->getId()] = [$key, $item];
51
        }
52 5
        return $finalItems;
53
    }
54
55 5
    private function createOrReplaceEntity(
56
        array $data, array $items, Collection $collection, string $entityClass, string $mapperClass
57
    ): array
58
    {
59 5
        if (isset($data['id'])) {
60 5
            if (isset($items[$data['id']])) {
61
                /**
62
                 * @var string $key
63
                 * @var Entity $item
64
                 */
65
                [$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...
66
                unset($items[$data['id']]);
67
                $collection[$key] = (new $mapperClass())->map($item, $data);
68
                return $items;
69
            }
70
        }
71 5
        $this->createEntity($data, $collection, $entityClass, $mapperClass);
72 5
        return $items;
73
    }
74
75 5
    private function createEntity(
76
        array $data, Collection $collection, string $entityClass, string $mapperClass
77
    )
78
    {
79
        /** @var Entity $item */
80 5
        $item = new $entityClass();
81 5
        $item = (new $mapperClass())->map($item, $data);
82 5
        $collection->add($item);
83 5
    }
84
}
85