Passed
Push — master ( 2dd083...084bc8 )
by Ricardo
06:10 queued 01:22
created

Model   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A refresh() 0 17 2
A newPivot() 0 4 2
1
<?php
2
3
namespace Cino\LaravelChronos\Eloquent;
4
5
use Cino\LaravelChronos\Eloquent\Concerns\ChronosRelations;
6
use Cino\LaravelChronos\Eloquent\Concerns\ChronosTimestamps;
7
use Cino\LaravelChronos\Eloquent\Relations\Pivot;
8
use Illuminate\Database\Eloquent\Model as BaseModel;
9
use Illuminate\Support\Collection;
10
11
abstract class Model extends BaseModel
12
{
13
    use ChronosRelations;
14
    use ChronosTimestamps;
15
16
    /**
17
     * Create a new pivot model instance.
18
     *
19
     * @param \Illuminate\Database\Eloquent\Model $parent
20
     * @param array $attributes
21
     * @param string $table
22
     * @param bool $exists
23
     * @param string|null $using
24
     * @return \Cino\LaravelChronos\Eloquent\Relations\Pivot
25
     */
26
    public function newPivot(BaseModel $parent, array $attributes, $table, $exists, $using = null)
27
    {
28
        return $using ? $using::fromRawAttributes($parent, $attributes, $table, $exists)
29
            : Pivot::fromAttributes($parent, $attributes, $table, $exists);
30
    }
31
32
    /**
33
     * Reload the current model instance with fresh attributes from the database.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Model
36
     */
37
    public function refresh()
38
    {
39
        if (!$this->exists) {
40
            return $this;
41
        }
42
43
        $this->setRawAttributes(
44
            static::newQueryWithoutScopes()->findOrFail($this->getKey())->attributes
0 ignored issues
show
Bug Best Practice introduced by
The method Illuminate\Database\Eloq...newQueryWithoutScopes() is not static, but was called statically. ( Ignorable by Annotation )

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

44
            static::/** @scrutinizer ignore-call */ 
45
                    newQueryWithoutScopes()->findOrFail($this->getKey())->attributes
Loading history...
45
        );
46
47
        $this->load(Collection::make($this->relations)->reject(function ($relation) {
48
            return $relation instanceof Pivot;
49
        })->keys()->all());
50
51
        $this->syncOriginal();
52
53
        return $this;
54
    }
55
}
56