1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\CatalogBundle\Controller\Front; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use WellCommerce\Bundle\CatalogBundle\Entity\Category; |
18
|
|
|
use WellCommerce\Bundle\CatalogBundle\Entity\Product; |
19
|
|
|
use WellCommerce\Bundle\CatalogBundle\Event\ProductViewedEvent; |
20
|
|
|
use WellCommerce\Bundle\CoreBundle\Controller\Front\AbstractFrontController; |
21
|
|
|
use WellCommerce\Component\Breadcrumb\Model\Breadcrumb; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class ProductController |
25
|
|
|
* |
26
|
|
|
* @author Adam Piotrowski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ProductController extends AbstractFrontController |
29
|
|
|
{ |
30
|
|
|
public function indexAction(Product $product = null): Response |
31
|
|
|
{ |
32
|
|
|
if (!$product instanceof Product || $product->getCategories()->isEmpty()) { |
33
|
|
|
return $this->redirectToRoute('front.home_page.index'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->addBreadcrumbs($product); |
37
|
|
|
$this->getProductStorage()->setCurrentProduct($product); |
38
|
|
|
$this->updatePopularity($product); |
39
|
|
|
$this->getMetadataHelper()->setMetadata($product->translate()->getMeta()); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$this->getEventDispatcher()->dispatch(ProductViewedEvent::EVENT_NAME, new ProductViewedEvent($product)); |
42
|
|
|
|
43
|
|
|
return $this->displayTemplate('index', [ |
44
|
|
|
'product' => $product, |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function viewAction(Product $product): JsonResponse |
49
|
|
|
{ |
50
|
|
|
$this->getProductStorage()->setCurrentProduct($product); |
51
|
|
|
|
52
|
|
|
$templateData = $this->get('product.helper')->getProductDefaultTemplateData($product); |
53
|
|
|
$basketModalContent = $this->renderView('WellCommerceCatalogBundle:Front/Product:view.html.twig', $templateData); |
54
|
|
|
|
55
|
|
|
return $this->jsonResponse([ |
56
|
|
|
'basketModalContent' => $basketModalContent, |
57
|
|
|
'templateData' => $templateData, |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function addBreadcrumbs(Product $product) |
62
|
|
|
{ |
63
|
|
|
$category = $product->getCategories()->last(); |
64
|
|
|
$paths = $this->get('category.repository')->getCategoryPath($category); |
65
|
|
|
|
66
|
|
|
/** @var Category $path */ |
67
|
|
|
foreach ($paths as $path) { |
68
|
|
|
$this->getBreadcrumbProvider()->add(new Breadcrumb([ |
69
|
|
|
'label' => $path->translate()->getName(), |
70
|
|
|
'url' => $this->getRouterHelper()->generateUrl($path->translate()->getRoute()->getId()), |
71
|
|
|
])); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->getBreadcrumbProvider()->add(new Breadcrumb([ |
75
|
|
|
'label' => $product->translate()->getName(), |
|
|
|
|
76
|
|
|
])); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function updatePopularity(Product $product) |
80
|
|
|
{ |
81
|
|
|
$product->increasePopularity(); |
82
|
|
|
$this->getEntityManager()->flush(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.