-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderController.php
More file actions
91 lines (76 loc) · 2.61 KB
/
OrderController.php
File metadata and controls
91 lines (76 loc) · 2.61 KB
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
<?php
namespace src\controller;
use src\service\order\OrderService;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Class OrderController
* @package src\controller
*/
class OrderController extends AbstractController
{
/** @var OrderService */
protected $orderService;
/**
* OrderController constructor.
* @param ContainerBuilder $container
* @throws \Exception
*/
public function __construct(ContainerBuilder $container)
{
parent::__construct($container);
$this->orderService = $container->get('order');
}
/**
* POST /order/create
*
* @return JsonResponse
* @throws \Doctrine\Common\Persistence\Mapping\MappingException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function actionCreate()
{
if (!$this->auth->checkAuthorization()) {
throw new AccessDeniedHttpException();
}
$productIds = $this->request->getCurrentRequest()->get('products');
if (empty($productIds) || !\is_array($productIds)) {
throw new BadRequestHttpException('Not found products!');
}
$order = $this->orderService->createNewOrder($productIds);
if (!$order) {
throw new BadRequestHttpException($this->orderService->getFirstError());
}
return new JsonResponse([
'id' => $order->getId(),
'status' => $order->getStatus(),
]);
}
/**
* POST /order/pay
*
* @return JsonResponse
*/
public function actionPay()
{
if (!$this->auth->checkAuthorization()) {
throw new AccessDeniedHttpException();
}
$id = $this->request->getCurrentRequest()->get('id');
$sumTotal = $this->request->getCurrentRequest()->get('sumTotal');
if (empty($id) || empty($sumTotal)) {
throw new BadRequestHttpException('Not found products!');
}
$order = $this->orderService->payOrder($id, $sumTotal);
if (!$order) {
throw new BadRequestHttpException($this->orderService->getFirstError());
}
return new JsonResponse([
'id' => $order->getId(),
'status' => $order->getStatus(),
]);
}
}