HasAttributes   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 67
ccs 0
cts 21
cp 0
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isRelationValueDirty() 0 16 4
A isCollectionValid() 0 12 3
A getCollectionValue() 0 5 1
1
<?php
2
3
namespace App\Support\Model;
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
/**
12
 * @property int $id
13
 * @property \Illuminate\Support\Carbon $created_at
14
 * @property \Illuminate\Support\Carbon $updated_at
15
 */
16
trait HasAttributes
17
{
18
    /**
19
     * Determine if the model relation value has been modified.
20
     *
21
     * @param  array|string|null  $relations
22
     * @return bool
23
     *
24
     * @throws \InvalidArgumentException
25
     */
26
    public function isRelationValueDirty($relations = null): bool
27
    {
28
        $attributes = array_map(function ($name) {
29
            $relation = $this->$name();
30
31
            if (!$relation instanceof Relation || !method_exists($relation, 'getForeignKeyName')) {
32
                throw new InvalidArgumentException(sprintf(
33
                    'The given relation name %s on model class %s must return a relationship instance and has getForeignKeyName() method.',
34
                    $name, static::class
35
                ));
36
            }
37
38
            return $relation->getForeignKeyName();
39
        }, is_array($relations) ? $relations : func_get_args());
40
41
        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

41
        return $this->/** @scrutinizer ignore-call */ isDirty($attributes);
Loading history...
42
    }
43
44
    /**
45
     * Determine whether the given model collection is valid with the given class name.
46
     *
47
     * @param  \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>  $models
48
     * @param  string  $className
49
     * @param  bool  $throwExceptionWhenNotValid
50
     * @return bool
51
     *
52
     * @throws \DomainException
53
     */
54
    protected function isCollectionValid(Collection $models, string $className, bool $throwExceptionWhenNotValid = true): bool
55
    {
56
        return $models->every(function (Model $model) use ($className, $throwExceptionWhenNotValid) {
57
            $isValid = $model instanceof $className;
58
59
            if (!$isValid && $throwExceptionWhenNotValid) {
60
                throw new DomainException(sprintf(
61
                    'Invalid %s model type, %s given', $className, get_class($model)
62
                ));
63
            }
64
65
            return $isValid;
66
        });
67
    }
68
69
    /**
70
     * Return collection value from specified model relation.
71
     *
72
     * @param  string  $key
73
     * @param  string  $className
74
     * @return \Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>
75
     *
76
     * @throws \DomainException
77
     */
78
    public function getCollectionValue(string $key, string $className): Collection
79
    {
80
        $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

80
        $this->isCollectionValid($models = $this->/** @scrutinizer ignore-call */ getRelationValue($key), $className);
Loading history...
81
82
        return $models;
83
    }
84
}
85