ChannelPricingBuilder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 27
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A consumeEvent() 0 17 3
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;
12
13
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
14
use Elastica\Document;
15
use FOS\ElasticaBundle\Event\PostTransformEvent;
16
use Sylius\Component\Core\Model\ProductInterface;
17
use Sylius\Component\Core\Model\ProductVariantInterface;
18
19
final class ChannelPricingBuilder extends AbstractBuilder
20
{
21
    /** @var ConcatedNameResolverInterface */
22
    private $channelPricingNameResolver;
23
24
    public function __construct(ConcatedNameResolverInterface $channelPricingNameResolver)
25
    {
26
        $this->channelPricingNameResolver = $channelPricingNameResolver;
27
    }
28
29
    public function consumeEvent(PostTransformEvent $event): void
30
    {
31
        $this->buildProperty(
32
            $event,
33
            ProductInterface::class,
34
            function (ProductInterface $product, Document $document): void {
35
                if (0 === $product->getVariants()->count()) {
36
                    return;
37
                }
38
39
                /** @var ProductVariantInterface $productVariant */
40
                $productVariant = $product->getVariants()->first();
41
42
                foreach ($productVariant->getChannelPricings() as $channelPricing) {
43
                    $propertyName = $this->channelPricingNameResolver->resolvePropertyName($channelPricing->getChannelCode());
44
45
                    $document->set($propertyName, $channelPricing->getPrice());
46
                }
47
            }
48
        );
49
    }
50
}
51