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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

92.59
/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
    {
UNCOV
34
    }
799✔
35

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

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

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

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

UNCOV
55
        if ($this->etag && !$response->getEtag()) {
546✔
UNCOV
56
            $response->setEtag(hash('xxh3', (string) $content));
546✔
57
        }
58

UNCOV
59
        if (null !== ($maxAge = $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age')) {
546✔
UNCOV
60
            $response->setMaxAge($maxAge);
546✔
61
        }
62

UNCOV
63
        $vary = $resourceCacheHeaders['vary'] ?? $this->vary;
546✔
UNCOV
64
        if (null !== $vary) {
546✔
UNCOV
65
            $response->setVary(array_diff($vary, $response->getVary()), false);
546✔
66
        }
67

68
        // if the public-property is defined and not yet set; apply it to the response
UNCOV
69
        $public = ($resourceCacheHeaders['public'] ?? $this->public);
546✔
UNCOV
70
        if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
546✔
UNCOV
71
            $public ? $response->setPublic() : $response->setPrivate();
546✔
72
        }
73

74
        // Cache-Control "s-maxage" is only relevant is resource is not marked as "private"
UNCOV
75
        if (false !== $public && null !== ($sharedMaxAge = $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage')) {
546✔
UNCOV
76
            $response->setSharedMaxAge($sharedMaxAge);
546✔
77
        }
78

UNCOV
79
        if (null !== ($staleWhileRevalidate = $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
546✔
80
            $response->headers->addCacheControlDirective('stale-while-revalidate', (string) $staleWhileRevalidate);
×
81
        }
82

UNCOV
83
        if (null !== ($staleIfError = $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error')) {
546✔
84
            $response->headers->addCacheControlDirective('stale-if-error', (string) $staleIfError);
×
85
        }
86

UNCOV
87
        return $response;
546✔
88
    }
89
}
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