• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 19799301771

30 Nov 2025 01:04PM UTC coverage: 25.229% (-0.03%) from 25.257%
19799301771

push

github

web-flow
fix(metadata): repeatable attribute mutators (#7542)

14557 of 57700 relevant lines covered (25.23%)

28.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/HttpCache/State/AddHeadersProcessor.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\HttpCache\State;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\State\ProcessorInterface;
19
use Symfony\Component\HttpFoundation\Response;
20

21
/**
22
 * @template T1
23
 * @template T2
24
 *
25
 * @implements ProcessorInterface<T1, T2>
26
 */
27
final class AddHeadersProcessor implements ProcessorInterface
28
{
29
    /**
30
     * @param ProcessorInterface<T1, T2> $decorated
31
     */
32
    public function __construct(
33
        private readonly ProcessorInterface $decorated,
34
        private readonly bool $etag = false,
35
        private readonly ?int $maxAge = null,
36
        private readonly ?int $sharedMaxAge = null,
37
        private readonly ?array $vary = null,
38
        private readonly ?bool $public = null,
39
        private readonly ?int $staleWhileRevalidate = null,
40
        private readonly ?int $staleIfError = null,
41
    ) {
42
    }
669✔
43

44
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
45
    {
46
        $response = $this->decorated->process($data, $operation, $uriVariables, $context);
659✔
47

48
        if (
49
            !($request = $context['request'] ?? null)
659✔
50
            || !$request->isMethodCacheable()
659✔
51
            || !$response instanceof Response
659✔
52
            || !$operation instanceof HttpOperation
659✔
53
        ) {
54
            return $response;
30✔
55
        }
56

57
        if (!($content = $response->getContent()) || !$response->isSuccessful()) {
637✔
58
            return $response;
115✔
59
        }
60

61
        $resourceCacheHeaders = $operation->getCacheHeaders() ?? [];
526✔
62

63
        $public = ($resourceCacheHeaders['public'] ?? $this->public);
526✔
64

65
        $options = [
526✔
66
            'etag' => $this->etag && !$response->getEtag() ? hash('xxh3', (string) $content) : null,
526✔
67
            'max_age' => null !== ($maxAge = $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age') ? $maxAge : null,
526✔
68
            // Cache-Control "s-maxage" is only relevant is resource is not marked as "private"
69
            's_maxage' => false !== $public && null !== ($sharedMaxAge = $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage') ? $sharedMaxAge : null,
526✔
70
            'public' => null !== $public && !$response->headers->hasCacheControlDirective('public') ? $public : null,
526✔
71
            'stale_while_revalidate' => null !== ($staleWhileRevalidate = $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate') ? $staleWhileRevalidate : null,
526✔
72
            'stale_if_error' => null !== ($staleIfError = $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error') ? $staleIfError : null,
526✔
73
            'must_revalidate' => null !== ($mustRevalidate = $resourceCacheHeaders['must_revalidate'] ?? null) && !$response->headers->hasCacheControlDirective('must-revalidate') ? $mustRevalidate : null,
526✔
74
            'proxy_revalidate' => null !== ($proxyRevalidate = $resourceCacheHeaders['proxy_revalidate'] ?? null) && !$response->headers->hasCacheControlDirective('proxy-revalidate') ? $proxyRevalidate : null,
526✔
75
            'no_cache' => null !== ($noCache = $resourceCacheHeaders['no_cache'] ?? null) && !$response->headers->hasCacheControlDirective('no-cache') ? $noCache : null,
526✔
76
            'no_store' => null !== ($noStore = $resourceCacheHeaders['no_store'] ?? null) && !$response->headers->hasCacheControlDirective('no-store') ? $noStore : null,
526✔
77
            'no_transform' => null !== ($noTransform = $resourceCacheHeaders['no_transform'] ?? null) && !$response->headers->hasCacheControlDirective('no-transform') ? $noTransform : null,
526✔
78
            'immutable' => null !== ($immutable = $resourceCacheHeaders['immutable'] ?? null) && !$response->headers->hasCacheControlDirective('immutable') ? $immutable : null,
526✔
79
        ];
526✔
80

81
        $response->setCache($options);
526✔
82

83
        $vary = $resourceCacheHeaders['vary'] ?? $this->vary;
526✔
84
        if (null !== $vary) {
526✔
85
            $response->setVary(array_diff($vary, $response->getVary()), false);
526✔
86
        }
87

88
        return $response;
526✔
89
    }
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc