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

api-platform / core / 6978770879

24 Nov 2023 09:02AM UTC coverage: 37.284% (-0.1%) from 37.409%
6978770879

push

github

soyuka
Merge 3.2

79 of 149 new or added lines in 21 files covered. (53.02%)

16 existing lines in 8 files now uncovered.

10287 of 27591 relevant lines covered (37.28%)

20.53 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
    }
60✔
29

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

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

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

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

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

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

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

62
        // if the public-property is defined and not yet set; apply it to the response
63
        $public = ($resourceCacheHeaders['public'] ?? $this->public);
52✔
64
        if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
52✔
65
            $public ? $response->setPublic() : $response->setPrivate();
52✔
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')) {
52✔
70
            $response->setSharedMaxAge($sharedMaxAge);
52✔
71
        }
72

73
        if (null !== ($staleWhileRevalidate = $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
52✔
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')) {
52✔
78
            $response->headers->addCacheControlDirective('stale-if-error', (string) $staleIfError);
×
79
        }
80

81
        return $response;
52✔
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

© 2025 Coveralls, Inc