LastViewedBoxController::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
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\LastViewedBundle\Controller\Box;
14
15
use Symfony\Component\HttpFoundation\Response;
16
use WellCommerce\Bundle\CatalogBundle\Entity\Product;
17
use WellCommerce\Bundle\CoreBundle\Controller\Box\AbstractBoxController;
18
use WellCommerce\Bundle\LastViewedBundle\Entity\LastViewed;
19
use WellCommerce\Bundle\LastViewedBundle\Manager\LastViewedManager;
20
use WellCommerce\Component\DataSet\Conditions\Condition\In;
21
use WellCommerce\Component\DataSet\Conditions\ConditionsCollection;
22
use WellCommerce\Component\Layout\Collection\LayoutBoxSettingsCollection;
23
24
/**
25
 * Class LastViewedBoxController
26
 *
27
 * @author  Adam Piotrowski <[email protected]>
28
 */
29
class LastViewedBoxController extends AbstractBoxController
30
{
31
    /**
32
     * @var LastViewedManager
33
     */
34
    protected $manager;
35
    
36
    public function indexAction(LayoutBoxSettingsCollection $boxSettings): Response
37
    {
38
        $dataset        = $this->get('product.dataset.front');
39
        $currentProduct = $this->getProductStorage()->getCurrentProduct();
40
        
41
        $products = $dataset->getResult('array', [
42
            'limit'      => $boxSettings->getParam('limit', 4),
43
            'page'       => 1,
44
            'order_by'   => 'hierarchy',
45
            'order_dir'  => 'asc',
46
            'conditions' => $this->createConditionsCollection($currentProduct),
47
        ]);
48
        
49
        return $this->displayTemplate('index', [
50
            'dataset'     => $products,
51
            'boxSettings' => $boxSettings,
52
        ]);
53
    }
54
    
55
    protected function createConditionsCollection(Product $product = null): ConditionsCollection
56
    {
57
        $identifiers        = [];
58
        $client             = $this->getSecurityHelper()->getCurrentClient();
59
        $sessionId          = $this->getRequestHelper()->getSessionId();
60
        $lastViewedProducts = $this->manager->getLastViewedProducts($client, $sessionId, $product);
0 ignored issues
show
Bug introduced by
It seems like $product defined by parameter $product on line 55 can be null; however, WellCommerce\Bundle\Last...getLastViewedProducts() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
61
        $lastViewedProducts->map(function (LastViewed $lastViewed) use (&$identifiers, $product) {
62
            $identifiers[] = $lastViewed->getProduct()->getId();
63
        });
64
        
65
        if (0 === count($identifiers)) {
66
            $identifiers = [0];
67
        }
68
        
69
        $conditions = new ConditionsCollection();
70
        $conditions->add(new In('id', $identifiers));
71
        
72
        return $conditions;
73
    }
74
}
75