Passed
Push — master ( 97616e...3a4af9 )
by Kévin
06:23 queued 02:33
created

CachedRouteNameResolver::getRouteName()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 6
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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Routing;
15
16
use Psr\Cache\CacheException;
17
use Psr\Cache\CacheItemPoolInterface;
18
19
/**
20
 * {@inheritdoc}
21
 *
22
 * @author Teoh Han Hui <[email protected]>
23
 */
24
final class CachedRouteNameResolver implements RouteNameResolverInterface
25
{
26
    const CACHE_KEY_PREFIX = 'route_name_';
27
28
    private $cacheItemPool;
29
    private $decorated;
30
31
    public function __construct(CacheItemPoolInterface $cacheItemPool, RouteNameResolverInterface $decorated)
32
    {
33
        $this->cacheItemPool = $cacheItemPool;
34
        $this->decorated = $decorated;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getRouteName(string $resourceClass, $operationType /**, array $context = []**/): string
41
    {
42
        $context = \func_num_args() > 2 ? func_get_arg(2) : [];
43
        $cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $operationType, $context['subresource_resources'] ?? null]));
44
45
        try {
46
            $cacheItem = $this->cacheItemPool->getItem($cacheKey);
47
        } catch (CacheException $e) {
48
            return $this->decorated->getRouteName($resourceClass, $operationType, $context);
0 ignored issues
show
Unused Code introduced by
The call to ApiPlatform\Core\Bridge\...terface::getRouteName() has too many arguments starting with $context. ( Ignorable by Annotation )

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

48
            return $this->decorated->/** @scrutinizer ignore-call */ getRouteName($resourceClass, $operationType, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
        }
50
51
        if ($cacheItem->isHit()) {
52
            return $cacheItem->get();
53
        }
54
55
        $routeName = $this->decorated->getRouteName($resourceClass, $operationType, $context);
56
57
        $cacheItem->set($routeName);
58
        $this->cacheItemPool->save($cacheItem);
59
60
        return $routeName;
61
    }
62
}
63