Passed
Push — master ( 4a884d...c9d1fc )
by Jeff
12:14
created

CountryRepository::listStates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->create($params) returns the type Illuminate\Database\Eloquent\Model which includes types incompatible with the type-hinted return App\Shop\Countries\Country.
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findOneOrFail($id) returns the type Illuminate\Database\Eloquent\Model which includes types incompatible with the type-hinted return App\Shop\Countries\Country.
Loading history...
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