ItemVisitedSubscriber::addScript()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStrandsPlugin\EventListener;
6
7
use Setono\SyliusStrandsPlugin\Tag\Tags;
8
use Setono\TagBag\Tag\TagInterface;
9
use Setono\TagBag\Tag\TwigTag;
10
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
11
use Sylius\Component\Product\Model\ProductInterface;
12
13
final class ItemVisitedSubscriber extends TagSubscriber
14
{
15
    public static function getSubscribedEvents(): array
16
    {
17
        return [
18
            'sylius.product.show' => [
19
                'addScript',
20
            ],
21
        ];
22
    }
23
24
    public function addScript(ResourceControllerEvent $event): void
25
    {
26
        $product = $event->getSubject();
27
28
        if (!$product instanceof ProductInterface) {
29
            return;
30
        }
31
32
        $this->tagBag->addTag(
33
            (new TwigTag('@SetonoSyliusStrandsPlugin/Tag/item_visited.js.twig', ['product' => $product]))
34
                ->setSection(TagInterface::SECTION_BODY_END)
35
                ->setName(Tags::TAG_ITEM_VISITED)
36
        );
37
    }
38
}
39