Completed
Push — master ( 5d49af...15d463 )
by Jeff
06:07
created

CourierRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 17
dl 0
loc 81
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A listCouriers() 0 3 1
A findCourierById() 0 6 2
A deleteCourier() 0 3 1
A createCourier() 0 6 2
A updateCourier() 0 6 2
A __construct() 0 4 1
1
<?php
2
3
namespace App\Shop\Couriers\Repositories;
4
5
use Jsdecena\Baserepo\BaseRepository;
6
use App\Shop\Couriers\Courier;
7
use App\Shop\Couriers\Exceptions\CourierInvalidArgumentException;
8
use App\Shop\Couriers\Exceptions\CourierNotFoundException;
9
use App\Shop\Couriers\Repositories\Interfaces\CourierRepositoryInterface;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Illuminate\Database\QueryException;
12
use Illuminate\Support\Collection;
13
14
class CourierRepository extends BaseRepository implements CourierRepositoryInterface
15
{
16
    /**
17
     * CourierRepository constructor.
18
     * @param Courier $courier
19
     */
20
    public function __construct(Courier $courier)
21
    {
22
        parent::__construct($courier);
23
        $this->model = $courier;
24
    }
25
26
    /**
27
     * Create the courier
28
     *
29
     * @param array $params
30
     * @return Courier
31
     * @throws CourierInvalidArgumentException
32
     */
33
    public function createCourier(array $params) : Courier
34
    {
35
        try {
36
            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\Couriers\Courier.
Loading history...
37
        } catch (QueryException $e) {
38
            throw new CourierInvalidArgumentException($e->getMessage());
39
        }
40
    }
41
42
    /**
43
     * Update the courier
44
     *
45
     * @param array $params
46
     *
47
     * @return bool
48
     * @throws CourierInvalidArgumentException
49
     */
50
    public function updateCourier(array $params) : bool
51
    {
52
        try {
53
            return $this->update($params);
54
        } catch (QueryException $e) {
55
            throw new CourierInvalidArgumentException($e->getMessage());
56
        }
57
    }
58
59
    /**
60
     * Return the courier
61
     *
62
     * @param int $id
63
     *
64
     * @return Courier
65
     * @throws CourierNotFoundException
66
     */
67
    public function findCourierById(int $id) : Courier
68
    {
69
        try {
70
            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\Couriers\Courier.
Loading history...
71
        } catch (ModelNotFoundException $e) {
72
            throw new CourierNotFoundException('Courier not found.');
73
        }
74
    }
75
76
    /**
77
     * Return all the couriers
78
     *
79
     * @param string $order
80
     * @param string $sort
81
     * @return Collection|mixed
82
     */
83
    public function listCouriers(string $order = 'id', string $sort = 'desc') : Collection
84
    {
85
        return $this->all(['*'], $order, $sort);
86
    }
87
88
    /**
89
     * @return bool
90
     * @throws \Exception
91
     */
92
    public function deleteCourier()
93
    {
94
        return $this->delete();
95
    }
96
}
97