|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Shop\Brands\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Shop\Base\BaseRepository; |
|
6
|
|
|
use App\Shop\Brands\Brand; |
|
7
|
|
|
use App\Shop\Brands\Exceptions\BrandNotFoundErrorException; |
|
8
|
|
|
use App\Shop\Brands\Exceptions\CreateBrandErrorException; |
|
9
|
|
|
use App\Shop\Brands\Exceptions\DeletingBrandErrorException; |
|
10
|
|
|
use App\Shop\Brands\Exceptions\UpdateBrandErrorException; |
|
11
|
|
|
use App\Shop\Products\Product; |
|
12
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
13
|
|
|
use Illuminate\Database\QueryException; |
|
14
|
|
|
use Illuminate\Support\Collection; |
|
15
|
|
|
|
|
16
|
|
|
class BrandRepository extends BaseRepository implements BrandRepositoryInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* BrandRepository constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param Brand $brand |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(Brand $brand) |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct($brand); |
|
26
|
|
|
$this->model = $brand; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $data |
|
31
|
|
|
* |
|
32
|
|
|
* @return Brand |
|
33
|
|
|
* @throws CreateBrandErrorException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function createBrand(array $data) : Brand |
|
36
|
|
|
{ |
|
37
|
|
|
try { |
|
38
|
|
|
return $this->create($data); |
|
|
|
|
|
|
39
|
|
|
} catch (QueryException $e) { |
|
40
|
|
|
throw new CreateBrandErrorException($e); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param int $id |
|
46
|
|
|
* |
|
47
|
|
|
* @return Brand |
|
48
|
|
|
* @throws BrandNotFoundErrorException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function findBrandById(int $id) : Brand |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
|
|
return $this->findOneOrFail($id); |
|
|
|
|
|
|
54
|
|
|
} catch (ModelNotFoundException $e) { |
|
55
|
|
|
throw new BrandNotFoundErrorException($e); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array $data |
|
61
|
|
|
* @param int $id |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool |
|
64
|
|
|
* @throws UpdateBrandErrorException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function updateBrand(array $data, int $id) : bool |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
|
|
return $this->update($data, $id); |
|
70
|
|
|
} catch (QueryException $e) { |
|
71
|
|
|
throw new UpdateBrandErrorException($e); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param int $id |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
* @throws DeletingBrandErrorException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function deleteBrand(int $id) : bool |
|
82
|
|
|
{ |
|
83
|
|
|
try { |
|
84
|
|
|
return $this->delete($id); |
|
85
|
|
|
} catch (QueryException $e) { |
|
86
|
|
|
throw new DeletingBrandErrorException($e); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $columns |
|
92
|
|
|
* @param string $orderBy |
|
93
|
|
|
* @param string $sortBy |
|
94
|
|
|
* |
|
95
|
|
|
* @return Collection |
|
96
|
|
|
*/ |
|
97
|
|
|
public function listBrands($columns = array('*'), string $orderBy = 'id', string $sortBy = 'asc') : Collection |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->all($columns, $orderBy, $sortBy); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return Collection |
|
104
|
|
|
*/ |
|
105
|
|
|
public function listProducts() : Collection |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->model->products()->get(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param Product $product |
|
112
|
|
|
*/ |
|
113
|
|
|
public function saveProduct(Product $product) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->model->products()->save($product); |
|
116
|
|
|
} |
|
117
|
|
|
} |