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

src/Serializer/CacheKeyTrait.php (1 issue)

Labels
Severity
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
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