ProductController::viewAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
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());
0 ignored issues
show
Bug introduced by
It seems like getMeta() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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(),
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
76
        ]));
77
    }
78
    
79
    private function updatePopularity(Product $product)
80
    {
81
        $product->increasePopularity();
82
        $this->getEntityManager()->flush();
83
    }
84
}
85