Passed
Pull Request — master (#17)
by Orkhan
03:43
created

EloquentRepository::getWhereInFirst()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Orkhanahmadov\EloquentRepository;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Contracts\Cache\Factory as Cache;
9
use Illuminate\Contracts\Foundation\Application;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Orkhanahmadov\EloquentRepository\Repository\Criteria;
12
use Illuminate\Contracts\Container\BindingResolutionException;
13
use Orkhanahmadov\EloquentRepository\Repository\Contracts\Repository;
14
use Orkhanahmadov\EloquentRepository\Repository\Concerns\CreatesEntity;
15
use Orkhanahmadov\EloquentRepository\Repository\Concerns\DeletesEntity;
16
use Orkhanahmadov\EloquentRepository\Repository\Concerns\SelectsEntity;
17
use Orkhanahmadov\EloquentRepository\Repository\Concerns\UpdatesEntity;
18
19
class EloquentRepository implements Repository
20
{
21
    use SelectsEntity;
22
    use CreatesEntity;
23
    use UpdatesEntity;
24
    use DeletesEntity;
25
26
    /**
27
     * @var Application
28
     */
29
    private $application;
30
    /**
31
     * @var Cache
32
     */
33
    protected $cache;
34
    /**
35
     * @var int
36
     */
37
    protected $cacheTTL = 3600;
38
    /**
39
     * @var string|null
40
     */
41
    protected $entity = null;
42
    /**
43
     * @var Builder|Model
44
     */
45
    protected $model;
46
47
    /**
48
     * EloquentRepository constructor.
49
     *
50
     * @param Application $application
51
     * @param Cache $cache
52
     * @throws BindingResolutionException
53
     */
54
    public function __construct(Application $application, Cache $cache)
55
    {
56
        $this->application = $application;
57
        $this->cache = $cache;
58
59
        if ($this->entity) {
60
            $this->resolveEntity();
61
        }
62
    }
63
64
    /**
65
     * @param Builder|Model $entity
66
     *
67
     * @return self
68
     *
69
     * @throws BindingResolutionException
70
     */
71
    public function entity($entity): self
72
    {
73
        $this->entity = $entity;
0 ignored issues
show
Documentation Bug introduced by
It seems like $entity can also be of type Illuminate\Database\Eloquent\Builder. However, the property $entity is declared as type null|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
74
        $this->resolveEntity();
75
76
        return $this;
77
    }
78
79
    /**
80
     * Sets listed criteria for entity.
81
     *
82
     * @param mixed ...$criteria
83
     *
84
     * @return self
85
     */
86
    public function withCriteria(...$criteria): self
87
    {
88
        $criteria = Arr::flatten($criteria);
89
90
        foreach ($criteria as $criterion) {
91
            /* @var Criteria\Criteria $criterion */
92
            $this->model = $criterion->apply($this->model);
0 ignored issues
show
Bug introduced by
The method apply() does not exist on Orkhanahmadov\EloquentRe...itory\Criteria\Criteria. ( Ignorable by Annotation )

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

92
            /** @scrutinizer ignore-call */ 
93
            $this->model = $criterion->apply($this->model);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
        }
94
95
        return $this;
96
    }
97
98
    /**
99
     * Defines cache key.
100
     *
101
     * @return string
102
     */
103
    public function cacheKey(): string
104
    {
105
        return $this->model->getTable();
106
    }
107
108
    /**
109
     * Removes cache for model.
110
     *
111
     * @param Model $model
112
     */
113
    public function invalidateCache($model): void
114
    {
115
        $this->cache->forget(
116
            $this->cacheKey().'.*'
117
        );
118
        $this->cache->forget(
119
            $this->cacheKey().'.'.$model->id
120
        );
121
    }
122
123
    /**
124
     * Resolves entity.
125
     *
126
     * @throws BindingResolutionException
127
     */
128
    private function resolveEntity(): void
129
    {
130
        $this->model = $this->application->make($this->entity);
131
    }
132
133
    /**
134
     * Get cache time-to-live value from property or method if available.
135
     *
136
     * @return int
137
     */
138
    private function cacheTTLValue(): int
0 ignored issues
show
Unused Code introduced by
The method cacheTTLValue() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
139
    {
140
        if (method_exists($this, 'cacheTTL')) {
141
            return $this->cacheTTL();
142
        }
143
144
        return $this->cacheTTL;
145
    }
146
147
    /**
148
     * Throws ModelNotFoundException exception.
149
     *
150
     * @param array|int $ids
151
     */
152
    private function throwModelNotFoundException($ids = [])
0 ignored issues
show
Unused Code introduced by
The method throwModelNotFoundException() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
153
    {
154
        throw (new ModelNotFoundException)->setModel(
155
            get_class($this->model->getModel()),
156
            $ids
157
        );
158
    }
159
}
160