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

api-platform / core / 10315659289

09 Aug 2024 07:49AM UTC coverage: 7.841% (-0.006%) from 7.847%
10315659289

push

github

soyuka
style: cs fixes

70 of 529 new or added lines in 176 files covered. (13.23%)

160 existing lines in 58 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 hits per line

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

0.0
/src/HttpCache/SurrogateKeysPurger.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;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Contracts\HttpClient\HttpClientInterface;
19

20
/**
21
 * Surrogate keys purger.
22
 *
23
 * @author Sylvain Combraque <darkweak@protonmail.com>
24
 */
25
class SurrogateKeysPurger implements PurgerInterface
26
{
27
    private const MAX_HEADER_SIZE_PER_BATCH = 1500;
28
    private const SEPARATOR = ', ';
29
    private const HEADER = 'Surrogate-Key';
30

31
    /**
32
     * @param HttpClientInterface[] $clients
33
     */
34
    public function __construct(protected readonly iterable $clients, protected readonly int $maxHeaderLength = self::MAX_HEADER_SIZE_PER_BATCH, protected readonly string $header = self::HEADER, protected readonly string $separator = self::SEPARATOR)
35
    {
36
    }
×
37

38
    /**
39
     * @return \Iterator<string>
40
     */
41
    private function getChunkedIris(array $iris): \Iterator
42
    {
43
        if (!$iris) {
×
44
            return;
×
45
        }
46

47
        $chunk = array_shift($iris);
×
48
        foreach ($iris as $iri) {
×
NEW
49
            $nextChunk = \sprintf('%s%s%s', $chunk, $this->separator, $iri);
×
50
            if (\strlen($nextChunk) <= $this->maxHeaderLength) {
×
51
                $chunk = $nextChunk;
×
52
                continue;
×
53
            }
54

55
            yield $chunk;
×
56
            $chunk = $iri;
×
57
        }
58

59
        yield $chunk;
×
60
    }
61

62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function purge(array $iris): void
66
    {
67
        foreach ($this->getChunkedIris($iris) as $chunk) {
×
68
            if (\strlen((string) $chunk) > $this->maxHeaderLength) {
×
NEW
69
                throw new RuntimeException(\sprintf('IRI "%s" is too long to fit current max header length (currently set to "%s"). You can increase it using the "api_platform.http_cache.invalidation.max_header_length" parameter.', $chunk, $this->maxHeaderLength));
×
70
            }
71

72
            foreach ($this->clients as $client) {
×
73
                $client->request(
×
74
                    Request::METHOD_PURGE,
×
75
                    '',
×
76
                    ['headers' => [$this->header => $chunk]]
×
77
                );
×
78
            }
79
        }
80
    }
81

82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getResponseHeaders(array $iris): array
86
    {
87
        return [$this->header => implode($this->separator, $iris)];
×
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