Passed
Push — main ( 964870...8374cd )
by Daniel
08:40 queued 03:10
created

ApiPlatformCompilerPass::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 25
ccs 0
cts 15
cp 0
crap 12
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[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
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\DependencyInjection\CompilerPass;
15
16
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Silverback\ApiComponentsBundle\EventListener\Api\CollectionApiEventListener;
18
use Silverback\ApiComponentsBundle\EventListener\Doctrine\PurgeHttpCacheListener;
19
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
use Symfony\Component\HttpClient\ScopingHttpClient;
24
25
/**
26
 * @author Daniel West <[email protected]>
27
 */
28
class ApiPlatformCompilerPass implements CompilerPassInterface
29
{
30
    public function process(ContainerBuilder $container): void
31
    {
32
        $itemsPerPageParameterName = $container->getParameter('api_platform.collection.pagination.items_per_page_parameter_name');
33
34
        $container->getDefinition(CollectionApiEventListener::class)->setArgument('$itemsPerPageParameterName', $itemsPerPageParameterName);
35
36
        // Todo: revert to checking for service id api_platform.http_cache.purger when https://github.com/api-platform/core/pull/4695 is merged
37
        if (!$container->hasDefinition('api_platform.http_cache.purger.varnish.xkey')) {
38
            $container->removeDefinition(PurgeHttpCacheListener::class);
39
40
            return;
41
        }
42
43
        // Todo: remove this - API Platform will be implementing this shortly using http client instead of guzzle
44
        $config = $container->getExtensionConfig('api_platform');
45
        $apiPlatformConfig = array_merge(...$config);
46
        $definitions = [];
47
        foreach ($apiPlatformConfig['http_cache']['invalidation']['varnish_urls'] as $key => $url) {
48
            $ops = $apiPlatformConfig['http_cache']['invalidation']['request_options'] ?? [];
49
            $definition = new Definition(ScopingHttpClient::class, [new Reference('http_client'), $url, ['base_uri' => $url] + $ops]);
50
            $definition->setFactory([ScopingHttpClient::class, 'forBaseUri']);
51
52
            $definitions[] = $definition;
53
        }
54
        $container->findDefinition('api_platform.http_cache.purger.varnish.xkey')->setArgument('$clients', $definitions);
55
    }
56
}
57