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); |
|
|
|
|
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
|
|
|
|
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):