This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
OrderController.php
114 lines (96 loc) · 3.25 KB
/
OrderController.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
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\OrderBundle\Controller;
use Sonata\Component\Customer\CustomerInterface;
use Sonata\Component\Order\OrderElementInterface;
use Sonata\Component\Order\OrderInterface;
use Sonata\Component\Order\OrderManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class OrderController extends Controller
{
/**
* @throws AccessDeniedException
*
* @return Response
*/
public function indexAction()
{
$user = $this->getUser();
if (!$user) {
throw new AccessDeniedException();
}
$orders = $this->getOrderManager()->findForUser($user, ['createdAt' => 'DESC']);
$this->get('sonata.seo.page')->setTitle($this->get('translator')->trans('order_index_title', [], 'SonataOrderBundle'));
return $this->render('@SonataOrder/Order/index.html.twig', [
'orders' => $orders,
'breadcrumb_context' => 'user_order',
]);
}
/**
* @param string $reference
*
* @throws AccessDeniedException
*
* @return Response
*/
public function viewAction($reference)
{
/** @var OrderInterface $order */
$order = $this->getOrderManager()->findOneBy(['reference' => $reference]);
if (null === $order) {
throw new AccessDeniedException();
}
$this->checkAccess($order->getCustomer());
$this->get('sonata.seo.page')->setTitle($this->get('translator')->trans('order_view_title', [], 'SonataOrderBundle'));
/** @var OrderElementInterface $element */
foreach ($order->getOrderElements() as $element) {
$provider = $this->get('sonata.product.pool')->getProvider($element->getProductType());
$element->setProduct($provider->getProductFromRaw($element, $this->get('sonata.product.pool')->getManager($element->getProductType())->getClass()));
}
return $this->render('@SonataOrder/Order/view.html.twig', [
'order' => $order,
'breadcrumb_context' => 'user_order',
]);
}
/**
* @param unknown $reference
*
* @throws \RuntimeException
*/
public function downloadAction($reference): void
{
throw new \RuntimeException('not implemented');
}
/**
* Checks that the current logged in user has access to given invoice.
*
* @param CustomerInterface $customer The linked customer
*
* @throws AccessDeniedException
*/
protected function checkAccess(CustomerInterface $customer): void
{
if (!($user = $this->getUser())
|| !$customer->getUser()
|| $customer->getUser()->getId() !== $user->getId()) {
throw new AccessDeniedException();
}
}
/**
* @return OrderManagerInterface
*/
protected function getOrderManager()
{
return $this->get('sonata.order.manager');
}
}