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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 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(private readonly ProcessorInterface $decorated, private readonly bool $etag = false, private readonly ?int $maxAge = null, private readonly ?int $sharedMaxAge = null, private readonly ?array $vary = null, private readonly ?bool $public = null, private readonly ?int $staleWhileRevalidate = null, private readonly ?int $staleIfError = null)
33
    {
34
    }
589✔
35

36
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
37
    {
38
        $response = $this->decorated->process($data, $operation, $uriVariables, $context);
582✔
39

40
        if (
41
            !($request = $context['request'] ?? null)
582✔
42
            || !$request->isMethodCacheable()
582✔
43
            || !$response instanceof Response
582✔
44
            || !$operation instanceof HttpOperation
582✔
45
        ) {
46
            return $response;
20✔
47
        }
48

49
        if (!($content = $response->getContent()) || !$response->isSuccessful()) {
564✔
50
            return $response;
102✔
51
        }
52

53
        $resourceCacheHeaders = $operation->getCacheHeaders() ?? [];
466✔
54

55
        $public = ($resourceCacheHeaders['public'] ?? $this->public);
466✔
56

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

73
        $response->setCache($options);
466✔
74

75
        $vary = $resourceCacheHeaders['vary'] ?? $this->vary;
466✔
76
        if (null !== $vary) {
466✔
77
            $response->setVary(array_diff($vary, $response->getVary()), false);
466✔
78
        }
79

80
        return $response;
466✔
81
    }
82
}
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

© 2025 Coveralls, Inc