Passed
Push — master ( 59f96f...9edaa9 )
by Mikołaj
05:36
created

ChannelsBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildProperty() 0 16 3
A __construct() 0 3 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag. 
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project. 
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected]. 
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
14
15
use FOS\ElasticaBundle\Event\TransformEvent;
16
use Sylius\Component\Core\Model\ProductInterface;
17
18
final class ChannelsBuilder extends AbstractBuilder
19
{
20
    /**
21
     * @var string
22
     */
23
    private $channelsProperty;
24
25
    /**
26
     * @param string $channelsProperty
27
     */
28
    public function __construct(string $channelsProperty)
29
    {
30
        $this->channelsProperty = $channelsProperty;
31
    }
32
33
    /**
34
     * @param TransformEvent $event
35
     */
36
    public function buildProperty(TransformEvent $event): void
37
    {
38
        $product = $event->getObject();
39
40
        if (!$product instanceof ProductInterface) {
41
            return;
42
        }
43
44
        $channels = [];
45
46
        /** @var ProductInterface $product */
47
        foreach ($product->getChannels() as $channel) {
48
            $channels[] = $channel->getCode();
49
        }
50
51
        $event->getDocument()->set($this->channelsProperty, $channels);
52
    }
53
}
54