Passed
Push — main ( cbd8d3...5d0786 )
by Michael
03:37 queued 29s
created

WithModelMapping   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 4
eloc 13
c 5
b 1
f 0
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A mapModelAttributes() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\ModelMapper\Traits;
6
7
use Illuminate\Database\Eloquent\Model;
8
9
trait WithModelMapping
10
{
11
    /**
12
     * Maps your model attributes to local class properties.
13
     *
14
     * @param Model|null $model
15
     *
16
     * @return void
17
     */
18 6
    public function mapModelAttributes(?Model $model = null): void
19
    {
20 6
        if (! is_null($model)) {
21 6
            $toIgnore = config('model-mapper.ignore_attributes');
22
23 6
            $ignores = is_array($toIgnore)
24 6
                ? $toIgnore
25
                : [];
26
27 6
            collect($model->getAttributes())
28 6
                ->except(...$ignores)
29 6
                ->each(function ($value, $property) use ($model) {
30 6
                    if (property_exists($this, $property)) {
31 6
                        rescue(
32 6
                            fn () => $this->{$property} = $model->{$property},
33 6
                            fn () => null,
34 6
                            config('model-mapper.log') ?? false
35
                        );
36
                    }
37 6
                });
38
        }
39 6
    }
40
}
41