SoldUnitsPropertyBuilder   A
last analyzed

Complexity

Total Complexity 3

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 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A consumeEvent() 0 13 2
A __construct() 0 4 1
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\Repository\OrderItemRepositoryInterface;
14
use Elastica\Document;
15
use FOS\ElasticaBundle\Event\PostTransformEvent;
16
use Sylius\Component\Core\Model\ProductInterface;
17
18
final class SoldUnitsPropertyBuilder extends AbstractBuilder
19
{
20
    /** @var OrderItemRepositoryInterface */
21
    private $orderItemRepository;
22
23
    /** @var string */
24
    private $soldUnitsProperty;
25
26
    public function __construct(OrderItemRepositoryInterface $orderItemRepository, string $soldUnitsProperty)
27
    {
28
        $this->orderItemRepository = $orderItemRepository;
29
        $this->soldUnitsProperty = $soldUnitsProperty;
30
    }
31
32
    public function consumeEvent(PostTransformEvent $event): void
33
    {
34
        $this->buildProperty(
35
            $event,
36
            ProductInterface::class,
37
            function (ProductInterface $product, Document $document): void {
38
                $soldUnits = 0;
39
40
                foreach ($product->getVariants() as $productVariant) {
41
                    $soldUnits += $this->orderItemRepository->countByVariant($productVariant);
42
                }
43
44
                $document->set($this->soldUnitsProperty, $soldUnits);
45
            }
46
        );
47
    }
48
}
49