HasAttributes::isRelationValueDirty()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 9.9666
cc 4
nc 1
nop 1
crap 20
1
<?php
2
3
namespace Ianrizky\Illuminate\Database\Eloquent\Concerns;
4
5
use DomainException;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
use InvalidArgumentException;
10
11
trait HasAttributes
12
{
13
    /**
14
     * Determine if the model relation value has been modified.
15
     *
16
     * @param  array|string|null  $relations
17
     * @return bool
18
     *
19
     * @throws \InvalidArgumentException
20
     */
21
    public function isRelationValueDirty($relations = null): bool
22
    {
23
        $attributes = array_map(function ($name) {
24
            $relation = $this->$name();
25
26
            if (!$relation instanceof Relation || !method_exists($relation, 'getForeignKeyName')) {
27
                throw new InvalidArgumentException(sprintf(
28
                    'The given relation name %s on model class %s must return a relationship instance and has getForeignKeyName() method.',
29
                    $name, static::class
30
                ));
31
            }
32
33
            return $relation->getForeignKeyName();
34
        }, is_array($relations) ? $relations : func_get_args());
35
36
        return $this->isDirty($attributes);
0 ignored issues
show
Bug introduced by
It seems like isDirty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        return $this->/** @scrutinizer ignore-call */ isDirty($attributes);
Loading history...
37
    }
38
39
    /**
40
     * Determine whether the given model collection is valid with the given class name.
41
     *
42
     * @param  \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>  $models
43
     * @param  string  $className
44
     * @param  bool  $throwExceptionWhenNotValid
45
     * @return bool
46
     *
47
     * @throws \DomainException
48
     */
49
    protected function isCollectionValid(Collection $models, string $className, bool $throwExceptionWhenNotValid = true): bool
50
    {
51
        return $models->every(function (Model $model) use ($className, $throwExceptionWhenNotValid) {
52
            $isValid = $model instanceof $className;
53
54
            if (!$isValid && $throwExceptionWhenNotValid) {
55
                throw new DomainException(sprintf(
56
                    'Invalid %s model type, %s given', $className, get_class($model)
57
                ));
58
            }
59
60
            return $isValid;
61
        });
62
    }
63
64
    /**
65
     * Return collection value from specified model relation.
66
     *
67
     * @param  string  $key
68
     * @param  string  $className
69
     * @return \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>
70
     *
71
     * @throws \DomainException
72
     */
73
    public function getCollectionValue(string $key, string $className): Collection
74
    {
75
        $this->isCollectionValid($models = $this->getRelationValue($key), $className);
0 ignored issues
show
Bug introduced by
It seems like getRelationValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

75
        $this->isCollectionValid($models = $this->/** @scrutinizer ignore-call */ getRelationValue($key), $className);
Loading history...
76
77
        return $models;
78
    }
79
}
80