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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

88.89
/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
final class AddHeadersProcessor implements ProcessorInterface
22
{
23
    /**
24
     * @param ProcessorInterface<Response>|ProcessorInterface<mixed> $decorated
25
     */
26
    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)
27
    {
28
    }
85✔
29

30
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
31
    {
32
        $response = $this->decorated->process($data, $operation, $uriVariables, $context);
82✔
33

34
        if (
35
            !($request = $context['request'] ?? null)
82✔
36
            || !$request->isMethodCacheable()
82✔
37
            || !$response instanceof Response
82✔
38
            || !$operation instanceof HttpOperation
82✔
39
        ) {
40
            return $response;
9✔
41
        }
42

43
        if (!($content = $response->getContent()) || !$response->isSuccessful()) {
73✔
44
            return $response;
×
45
        }
46

47
        $resourceCacheHeaders = $operation->getCacheHeaders() ?? [];
73✔
48

49
        if ($this->etag && !$response->getEtag()) {
73✔
50
            $response->setEtag(md5((string) $content));
73✔
51
        }
52

53
        if (null !== ($maxAge = $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age')) {
73✔
54
            $response->setMaxAge($maxAge);
73✔
55
        }
56

57
        $vary = $resourceCacheHeaders['vary'] ?? $this->vary;
73✔
58
        if (null !== $vary) {
73✔
59
            $response->setVary(array_diff($vary, $response->getVary()), false);
73✔
60
        }
61

62
        // if the public-property is defined and not yet set; apply it to the response
63
        $public = ($resourceCacheHeaders['public'] ?? $this->public);
73✔
64
        if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
73✔
65
            $public ? $response->setPublic() : $response->setPrivate();
73✔
66
        }
67

68
        // Cache-Control "s-maxage" is only relevant is resource is not marked as "private"
69
        if (false !== $public && null !== ($sharedMaxAge = $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage')) {
73✔
70
            $response->setSharedMaxAge($sharedMaxAge);
73✔
71
        }
72

73
        if (null !== ($staleWhileRevalidate = $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
73✔
74
            $response->headers->addCacheControlDirective('stale-while-revalidate', (string) $staleWhileRevalidate);
×
75
        }
76

77
        if (null !== ($staleIfError = $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error')) {
73✔
78
            $response->headers->addCacheControlDirective('stale-if-error', (string) $staleIfError);
×
79
        }
80

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