Completed
Push — master ( c76df9...ef3d70 )
by Jeff
18:25
created

OrderRepository::buildOrderDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

177
        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...
178
            $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...
179
            $product->sku = $product->pivot->product_sku;
180
            $product->description = $product->pivot->product_description;
181
            $product->price = $product->pivot->product_price;
182
            $product->quantity = $product->pivot->quantity;
183
            return $product;
184
        });
185
    }
186
187
    /**
188
     * @param Collection $items
189
     */
190
    public function buildOrderDetails(Collection $items)
191
    {
192
        $items->each(function ($item) {
193
            $productRepo = new ProductRepository(new Product);
194
            $product = $productRepo->find($item->id);
195
            $this->associateProduct($product, $item->qty);
196
        });
197
    }
198
}
199