Test Failed
Push — master ( fdb79d...2e4512 )
by Chris
19:35
created

SystemModelCollection::diffCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leonidas\Library\System;
4
5
use Contracts\Collection\CollectionInterface;
6
7
class SystemModelCollection implements CollectionInterface
8
{
9
    protected string $entityType;
10
11
    protected string $idKey;
12
13
    protected string $slugKey;
14
15
    protected array $items;
16
17
    public function __construct(string $entityType, string $idKey, string $slugKey, ...$items)
18
    {
19
        $this->entityType = $entityType;
20
        $this->idKey = $idKey;
21
        $this->slugKey = $slugKey;
22
        $this->items = $items;
23
    }
24
25
    public function all(): array
26
    {
27
        return $this->items;
28
    }
29
30
    public function has(string $item): bool
31
    {
32
        return isset($this->items[$item]);
33
    }
34
35
    public function remove(string $item): void
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
    public function remove(/** @scrutinizer ignore-unused */ string $item): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        //
38
    }
39
40
    public function select(string $property): array
41
    {
42
        return array_map(function ($object) use ($property) {
43
            return $object->{$property};
44
        }, $this->items);
45
    }
46
47
    public function getIds(): array
48
    {
49
        return $this->select($this->idKey);
50
    }
51
52
    public function getMeta(string $metaKey): array
53
    {
54
        $meta = [];
55
56
        foreach ($this->items as $object) {
57
            $id = $object->{$this->idKey};
58
59
            $meta[$id] = $this->getObjectMetadata($object, $metaKey);
60
        }
61
62
        return $meta;
63
    }
64
65
    public function isEmpty(): bool
66
    {
67
        return empty($this->items);
68
    }
69
70
    public function sortByMeta(string $metaKey)
71
    {
72
        $orderArray = [];
73
74
        $collection = $this->items;
75
        $id = $this->idKey;
76
77
        foreach ($collection as $item) {
78
            $orderArray[$item->{$id}] = $this->getObjectMetadata($item, $metaKey);
79
        }
80
81
        usort($collection, $this->sortByMetaCallback($orderArray));
82
83
        return $collection;
84
    }
85
86
    public function diff(CollectionInterface $collection): CollectionInterface
87
    {
88
        $items = array_udiff(
89
            $this->all(),
90
            $collection->all(),
91
            $this->diffCallback()
92
        );
93
94
        return new static(
95
            $this->idKey,
96
            $this->nameKey,
0 ignored issues
show
Bug Best Practice introduced by
The property nameKey does not exist on Leonidas\Library\System\SystemModelCollection. Did you maybe forget to declare it?
Loading history...
97
            $this->slugKey,
98
            $this->entityType,
99
            $items
100
        );
101
    }
102
103
    protected function getObjectMetadata(object $object, string $metaKey)
104
    {
105
        return get_metadata($this->entityType, $object->{$this->idKey}, $metaKey, true);
0 ignored issues
show
Bug introduced by
The function get_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        return /** @scrutinizer ignore-call */ get_metadata($this->entityType, $object->{$this->idKey}, $metaKey, true);
Loading history...
106
    }
107
108
    protected function diffCallback(): callable
109
    {
110
        return function ($object1, $object2) {
111
            return $object1->{$this->idKey} - $object2->{$this->idKey};
112
        };
113
    }
114
115
    /**
116
     * @param array $orderArray Associative array with object ids as keys and
117
     *              desired order as values
118
     *
119
     * @return callable
120
     */
121
    protected function sortByMetaCallback(array $orderArray): callable
122
    {
123
        return function ($a, $b) use ($orderArray) {
124
            // Set value to 0 if one is not provided
125
            $a = (int) $orderArray[$a->{$this->idKey}] ?? 0;
126
            $b = (int) $orderArray[$b->{$this->idKey}] ?? 0;
127
128
            if ($a === $b) {
129
                return 0;
130
            }
131
132
            if ($a < $b && $a === 0) {
133
                return 1;
134
            }
135
136
            if ($a > $b && $b === 0) {
137
                return -1;
138
            }
139
140
            return $a > $b ? 1 : -1;
141
        };
142
    }
143
}
144