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

CityRepository::listCitiesByStateCode()   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 1
1
<?php
2
3
namespace App\Shop\Cities\Repositories;
4
5
use App\Shop\Base\BaseRepository;
6
use App\Shop\Cities\Exceptions\CityNotFoundException;
7
use App\Shop\Cities\Repositories\Interfaces\CityRepositoryInterface;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use App\Shop\Cities\City;
10
use Illuminate\Support\Collection;
11
12
class CityRepository extends BaseRepository implements CityRepositoryInterface
13
{
14
    /**
15
     * CityRepository constructor.
16
     *
17
     * @param City $city
18
     */
19
    public function __construct(City $city)
20
    {
21
        parent::__construct($city);
22
        $this->model = $city;
23
    }
24
25
    /**
26
     * @param array $columns
27
     * @param string $orderBy
28
     * @param string $sortBy
29
     *
30
     * @return mixed
31
     */
32
    public function listCities($columns = ['*'], string $orderBy = 'name', string $sortBy = 'asc')
33
    {
34
        return $this->all($columns, $orderBy, $sortBy);
35
    }
36
37
    /**
38
     * @param int $id
39
     * @return City
40
     * @throws CityNotFoundException
41
     *
42
     * @deprecated @findCityByName
43
     */
44
    public function findCityById(int $id) : City
45
    {
46
        try {
47
            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\Cities\City.
Loading history...
48
        } catch (ModelNotFoundException $e) {
49
            throw new CityNotFoundException('City not found.');
50
        }
51
    }
52
53
    /**
54
     * @param array $params
55
     *
56
     * @return boolean
57
     */
58
    public function updateCity(array $params) : bool
59
    {
60
        $this->model->update($params);
61
        return $this->model->save();
62
    }
63
64
    /**
65
     * @param string $state_code
66
     *
67
     * @return Collection
68
     */
69
    public function listCitiesByStateCode(string $state_code) : Collection
70
    {
71
        return $this->model->where(compact('state_code'))->get();
72
    }
73
74
    /**
75
     * @param string $name
76
     *
77
     * @return mixed
78
     * @throws CityNotFoundException
79
     */
80
    public function findCityByName(string $name) : City
81
    {
82
        try {
83
            return $this->model->where(compact('name'))->firstOrFail();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->model->whe...'name'))->firstOrFail() returns the type Illuminate\Database\Eloquent\Model which includes types incompatible with the type-hinted return App\Shop\Cities\City.
Loading history...
84
        } catch (ModelNotFoundException $e) {
85
            throw new CityNotFoundException('City not found.');
86
        }
87
    }
88
}
89