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

ChannelPricingBuilder::buildProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A ChannelPricingBuilder::consumeEvent() 0 11 2
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 BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
16
use Elastica\Document;
17
use FOS\ElasticaBundle\Event\TransformEvent;
18
use Sylius\Component\Core\Model\ProductInterface;
19
use Sylius\Component\Core\Model\ProductVariantInterface;
20
21
final class ChannelPricingBuilder extends AbstractBuilder
22
{
23
    /**
24
     * @var ConcatedNameResolverInterface
25
     */
26
    private $channelPricingNameResolver;
27
28
    /**
29
     * @param ConcatedNameResolverInterface $channelPricingNameResolver
30
     */
31
    public function __construct(ConcatedNameResolverInterface $channelPricingNameResolver)
32
    {
33
        $this->channelPricingNameResolver = $channelPricingNameResolver;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function consumeEvent(TransformEvent $event): void
40
    {
41
        $this->buildProperty($event, ProductInterface::class,
42
            function (ProductInterface $product, Document $document): void {
43
                /** @var ProductVariantInterface $productVariant */
44
                $productVariant = $product->getVariants()->first();
45
46
                foreach ($productVariant->getChannelPricings() as $channelPricing) {
47
                    $propertyName = $this->channelPricingNameResolver->resolvePropertyName($channelPricing->getChannelCode());
48
49
                    $document->set($propertyName, $channelPricing->getPrice());
50
                }
51
            });
52
    }
53
}
54