ResourceIndexListener::updateIndex()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 14
rs 9.6111
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\EventListener;
12
13
use BitBag\SyliusElasticsearchPlugin\Refresher\ResourceRefresherInterface;
14
use Sylius\Component\Resource\Model\ResourceInterface;
15
use Symfony\Component\EventDispatcher\GenericEvent;
16
use Webmozart\Assert\Assert;
17
18
final class ResourceIndexListener implements ResourceIndexListenerInterface
19
{
20
    /** @var ResourceRefresherInterface */
21
    private $resourceRefresher;
22
23
    /** @var array */
24
    private $persistersMap;
25
26
    public function __construct(ResourceRefresherInterface $resourceRefresher, array $persistersMap)
27
    {
28
        $this->resourceRefresher = $resourceRefresher;
29
        $this->persistersMap = $persistersMap;
30
    }
31
32
    public function updateIndex(GenericEvent $event): void
33
    {
34
        $resource = $event->getSubject();
35
36
        Assert::isInstanceOf($resource, ResourceInterface::class);
37
        foreach ($this->persistersMap as $config) {
38
            $method = $config[self::GET_PARENT_METHOD_KEY] ?? null;
39
40
            if (null !== $method && method_exists($resource, $method)) {
41
                $resource = $resource->$method();
42
            }
43
44
            if ($resource instanceof $config[self::MODEL_KEY]) {
45
                $this->resourceRefresher->refresh($resource, $config[self::SERVICE_ID_KEY]);
46
            }
47
        }
48
    }
49
}
50