Completed
Push — master ( 5ca964...bf9a0c )
by Mikołaj
09:05
created

ResourceIndexListener::updateIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file has been created by developers from BitBag.
7
 * Feel free to contact us once you face any issues or want to start
8
 * another great project.
9
 * You can find more information about us on https://bitbag.shop and write us
10
 * an email on [email protected].
11
 */
12
13
namespace BitBag\SyliusElasticsearchPlugin\EventListener;
14
15
use BitBag\SyliusElasticsearchPlugin\Refresher\ResourceRefresherInterface;
16
use Sylius\Component\Resource\Model\ResourceInterface;
17
use Symfony\Component\EventDispatcher\GenericEvent;
18
use Webmozart\Assert\Assert;
19
20
final class ResourceIndexListener
21
{
22
    /** @var ResourceRefresherInterface */
23
    private $resourceRefresher;
24
25
    /** @var array */
26
    private $persistersMap;
27
28
    public function __construct(ResourceRefresherInterface $resourceRefresher, array $persistersMap)
29
    {
30
        $this->resourceRefresher = $resourceRefresher;
31
        $this->persistersMap = $persistersMap;
32
    }
33
34
    public function updateIndex(GenericEvent $event): void
35
    {
36
        $resource = $event->getSubject();
37
38
        Assert::isInstanceOf($resource, ResourceInterface::class);
39
40
        foreach ($this->persistersMap as $objectPersisterId => $modelClass) {
41
            if ($modelClass === get_class($resource)) {
42
                $this->resourceRefresher->refresh($resource, $objectPersisterId);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type null; however, parameter $resource of BitBag\SyliusElasticsear...herInterface::refresh() does only seem to accept Sylius\Component\Resource\Model\ResourceInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                $this->resourceRefresher->refresh(/** @scrutinizer ignore-type */ $resource, $objectPersisterId);
Loading history...
43
            }
44
        }
45
    }
46
}
47