Test Failed
Push — master ( 55431f...64d5a8 )
by Jeff
04:55
created

CustomerRepository::isStripeCustomer()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Shop\Customers\Repositories;
4
5
use App\Shop\Addresses\Address;
6
use App\Shop\Base\BaseRepository;
7
use App\Shop\Customers\Customer;
8
use App\Shop\Customers\Exceptions\CreateCustomerInvalidArgumentException;
9
use App\Shop\Customers\Exceptions\CustomerNotFoundException;
10
use App\Shop\Customers\Exceptions\CustomerPaymentChargingErrorException;
11
use App\Shop\Customers\Exceptions\UpdateCustomerInvalidArgumentException;
12
use App\Shop\Customers\Repositories\Interfaces\CustomerRepositoryInterface;
13
use Illuminate\Database\Eloquent\Collection;
14
use Illuminate\Database\Eloquent\ModelNotFoundException;
15
use Illuminate\Database\QueryException;
16
use Illuminate\Support\Collection as Support;
17
18
class CustomerRepository extends BaseRepository implements CustomerRepositoryInterface
19
{
20
    /**
21
     * CustomerRepository constructor.
22
     * @param Customer $customer
23
     */
24
    public function __construct(Customer $customer)
25
    {
26
        parent::__construct($customer);
27
        $this->model = $customer;
28
    }
29
30
    /**
31
     * List all the employees
32
     *
33
     * @param string $order
34
     * @param string $sort
35
     * @param array $columns
36
     * @return \Illuminate\Support\Collection
37
     */
38
    public function listCustomers(string $order = 'id', string $sort = 'desc', array $columns = ['*']) : Support
39
    {
40
        return $this->all($columns, $order, $sort);
41
    }
42
43
    /**
44
     * Create the customer
45
     *
46
     * @param array $params
47
     * @return Customer
48
     * @throws CreateCustomerInvalidArgumentException
49
     */
50
    public function createCustomer(array $params) : Customer
51
    {
52
        try {
53
            $data = collect($params)->except('password')->all();
54
55
            $customer = new Customer($data);
56
            if (isset($params['password'])) {
57
                $customer->password = bcrypt($params['password']);
58
            }
59
60
            $customer->save();
61
62
            return $customer;
63
        } catch (QueryException $e) {
64
            throw new CreateCustomerInvalidArgumentException($e->getMessage(), 500, $e);
65
        }
66
    }
67
68
    /**
69
     * Update the customer
70
     *
71
     * @param array $params
72
     * @return bool
73
     */
74
    public function updateCustomer(array $params) : bool
75
    {
76
        try {
77
            return $this->model->update($params);
78
        } catch (QueryException $e) {
79
            throw new UpdateCustomerInvalidArgumentException('Cannot update customer', 500, $e);
80
        }
81
    }
82
83
    /**
84
     * Find the customer or fail
85
     *
86
     * @param int $id
87
     * @return Customer
88
     */
89
    public function findCustomerById(int $id) : Customer
90
    {
91
        try {
92
            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\Customers\Customer.
Loading history...
93
        } catch (ModelNotFoundException $e) {
94
            throw new CustomerNotFoundException('Cannot find customer', $e);
0 ignored issues
show
Unused Code introduced by
The call to App\Shop\Customers\Excep...xception::__construct() has too many arguments starting with 'Cannot find customer'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
            throw /** @scrutinizer ignore-call */ new CustomerNotFoundException('Cannot find customer', $e);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
95
        }
96
    }
97
98
    /**
99
     * Delete a customer
100
     *
101
     * @return bool
102
     */
103
    public function deleteCustomer() : bool
104
    {
105
        return $this->model->delete();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->model->delete() could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
106
    }
107
108
    /**
109
     * @param Address $address
110
     * @return Address
111
     */
112
    public function attachAddress(Address $address) : Address
113
    {
114
        return $this->model->addresses()->save($address);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->model->addresses()->save($address) could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return App\Shop\Addresses\Address. Consider adding an additional type-check to rule them out.
Loading history...
115
    }
116
117
    /**
118
     * Find the address attached to the customer
119
     *
120
     * @return mixed
121
     */
122
    public function findAddresses() : Support
123
    {
124
        return $this->model->addresses;
125
    }
126
127
    /**
128
     * @return Collection
129
     */
130
    public function findOrders() : Collection
131
    {
132
        return $this->model->orders()->get();
133
    }
134
135
    /**
136
     * @param string $text
137
     * @return mixed
138
     */
139
    public function searchCustomer(string $text) : Collection
140
    {
141
        return $this->model->search($text, ['name' => 10, 'email' => 5])->get();
142
    }
143
144
    /**
145
     * @param int $amount
146
     * @param array $options
147
     * @return \Stripe\Charge
148
     * @throws CustomerPaymentChargingErrorException
149
     */
150
    public function charge(int $amount, array $options)
151
    {
152
        try {
153
            return $this->model->charge($amount * 100, $options);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->model->cha...amount * 100, $options) also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type Stripe\Charge.
Loading history...
154
        } catch (\Exception $e) {
155
            throw new CustomerPaymentChargingErrorException($e);
156
        }
157
    }
158
}
159