Passed
Pull Request — master (#2789)
by
unknown
04:18
created

QueryExpander::expand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 8
rs 10
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
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
62
        $config = $resourceMetadata->getAttribute(self::DOCTRINE_CACHE_CONFIG_ATTR, null);
63
        return is_array($config) ? $config : null;
64
    }
65
66
    private function processCacheable(array $cacheConfig, AbstractQuery $query)
67
    {
68
        if (false !== ($cacheConfig[self::CACHEABLE_ATTR] ?? false)) {
69
            $query->setCacheable(true);
70
        }
71
    }
72
73
    /**
74
     * @throws RuntimeException
75
     */
76
    private function processCacheHint(array $cacheConfig, AbstractQuery $query)
77
    {
78
        if (null !== $cacheHint = $cacheConfig[self::CACHE_HINT_ATTR] ?? null) {
79
            if (!\is_array($cacheHint)) {
80
                throw new RuntimeException(sprintf('Attribute value %s should be an array', self::CACHE_HINT_ATTR));
81
            }
82
            foreach ($cacheHint as $name => $value) {
83
                $query->setHint($name, $value);
84
            }
85
        }
86
    }
87
88
    private function processCacheMode(array $cacheConfig, AbstractQuery $query)
89
    {
90
        if (null !== $cacheMode = $cacheConfig[self::CACHE_MODE_ATTR] ?? null) {
91
            $query->setCacheMode($cacheMode);
92
        }
93
    }
94
95
    /**
96
     * @throws RuntimeException
97
     */
98
    private function processResultCache(array $cacheConfig, AbstractQuery $query)
99
    {
100
        if (null !== $useResultCache = $cacheConfig[self::USE_RESULT_CACHE_ATTR] ?? null) {
101
            if (!\is_array($useResultCache)) {
102
                throw new RuntimeException(sprintf('Attribute value %s should be an array', self::USE_RESULT_CACHE_ATTR));
103
            }
104
            if (empty($useResultCache) || 3 < \count($useResultCache)) {
105
                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));
106
            }
107
            $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

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