Test Failed
Pull Request — master (#198)
by
unknown
14:31 queued 06:23
created

OrderRepository::getCouriers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace App\Shop\Orders\Repositories;
4
5
use App\Shop\Carts\Repositories\CartRepository;
6
use App\Shop\Carts\ShoppingCart;
7
use Gloudemans\Shoppingcart\Facades\Cart;
8
use Jsdecena\Baserepo\BaseRepository;
9
use App\Shop\Employees\Employee;
10
use App\Shop\Employees\Repositories\EmployeeRepository;
11
use App\Events\OrderCreateEvent;
12
use App\Mail\sendEmailNotificationToAdminMailable;
13
use App\Mail\SendOrderToCustomerMailable;
14
use App\Shop\Orders\Exceptions\OrderInvalidArgumentException;
15
use App\Shop\Orders\Exceptions\OrderNotFoundException;
16
use App\Shop\Addresses\Address;
17
use App\Shop\Couriers\Courier;
18
use App\Shop\Orders\Order;
19
use App\Shop\Orders\Repositories\Interfaces\OrderRepositoryInterface;
20
use App\Shop\Orders\Transformers\OrderTransformable;
21
use App\Shop\Products\Product;
22
use App\Shop\Products\Repositories\ProductRepository;
23
use Illuminate\Database\Eloquent\ModelNotFoundException;
24
use Illuminate\Database\QueryException;
25
use Illuminate\Support\Collection;
26
use Illuminate\Support\Facades\Mail;
27
28
class OrderRepository extends BaseRepository implements OrderRepositoryInterface
29
{
30
    use OrderTransformable;
0 ignored issues
show
introduced by
The trait App\Shop\Orders\Transformers\OrderTransformable requires some properties which are not provided by App\Shop\Orders\Repositories\OrderRepository: $courier_id, $customer_id, $address_id, $order_status_id
Loading history...
31
32
    /**
33
     * OrderRepository constructor.
34
     * @param Order $order
35
     */
36
    public function __construct(Order $order)
37
    {
38
        parent::__construct($order);
39
        $this->model = $order;
40
    }
41
42
    /**
43
     * Create the order
44
     *
45
     * @param array $params
46
     * @return Order
47
     * @throws OrderInvalidArgumentException
48
     */
49
    public function createOrder(array $params) : Order
50
    {
51
        try {
52
53
            $order = $this->create($params);
54
55
            $orderRepo = new OrderRepository($order);
56
            $orderRepo->buildOrderDetails(Cart::content());
57
58
            event(new OrderCreateEvent($order));
59
60
            return $order;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $order returns the type Illuminate\Database\Eloquent\Model which includes types incompatible with the type-hinted return App\Shop\Orders\Order.
Loading history...
61
        } catch (QueryException $e) {
62
            throw new OrderInvalidArgumentException($e->getMessage(), 500, $e);
63
        }
64
    }
65
66
    /**
67
     * @param array $params
68
     *
69
     * @return bool
70
     * @throws OrderInvalidArgumentException
71
     */
72
    public function updateOrder(array $params) : bool
73
    {
74
        try {
75
            return $this->update($params);
76
        } catch (QueryException $e) {
77
            throw new OrderInvalidArgumentException($e->getMessage());
78
        }
79
    }
80
81
    /**
82
     * @param int $id
83
     * @return Order
84
     * @throws OrderNotFoundException
85
     */
86
    public function findOrderById(int $id) : Order
87
    {
88
        try {
89
            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\Orders\Order.
Loading history...
90
        } catch (ModelNotFoundException $e) {
91
            throw new OrderNotFoundException($e);
92
        }
93
    }
94
95
96
    /**
97
     * Return all the orders
98
     *
99
     * @param string $order
100
     * @param string $sort
101
     * @param array $columns
102
     * @return Collection
103
     */
104
    public function listOrders(string $order = 'id', string $sort = 'desc', array $columns = ['*']) : Collection
105
    {
106
        return $this->all($columns, $order, $sort);
107
    }
108
109
    /**
110
     * @param Order $order
111
     * @return mixed
112
     */
113
    public function findProducts(Order $order) : Collection
114
    {
115
        return $order->products;
116
    }
117
118
    /**
119
     * @param Product $product
120
     * @param int $quantity
121
     * @param array $data
122
     */
123
    public function associateProduct(Product $product, int $quantity = 1, array $data = [])
124
    {
125
        $this->model->products()->attach($product, [
126
            'quantity' => $quantity,
127
            'product_name' => $product->name,
128
            'product_sku' => $product->sku,
129
            'product_description' => $product->description,
130
            'product_price' => $product->price,
131
            'product_attribute_id' => isset($data['product_attribute_id']) ? $data['product_attribute_id']: null,
132
        ]);
133
        $product->quantity = ($product->quantity - $quantity);
134
        $product->save();
135
    }
136
137
    /**
138
     * Send email to customer
139
     */
140
    public function sendEmailToCustomer()
141
    {
142
        Mail::to($this->model->customer)
143
            ->send(new SendOrderToCustomerMailable($this->findOrderById($this->model->id)));
144
    }
145
146
    /**
147
     * Send email notification to the admin
148
     */
149
    public function sendEmailNotificationToAdmin()
150
    {
151
        $employeeRepo = new EmployeeRepository(new Employee);
152
        $employee = $employeeRepo->findEmployeeById(1);
153
154
        Mail::to($employee)
155
            ->send(new sendEmailNotificationToAdminMailable($this->findOrderById($this->model->id)));
156
    }
157
158
    /**
159
     * @param string $text
160
     * @return mixed
161
     */
162
    public function searchOrder(string $text) : Collection
163
    {
164
        if (!empty($text)) {
165
            return $this->model->searchForOrder($text)->get();
166
        } else {
167
            return $this->listOrders();
168
        }
169
    }
170
171
    /**
172
     * @return Order
173
     */
174
    public function transform()
175
    {
176
        return $this->transformOrder($this->model);
177
    }
178
179
    /**
180
     * @return Collection
181
     */
182
    public function listOrderedProducts() : Collection
183
    {
184
        return $this->model->products->map(function (Product $product) {
0 ignored issues
show
Bug introduced by
The method map() does not exist on null. ( Ignorable by Annotation )

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

184
        return $this->model->products->/** @scrutinizer ignore-call */ map(function (Product $product) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
185
            $product->name = $product->pivot->product_name;
0 ignored issues
show
Bug introduced by
The property pivot does not seem to exist on App\Shop\Products\Product. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
186
            $product->sku = $product->pivot->product_sku;
187
            $product->description = $product->pivot->product_description;
188
            $product->price = $product->pivot->product_price;
189
            $product->quantity = $product->pivot->quantity;
190
            $product->product_attribute_id = $product->pivot->product_attribute_id;
0 ignored issues
show
Bug introduced by
The property product_attribute_id does not seem to exist on App\Shop\Products\Product. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
191
            return $product;
192
        });
193
    }
194
195
    /**
196
     * @param Collection $items
197
     */
198
    public function buildOrderDetails(Collection $items)
199
    {
200
        $items->each(function ($item) {
201
            $productRepo = new ProductRepository(new Product);
202
            $product = $productRepo->find($item->id);
203
            if ($item->options->has('product_attribute_id')) {
204
                $this->associateProduct($product, $item->qty, [
205
                    'product_attribute_id' => $item->options->product_attribute_id
206
                ]);
207
            } else {
208
                $this->associateProduct($product, $item->qty);
209
            }
210
        });
211
    }
212
213
    /**
214
     * @return Collection $addresses
215
     */
216
    public function getAddresses() : Collection
217
    {
218
        $addresses = $this->model->first()->address()->get();
219
        return $addresses;
220
    }
221
222
    /**
223
     * @return Collection $couriers
224
     */
225
    public function getCouriers() : Collection
226
    {
227
        $couriers = $this->model->first()->courier()->get();
228
        return $couriers;
229
    }
230
}
231