Completed
Pull Request — master (#578)
by Kévin
04:31 queued 40s
created

DataProviderPass::registerDataProviders()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 6
Ratio 50 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 12
rs 9.2
cc 4
eloc 8
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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 ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
/**
19
 * Registers data providers.
20
 *
21
 * @internal
22
 *
23
 * @author Kévin Dunglas <[email protected]>
24
 */
25
final class DataProviderPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function process(ContainerBuilder $container)
31
    {
32
        $this->registerDataProviders($container, 'collection');
33
        $this->registerDataProviders($container, 'item');
34
    }
35
36
    private function registerDataProviders(ContainerBuilder $container, string $type) {
37
        $sortedServices = [];
38 View Code Duplication
        foreach ($container->findTaggedServiceIds('api_platform.'.$type.'_data_provider') as $serviceId => $tags) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
            foreach ($tags as $tag) {
40
                $priority = isset($tag['priority']) ? $tag['priority'] : 0;
41
                $sortedServices[$priority][] = new Reference($serviceId);
42
            }
43
        }
44
        krsort($sortedServices);
45
46
        $container->getDefinition('api_platform.'.$type.'_data_provider')->addArgument(call_user_func_array('array_merge', $sortedServices));
47
    }
48
}
49