Passed
Push — master ( 1a7a28...c3773f )
by Mikołaj
03:50
created

ChannelsBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A consumeEvent() 0 11 2
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 Elastica\Document;
16
use FOS\ElasticaBundle\Event\TransformEvent;
17
use Sylius\Component\Core\Model\ProductInterface;
18
19
final class ChannelsBuilder extends AbstractBuilder
20
{
21
    /**
22
     * @var string
23
     */
24
    private $channelsProperty;
25
26
    /**
27
     * @param string $channelsProperty
28
     */
29
    public function __construct(string $channelsProperty)
30
    {
31
        $this->channelsProperty = $channelsProperty;
32
    }
33
34
    /**
35
     * @param TransformEvent $event
36
     */
37
    public function consumeEvent(TransformEvent $event): void
38
    {
39
        $this->buildProperty($event, ProductInterface::class,
40
            function (ProductInterface $product, Document $document): void {
41
                $channels = [];
42
43
                foreach ($product->getChannels() as $channel) {
44
                    $channels[] = $channel->getCode();
45
                }
46
47
                $document->set($this->channelsProperty, $channels);
48
            }
49
        );
50
    }
51
}
52