UpdatesEntity::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 10
c 2
b 0
f 1
1
<?php
2
3
namespace Orkhanahmadov\EloquentRepository\Repository\Concerns;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Orkhanahmadov\EloquentRepository\Repository\Contracts\Cacheable;
8
9
/**
10
 * @method Builder|Model find($modelId)
11
 * @method void invalidateCache($model)
12
 * @mixin \Orkhanahmadov\EloquentRepository\EloquentRepository
13
 */
14
trait UpdatesEntity
15
{
16
    /**
17
     * Finds a model with ID and updates it with given properties.
18
     *
19
     * @param int|string $modelId
20
     * @param mixed $properties
21
     *
22
     * @return Builder|Model
23
     */
24
    public function findAndUpdate($modelId, $properties)
25
    {
26
        $model = $this->find($modelId);
27
28
        return $this->update($model, $properties);
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $model of Orkhanahmadov\EloquentRe...UpdatesEntity::update() does only seem to accept Illuminate\Database\Eloquent\Model, 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

28
        return $this->update(/** @scrutinizer ignore-type */ $model, $properties);
Loading history...
29
    }
30
31
    /**
32
     * Updates a model given properties.
33
     *
34
     * @param Model $model
35
     * @param mixed $properties
36
     *
37
     * @return Builder|Model
38
     */
39
    public function update($model, $properties)
40
    {
41
        if ($this instanceof Cacheable) {
42
            $this->invalidateCache($model);
43
        }
44
45
        $model->fill($properties)->save();
46
47
        return $model->refresh();
48
    }
49
}
50