|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Shop\Countries\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Shop\Base\BaseRepository; |
|
6
|
|
|
use App\Shop\Countries\Exceptions\CountryInvalidArgumentException; |
|
7
|
|
|
use App\Shop\Countries\Exceptions\CountryNotFoundException; |
|
8
|
|
|
use App\Shop\Countries\Repositories\Interfaces\CountryRepositoryInterface; |
|
9
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
10
|
|
|
use Illuminate\Database\QueryException; |
|
11
|
|
|
use App\Shop\Countries\Country; |
|
12
|
|
|
use Illuminate\Support\Collection; |
|
13
|
|
|
|
|
14
|
|
|
class CountryRepository extends BaseRepository implements CountryRepositoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* CountryRepository constructor. |
|
18
|
|
|
* @param Country $country |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(Country $country) |
|
21
|
|
|
{ |
|
22
|
|
|
parent::__construct($country); |
|
23
|
|
|
$this->model = $country; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* List all the countries |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $order |
|
30
|
|
|
* @param string $sort |
|
31
|
|
|
* @return Collection |
|
32
|
|
|
*/ |
|
33
|
|
|
public function listCountries(string $order = 'id', string $sort = 'desc') : Collection |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->model->where('status', 1)->get(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $params |
|
40
|
|
|
* @return Country |
|
41
|
|
|
*/ |
|
42
|
|
|
public function createCountry(array $params) : Country |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->create($params); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Find the country |
|
49
|
|
|
* |
|
50
|
|
|
* @param $id |
|
51
|
|
|
* @return Country |
|
52
|
|
|
* @throws CountryNotFoundException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function findCountryById(int $id) : Country |
|
55
|
|
|
{ |
|
56
|
|
|
try { |
|
57
|
|
|
return $this->findOneOrFail($id); |
|
|
|
|
|
|
58
|
|
|
} catch (ModelNotFoundException $e) { |
|
59
|
|
|
throw new CountryNotFoundException('Country not found.'); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Show all the provinces |
|
65
|
|
|
* |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
|
|
public function findProvinces() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->model->provinces; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Update the country |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $params |
|
77
|
|
|
* |
|
78
|
|
|
* @return Country |
|
79
|
|
|
* @throws CountryNotFoundException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function updateCountry(array $params) : Country |
|
82
|
|
|
{ |
|
83
|
|
|
try { |
|
84
|
|
|
$this->model->update($params); |
|
85
|
|
|
return $this->findCountryById($this->model->id); |
|
86
|
|
|
} catch (QueryException $e) { |
|
87
|
|
|
throw new CountryInvalidArgumentException($e->getMessage()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* @return Collection |
|
94
|
|
|
*/ |
|
95
|
|
|
public function listStates() : Collection |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->model->states()->get(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|