UpdatesEntity::findAndUpdate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
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