-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderService.php
228 lines (196 loc) · 6.21 KB
/
OrderService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace AppBundle\Services;
use Doctrine\ORM\EntityManager;
use AppBundle\Entity\State;
use AppBundle\Entity\Forder;
use Symfony\Component\Config\Definition\Exception\Exception;
use AppBundle\Utils\QueryFilter;
use AppBundle\Utils\Traits\ServiceDataPersistenceTrait;
include('Constants/OrderServiceConstants.php');
class OrderService
{
protected $entityManager;
protected $user;
protected $knp_paginator;
use ServiceDataPersistenceTrait;
/**
* Order Service constructor.
*
* @param EntityManager $entityManager
* @param $user represents the current user
*/
public function __construct(EntityManager $entityManager, $user)
{
$this->entityManager = $entityManager;
$this->user = $user;
}
/**
* Creates a new order
*
* @param Forder $forder
*/
public function create(Forder $forder)
{
//Initial Values
$state = $this->entityManager->getRepository('AppBundle:state')->find(State::ACTIVE);
$forder->setState($state);
$forder->setUser($this->user);
$forder->setCreatedAt(new \DateTime("now"));
$this->save($forder);
}
/**
* Changes the order state to ready
*
* @param Forder $forder
*/
public function makeReady(Forder $forder){
$this->changeOrderState($forder, State::READY);
$this->save($forder);
}
/**
* Changes the order state to waiting
*
* @param Forder $forder
*/
public function makeWaiting(Forder $forder){
$this->changeOrderState($forder, State::WAITING);
$forder->setCalledAt(new \DateTime("now"));
$this->save($forder);
}
/**
* Changes the order state to delivered
*
* @param Forder $forder
* @param float $price
*
* @throws exception if price is not number
* @throws exception if price out of range
*/
public function makeDelivered(Forder $forder, float $price){
$this->changeOrderState($forder, State::DELIVERED);
if(is_numeric($price) == false){
throw new Exception("Price should be a number");
}
if($price < 0 || $price > 100000){
throw new Exception("Please enter a number between 0 and 100,000");
}
$forder->setDeliveredAt(new \DateTime("now"));
$forder->setPrice($price);
$this->save($forder);
}
/**
* Changes the order state to complete
*
* @param Forder $forder
*/
public function makeComplete(Forder $forder){
$this->changeOrderState($forder, State::COMPLETE);
$forder->setCompletedAt(new \DateTime("now"));
$this->save($forder);
}
/**
* Changes the order state to a specif state
* This is a genral function called by all other functions that change the state
*
* @param Forder $forder
* @param int $StateID
*
* @throws exception if user is not the order creator
* @throws exception if there is no items
*/
public function changeOrderState(Forder $forder, int $StateID){
//Validation: User should be the creator of the restaurant
if($this->user->getID() != $forder->getUser()->getID()){
throw new Exception("You're not allowed to update this order!");
}
//Validation: Order couldn't be empty of items
if ($forder->getItems()->count() == 0){
throw new Exception("No enough items!");
}
$state = $this->entityManager->getRepository('AppBundle:State')->find($StateID);
$forder->setState($state);
}
/**
* checks if Order is active
*
* @param Forder $forder
*
* @return bool
*/
public function isActive(Forder $forder){
return $forder->getState()->getID() == State::ACTIVE;
}
/**
* gets the count of orders with some crietria
*
* @param string $section
* @param int $start = 1
* @param array $urlFilter = null
*
* @return int
*/
public function getOrdersCount(string $section, int $start = 1, array $urlFilter = null){
$filter = $this->getQueryFilterArray($section, $urlFilter, $this->user->getID())->getSQLFilter();
return $this->entityManager->getRepository('AppBundle:Forder')->getCount($filter);
}
/**
* gets the orders with some crietria
*
* @param string $section
* @param int $start = 1
* @param array $urlFilter = null
*
* @return group of orders
*/
public function getOrders($section, $start = 1, $urlFilter = null){
$forders = $this->entityManager->getRepository('AppBundle:Forder')->findBy(
$this->getQueryFilterArray($section, $urlFilter, $this->user->getID())->getArray() ,
$this->getQuerySortArray(),
ORDERS_PER_PAGE,
($start-1) * ORDERS_PER_PAGE
);
return $forders;
}
/**
* gets doctrine filter from a url filter
*
* @param string $section
* @param array $urlFilter = null
* @param int $userID = null
*
* @return queryFilter Object
*/
static function getQueryFilterArray(string $section, array $urlFilter = null, int $userID = null){
$queryFilter = new QueryFilter;
if( in_array($section, CURRENT_ORDERS_ROUTES_ARRAY) ){
$queryFilter->addFilter('state', CURRENT_ORDERS_STATES_ARRAY);
}elseif( in_array($section, HISTORY_ORDERS_ROUTES_ARRAY) ){
$queryFilter->addFilter('state', HISTORY_ORDERS_STATES_ARRAY);
}
if(isset($urlFilter['myorders']) && intval($urlFilter['myorders']) === 1)
$queryFilter->addFilter('user', $userID);
if(isset($urlFilter['restaurant']))
$queryFilter->addFilter('restaurant', $urlFilter['restaurant']);
if(isset($urlFilter['state']))
$queryFilter->addFilter('state', $urlFilter['state']);
return $queryFilter;
}
/**
* Gets the sorting array
*
* @return array to be used by doctrine
*/
private function getQuerySortArray(){
return array('id' => 'DESC');
}
/**
* Returns any service constant to any external consumer
*
* @param string $constant
*
* @return the value of the corresponding constant
*/
public function getServiceConstant(string $constant){
return constant($constant);
}
}