Completed
Push — master ( ac8ec4...1a57ac )
by Kévin
04:37 queued 11s
created

CacheKeyTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A getCacheKey() 0 13 3
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\Serializer;
15
16
trait CacheKeyTrait
17
{
18
    /**
19
     * @return string|bool
20
     */
21
    private function getCacheKey(?string $format, array $context)
22
    {
23
        foreach ($context[self::EXCLUDE_FROM_CACHE_KEY] ?? $this->defaultContext[self::EXCLUDE_FROM_CACHE_KEY] as $key) {
0 ignored issues
show
Bug introduced by
The constant ApiPlatform\Core\Seriali...:EXCLUDE_FROM_CACHE_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
            unset($context[$key]);
25
        }
26
        unset($context[self::EXCLUDE_FROM_CACHE_KEY]);
27
        unset($context['cache_key']); // avoid artificially different keys
28
29
        try {
30
            return md5($format.serialize($context));
31
        } catch (\Exception $exception) {
32
            // The context cannot be serialized, skip the cache
33
            return false;
34
        }
35
    }
36
}
37