Test Failed
Push — main ( 92d586...908f29 )
by Davide
12:57
created

RegionRepository::assignDataAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
4
namespace App\Repositories;
5
6
use App\Models\Region;
7
use Illuminate\Support\Collection;
8
9
class RegionRepository
10
{
11
    /**
12
     * Get all Regions.
13
     *
14
     * @return \Illuminate\Support\Collection
15
     */
16
    public function getAll(): Collection
17
    {
18
        return Region::orderBy('name')->get();
19
    }
20
21
    /**
22
     * Get Region by id
23
     *
24
     * @param int $id
25
     * @return Region
26
     */
27
    public function getById(int $id)
28
    {
29
        return Region::findOrFail($id);
30
    }
31
32
    /**
33
     * Store Region
34
     *
35
     * @param array $data
36
     * @return Region
37
     */
38
    public function store(array $data): Region
39
    {
40
        $region = new Region();
41
        $region = self::assignDataAttributes($region, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\RegionR...:assignDataAttributes() 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

41
        /** @scrutinizer ignore-call */ 
42
        $region = self::assignDataAttributes($region, $data);
Loading history...
42
43
        $region->save();
44
45
        return $region->fresh();
46
    }
47
48
    /**
49
     * Update Region
50
     *
51
     * @param array $data
52
     * @param int $id
53
     * @return Region
54
     */
55
    public function update(array $data, int $id): Region
56
    {
57
        $region = $this->getById($id);
58
        $region = self::assignDataAttributes($region, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\RegionR...:assignDataAttributes() 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

58
        /** @scrutinizer ignore-call */ 
59
        $region = self::assignDataAttributes($region, $data);
Loading history...
59
60
        $region->update();
61
62
        return $region;
63
    }
64
65
    /**
66
     * Delete Region
67
     *
68
     * @param int $id
69
     * @return void
70
     */
71
    public function delete(int $id): void
72
    {
73
        Region::destroy($id);
74
    }
75
76
    /**
77
     * Assign the attributes of the data array to the object
78
     *
79
     * @param \App\Models\Region $region
80
     * @param array $data
81
     *
82
     * @return \App\Models\Region
83
     */
84
    public function assignDataAttributes(Region $region, array $data): Region
85
    {
86
        $region->name = $data['name'];
87
        $region->timezone = $data['timezone'];
88
        $region->country_id = $data['country_id'];
0 ignored issues
show
Bug introduced by
The property country_id does not exist on App\Models\Region. Did you mean country?
Loading history...
89
90
        return $region;
91
    }
92
}
93