Completed
Push — master ( 6b04dc...e5fb04 )
by Paweł
08:58
created

SyliusReviewExtension::createReviewListeners()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ReviewBundle\DependencyInjection;
13
14
use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension;
15
use Sylius\Bundle\ReviewBundle\EventListener\ReviewChangeListener;
16
use Sylius\Bundle\ReviewBundle\Updater\AverageRatingUpdater;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
/**
24
 * @author Mateusz Zalewski <[email protected]>
25
 * @author Grzegorz Sadowski <[email protected]>
26
 */
27
final class SyliusReviewExtension extends AbstractResourceExtension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(array $config, ContainerBuilder $container)
33
    {
34
        $config = $this->processConfiguration($this->getConfiguration([], $container), $config);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration(array(), $container) is of type object|null, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
36
37
        $this->registerResources('sylius', $config['driver'], $this->resolveResources($config['resources'], $container), $container);
38
39
        $loader->load('services.xml');
40
41
        $loader->load(sprintf('integrations/%s.xml', $config['driver']));
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    private function resolveResources(array $resources, ContainerBuilder $container)
48
    {
49
        $container->setParameter('sylius.review.subjects', $resources);
50
51
        $this->createReviewListeners(array_keys($resources), $container);
52
53
        $resolvedResources = [];
54
        foreach ($resources as $subjectName => $subjectConfig) {
55
            foreach ($subjectConfig as $resourceName => $resourceConfig) {
56
                if (is_array($resourceConfig)) {
57
                    $resolvedResources[$subjectName.'_'.$resourceName] = $resourceConfig;
58
                }
59
            }
60
        }
61
62
        return $resolvedResources;
63
    }
64
65
    /**
66
     * @param array $reviewSubjects
67
     * @param ContainerBuilder $container
68
     */
69
    private function createReviewListeners(array $reviewSubjects, ContainerBuilder $container)
70
    {
71
        foreach ($reviewSubjects as $reviewSubject) {
72
            $reviewChangeListener = new Definition(ReviewChangeListener::class, [
73
                new Reference(sprintf('sylius.%s_review.average_rating_updater', $reviewSubject)),
74
            ]);
75
76
            $reviewChangeListener->addTag('kernel.event_listener', [
77
                'event' => sprintf('sylius.%s_review.post_update', $reviewSubject),
78
                'method' => 'recalculateSubjectRating',
79
            ]);
80
            $reviewChangeListener->addTag('kernel.event_listener', [
81
                'event' => sprintf('sylius.%s_review.post_delete', $reviewSubject),
82
                'method' => 'recalculateSubjectRating',
83
            ]);
84
85
            $container->addDefinitions([
86
                sprintf('sylius.%s_review.average_rating_updater', $reviewSubject) => new Definition(AverageRatingUpdater::class, [
87
                    new Reference('sylius.average_rating_calculator'),
88
                    new Reference(sprintf('sylius.manager.%s_review', $reviewSubject)),
89
                ]),
90
                sprintf('sylius.listener.%s_review_change', $reviewSubject) => $reviewChangeListener,
91
            ]);
92
        }
93
    }
94
}
95