Completed
Pull Request — master (#2789)
by
unknown
03:32
created

QueryExpander::processCacheMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
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 ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
20
use Doctrine\ORM\AbstractQuery;
21
22
/**
23
 * Class QueryExpander.
24
 *
25
 * Expands Query settings with Second Level Cache and Result Cache settings from attributes.
26
 *
27
 * @author st-it <[email protected]>
28
 */
29
class QueryExpander
30
{
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
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
49
        $this->processCacheable($resourceMetadata, $query);
50
        $this->processCacheHint($resourceMetadata, $query);
51
        $this->processCacheMode($resourceMetadata, $query);
52
        $this->processResultCache($resourceMetadata, $query);
53
    }
54
55
    private function processCacheable(ResourceMetadata $resourceMetadata, AbstractQuery $query)
56
    {
57
        if (false !== $resourceMetadata->getAttribute(self::CACHEABLE_ATTR, false)) {
58
            $query->setCacheable(true);
59
        }
60
    }
61
62
    /**
63
     * @throws RuntimeException
64
     */
65
    private function processCacheHint(ResourceMetadata $resourceMetadata, AbstractQuery $query)
66
    {
67
        if (null !== $cacheHint = $resourceMetadata->getAttribute(self::CACHE_HINT_ATTR, null)) {
68
            if (!\is_array($cacheHint)) {
69
                throw new RuntimeException(sprintf('Attribute value %s should be an array', self::CACHE_HINT_ATTR));
70
            }
71
            foreach ($cacheHint as $name => $value) {
72
                $query->setHint($name, $value);
73
            }
74
        }
75
    }
76
77
    private function processCacheMode(ResourceMetadata $resourceMetadata, AbstractQuery $query)
78
    {
79
        if (null !== $cacheMode = $resourceMetadata->getAttribute(self::CACHE_MODE_ATTR, null)) {
80
            $query->setCacheMode($cacheMode);
81
        }
82
    }
83
84
    /**
85
     * @throws RuntimeException
86
     */
87
    private function processResultCache(ResourceMetadata $resourceMetadata, AbstractQuery $query)
88
    {
89
        if (null !== $useResultCache = $resourceMetadata->getAttribute(self::USE_RESULT_CACHE_ATTR, null)) {
90
            if (!\is_array($useResultCache)) {
91
                throw new RuntimeException(sprintf('Attribute value %s should be an array', self::USE_RESULT_CACHE_ATTR));
92
            }
93
            if (empty($useResultCache) || 3 < \count($useResultCache)) {
94
                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));
95
            }
96
            $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

96
            $query->useResultCache(/** @scrutinizer ignore-type */ ...$useResultCache);
Loading history...
97
        }
98
    }
99
}
100