Repository::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of ibrand/address.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Component\Address;
13
14
use Prettus\Repository\Eloquent\BaseRepository;
15
use Prettus\Repository\Traits\CacheableRepository;
16
17
/**
18
 * Class Repository.
19
 */
20
class Repository extends BaseRepository implements RepositoryContract
21
{
22
    use CacheableRepository;
23
24
    /**
25
     * Specify Model class name.
26
     *
27
     * @return string
28
     */
29 3
    public function model()
30
    {
31 3
        return Address::class;
32
    }
33
34
    /**
35
     * @param array $attributes
36
     *
37
     * @return mixed
38
     */
39 3
    public function create(array $attributes = [])
40
    {
41 3
        $address = parent::create($attributes);
42
43 3
        $this->setDefault($address);
44
45 3
        return $address;
46
    }
47
48
    /**
49
     * @param array $attributes
50
     * @param $id
51
     *
52
     * @return mixed
53
     */
54 1
    public function update(array $attributes, $id)
55
    {
56 1
        $address = parent::update($attributes, $id);
57
58 1
        $this->setDefault($address);
59
60 1
        return $address;
61
    }
62
63
    /**
64
     * @param Address $address
65
     */
66 3
    protected function setDefault(Address $address)
67
    {
68 3
        if (1 == $address->is_default) {
69 3
            $this->model->where('user_id', $address->user_id)
70 3
                ->where('id', '!=', $address->id)->update(['is_default' => 0]);
71
        }
72 3
    }
73
74
    /**
75
     * @param $userId
76
     *
77
     * @return mixed
78
     */
79 2
    public function getByUser($userId)
80
    {
81 2
        return $this->orderBy('updated_at', 'desc')->findByField('user_id', $userId);
82
    }
83
84
    /**
85
     * @param $userId
86
     */
87 1
    public function getDefaultByUser($userId)
88
    {
89 1
        $addresses = $this->getByUser($userId);
90
91 1
        if (0 == count($addresses)) {
92 1
            return null;
93
        }
94
95 1
        $default = $addresses->filter(function ($address) {
96 1
            return 1 == $address->is_default;
97 1
        })->first();
98
99 1
        if (!$default) {
100 1
            $default = $addresses->first();
101
        }
102
103 1
        return $default;
104
    }
105
106
    /**
107
     * @param array $attributes
108
     * @param $id
109
     * @param $userId
110
     *
111
     * @return mixed
112
     */
113 1
    public function updateByUser(array $attributes, $id, $userId)
114
    {
115 1
        $address = $this->find($id);
116
117 1
        if ($address->user_id != $userId) {
118 1
            return false;
119
        }
120
121 1
        return $this->update($attributes, $id);
122
    }
123
}
124