Completed
Pull Request — master (#22)
by Sergey
15:59 queued 01:02
created

CacheDecorator::find()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 15
nc 8
nop 3
1
<?php
2
3
class CacheDecorator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private $repository;
6
    private $cacheRepository;
7
    private $allColumns;
8
9
    public function __construct(IRepository $repository, IRepository $cacheRepository)
10
    {
11
        $this->repository = $repository;
12
        $this->cacheRepository = $cacheRepository;
13
        $this->allColumns = new Collection();
14
    }
15
16
    private function getHash(string $id, string $class): string
17
    {
18
        return $class . '@' . $id;
19
    }
20
21
    public function find(string $id, string $class, array $columns = []): ?Storable
22
    {
23
        $hash = $this->getHash($class, $id);
24
        $model = $this->cacheRepository->find($id, $class, $columns);
25
        if ($model) {
26
            if ($columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
27
                // If you wanna specified columns...
28
                if (!array_diff($columns, array_keys($model->toArray()))) {
29
                    return $model;
30
                }
31
            } else {
32
                // If you wanna all columns...
33
                if ($this->allColumns->get($hash, false)) {
34
                    return $model;
35
                }
36
            }
37
        }
38
        $model = $this->repository->find($id, $class, $columns);
39
        $this->cacheRepository->update($model);
40
        if (!$columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
41
            $this->allColumns->put($hash, true);
42
        }
43
        return $model;
44
    }
45
}