Passed
Pull Request — master (#2789)
by
unknown
05:40
created

QueryExpander::processResultCache()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 4
nop 2
dl 0
loc 10
rs 9.6111
c 0
b 0
f 0
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\Doctrine\Orm\Cache;
15
16
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
17
use ApiPlatform\Core\Exception\RuntimeException;
18
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
19
use Doctrine\ORM\AbstractQuery;
20
21
/**
22
 * Class QueryExpander.
23
 *
24
 * Expands Query settings with Second Level Cache and Result Cache settings from attributes.
25
 *
26
 * @author st-it <[email protected]>
27
 */
28
class QueryExpander
29
{
30
    public const DOCTRINE_CACHE_CONFIG_ATTR = 'doctrine_cache';
31
    public const CACHEABLE_ATTR = 'cacheable';
32
    public const CACHE_HINT_ATTR = 'cache_hint';
33
    public const CACHE_MODE_ATTR = 'cache_mode';
34
    public const USE_RESULT_CACHE_ATTR = 'use_result_cache';
35
36
    private $resourceMetadataFactory;
37
38
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory)
39
    {
40
        $this->resourceMetadataFactory = $resourceMetadataFactory;
41
    }
42
43
    /**
44
     * @throws ResourceClassNotFoundException
45
     */
46
    public function expand(string $resourceClass, AbstractQuery $query)
47
    {
48
        $cacheConfig = $this->getDoctrineCacheConfig($resourceClass);
49
        if (null !== $cacheConfig) {
50
            $this->processCacheable($cacheConfig, $query);
51
            $this->processCacheHint($cacheConfig, $query);
52
            $this->processCacheMode($cacheConfig, $query);
53
            $this->processResultCache($cacheConfig, $query);
54
        }
55
    }
56
57
    /**
58
     * @throws ResourceClassNotFoundException
59
     */
60
    private function getDoctrineCacheConfig(string $resourceClass): ?array
61
    {
62
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
63
        $config = $resourceMetadata->getAttribute(self::DOCTRINE_CACHE_CONFIG_ATTR, null);
64
65
        return \is_array($config) ? $config : null;
66
    }
67
68
    private function processCacheable(array $cacheConfig, AbstractQuery $query)
69
    {
70
        if (false !== ($cacheConfig[self::CACHEABLE_ATTR] ?? false)) {
71
            $query->setCacheable(true);
72
        }
73
    }
74
75
    /**
76
     * @throws RuntimeException
77
     */
78
    private function processCacheHint(array $cacheConfig, AbstractQuery $query)
79
    {
80
        if (null !== $cacheHint = $cacheConfig[self::CACHE_HINT_ATTR] ?? null) {
81
            if (!\is_array($cacheHint)) {
82
                throw new RuntimeException(sprintf('Attribute value %s should be an array', self::CACHE_HINT_ATTR));
83
            }
84
            foreach ($cacheHint as $name => $value) {
85
                $query->setHint($name, $value);
86
            }
87
        }
88
    }
89
90
    private function processCacheMode(array $cacheConfig, AbstractQuery $query)
91
    {
92
        if (null !== $cacheMode = $cacheConfig[self::CACHE_MODE_ATTR] ?? null) {
93
            $query->setCacheMode($cacheMode);
94
        }
95
    }
96
97
    /**
98
     * @throws RuntimeException
99
     */
100
    private function processResultCache(array $cacheConfig, AbstractQuery $query)
101
    {
102
        if (null !== $useResultCache = $cacheConfig[self::USE_RESULT_CACHE_ATTR] ?? null) {
103
            if (!\is_array($useResultCache)) {
104
                throw new RuntimeException(sprintf('Attribute value %s should be an array', self::USE_RESULT_CACHE_ATTR));
105
            }
106
            if (empty($useResultCache) || 3 < \count($useResultCache)) {
107
                throw new RuntimeException(sprintf('Attribute %s should at least contain one item for use. Other options are lifetime and and result cache id', self::USE_RESULT_CACHE_ATTR));
108
            }
109
            $query->useResultCache(...$useResultCache);
0 ignored issues
show
Bug introduced by
$useResultCache is expanded, but the parameter $bool of Doctrine\ORM\AbstractQuery::useResultCache() does not expect variable arguments. ( Ignorable by Annotation )

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

109
            $query->useResultCache(/** @scrutinizer ignore-type */ ...$useResultCache);
Loading history...
110
        }
111
    }
112
}
113