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

Model::newPivot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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