Passed
Push — master ( 2e7c63...9eb36d )
by Artem
01:35
created

ArrayRepository::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prozorov\Repositories;
6
7
use Prozorov\Repositories\Contracts\RepositoryInterface;
8
use Prozorov\Repositories\{Query, Result};
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Prozorov\Repositories\Result. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, Prozorov\Repositories\Query. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Prozorov\Repositories\Exceptions\DataNotFound;
10
use Illuminate\Support\Collection;
11
12
class ArrayRepository implements RepositoryInterface
13
{
14
    /**
15
     * @var Collection $data
16
     */
17
    protected $data;
18
19
    /**
20
     * @var string $idField
21
     */
22
    protected $idField;
23
24 15
    public function __construct(array $data, string $idField = 'id')
25
    {
26 15
        $this->data = (new Collection($data))->keyBy('id');
27 15
        $this->idField = $idField;
28 15
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33 1
    public function create(array $data)
34
    {
35 1
        $this->data->put($data[$this->idField], $data);
36 1
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41 1
    public function update($model, array $data): void
42
    {
43 1
        if (is_int($model)) {
44 1
            $model = $this->data->get($model);
45
46 1
            $this->data->put($model[$this->idField], array_merge($model, $data));
47
        }
48 1
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53 1
    public function delete(int $id): void
54
    {
55 1
        $this->data->forget($id);
56 1
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 1
    public function exists(array $filter): bool
62
    {
63 1
        $data = $this->getRaw((new Query())->where($filter));
64
65 1
        return ! empty($data);
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71 5
    public function count(array $filter = []): int
72
    {
73 5
        $data = $this->getRaw((new Query())->where($filter));
74
75 5
        return count($data);
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81 2
    public function get(Query $query): Result
82
    {
83 2
        return new Result(
84 2
            $this->getRaw($query),
0 ignored issues
show
Bug introduced by
It seems like $this->getRaw($query) can also be of type null; however, parameter $data of Prozorov\Repositories\Result::__construct() does only seem to accept iterable, maybe add an additional type check? ( Ignorable by Annotation )

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

84
            /** @scrutinizer ignore-type */ $this->getRaw($query),
Loading history...
85 2
            $this->getMeta($query)
86
        );
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function first(array $filter)
93
    {
94
        $data = $this->getRaw((new Query())->where($filter)->limit(1));
95
96
        if (empty($data[0])) {
97
            return null;
98
        }
99
100
        return $data[0];
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106 6
    public function getById(int $id, array $select = null)
107
    {
108 6
        return $this->data->get($id);
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114 1
    public function getByIdOrFail(int $id, array $select = null)
115
    {
116 1
        $data = $this->getById($id, $select);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $this->getById($id, $select) targeting Prozorov\Repositories\ArrayRepository::getById() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
117
118 1
        if (empty($data)) {
119 1
            throw new DataNotFound();
120
        }
121
122
        return $data;
123
    }
124
125
    /**
126
     * Returns raw data
127
     *
128
     * @access	protected
129
     * @param	Query	$query	
130
     * @return	iterable|null
131
     */
132 7
    protected function getRaw(Query $query)
133
    {
134 7
        $data = $this->data;
135
136 7
        if (! empty($query->getWhere())) {
137 3
            foreach ($query->getWhere() as $key => $value) {
138 3
                $data = $this->data->where($key, $value);
139
            }
140
        }
141
142 7
        return $data->values()->toArray();
143
    }
144
145
    /**
146
     * Calculate meta information
147
     * 
148
     * @access	protected
149
     * @param	Query	$query
150
     * @return	array|null
151
     */
152 2
    protected function getMeta(Query $query): ?array
153
    {
154 2
        if ($query->isWithMeta()) {
155
            $meta = [
156 1
                'offset' => $query->getOffset(),
157 1
                'limit' => $query->getLimit(),
158
            ];
159
160 1
            if ($query->isCountTotal()) {
161 1
                $meta['total'] = $this->count($query->getWhere() ?? []);
162
            }
163
        }
164
165 2
        return $meta ?? null;
166
    }
167
}
168