|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Shop\Attributes\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Shop\Attributes\Attribute; |
|
6
|
|
|
use App\Shop\Attributes\Exceptions\AttributeNotFoundException; |
|
7
|
|
|
use App\Shop\Attributes\Exceptions\CreateAttributeErrorException; |
|
8
|
|
|
use App\Shop\Attributes\Exceptions\UpdateAttributeErrorException; |
|
9
|
|
|
use App\Shop\AttributeValues\AttributeValue; |
|
10
|
|
|
use App\Shop\Base\BaseRepository; |
|
11
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
12
|
|
|
use Illuminate\Support\Collection; |
|
13
|
|
|
use Illuminate\Database\QueryException; |
|
14
|
|
|
|
|
15
|
|
|
class AttributeRepository extends BaseRepository implements AttributeRepositoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Attribute |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $model; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* AttributeRepository constructor. |
|
24
|
|
|
* @param Attribute $attribute |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(Attribute $attribute) |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct($attribute); |
|
29
|
|
|
$this->model = $attribute; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $data |
|
34
|
|
|
* @return Attribute |
|
35
|
|
|
* @throws CreateAttributeErrorException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function createAttribute(array $data) : Attribute |
|
38
|
|
|
{ |
|
39
|
|
|
try { |
|
40
|
|
|
$attribute = new Attribute($data); |
|
41
|
|
|
$attribute->save(); |
|
42
|
|
|
return $attribute; |
|
43
|
|
|
} catch (QueryException $e) { |
|
44
|
|
|
throw new CreateAttributeErrorException($e); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param int $id |
|
50
|
|
|
* @return Attribute |
|
51
|
|
|
* @throws AttributeNotFoundException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function findAttributeById(int $id) : Attribute |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
|
|
return $this->findOneOrFail($id); |
|
57
|
|
|
} catch (ModelNotFoundException $e) { |
|
58
|
|
|
throw new AttributeNotFoundException($e); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param array $data |
|
64
|
|
|
* @return bool |
|
65
|
|
|
* @throws UpdateAttributeErrorException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function updateAttribute(array $data) : bool |
|
68
|
|
|
{ |
|
69
|
|
|
try { |
|
70
|
|
|
return $this->model->update($data); |
|
71
|
|
|
} catch (QueryException $e) { |
|
72
|
|
|
throw new UpdateAttributeErrorException($e); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return bool|null |
|
78
|
|
|
*/ |
|
79
|
|
|
public function deleteAttribute() : ?bool |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->model->delete(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $columns |
|
86
|
|
|
* @param string $orderBy |
|
87
|
|
|
* @param string $sortBy |
|
88
|
|
|
* @return Collection |
|
89
|
|
|
*/ |
|
90
|
|
|
public function listAttributes($columns = array('*'), string $orderBy = 'id', string $sortBy = 'asc') : Collection |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->all($columns, $orderBy, $sortBy); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return Collection |
|
97
|
|
|
*/ |
|
98
|
|
|
public function listAttributeValues() : Collection |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->model->values()->get(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param AttributeValue $attributeValue |
|
105
|
|
|
* @return AttributeValue |
|
106
|
|
|
*/ |
|
107
|
|
|
public function associateAttributeValue(AttributeValue $attributeValue) : AttributeValue |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->model->values()->save($attributeValue); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|