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

api-platform / core / 6250499992

20 Sep 2023 03:04PM UTC coverage: 36.863% (-0.2%) from 37.089%
6250499992

push

github

web-flow
fix: errors without compatibility flag (#5841)

24 of 24 new or added lines in 8 files covered. (100.0%)

10081 of 27347 relevant lines covered (36.86%)

13.36 hits per line

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

93.33
/src/Hydra/EventListener/AddLinkHeaderListener.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\Hydra\EventListener;
15

16
use ApiPlatform\Api\UrlGeneratorInterface;
17
use ApiPlatform\JsonLd\ContextBuilder;
18
use ApiPlatform\State\Util\CorsTrait;
19
use Psr\Link\EvolvableLinkProviderInterface;
20
use Symfony\Component\HttpKernel\Event\ResponseEvent;
21
use Symfony\Component\WebLink\GenericLinkProvider;
22
use Symfony\Component\WebLink\Link;
23

24
/**
25
 * Adds the HTTP Link header pointing to the Hydra documentation.
26
 *
27
 * @author Kévin Dunglas <dunglas@gmail.com>
28
 */
29
final class AddLinkHeaderListener
30
{
31
    use CorsTrait;
32

33
    public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
34
    {
35
    }
78✔
36

37
    /**
38
     * Sends the Hydra header on each response.
39
     */
40
    public function onKernelResponse(ResponseEvent $event): void
41
    {
42
        $request = $event->getRequest();
78✔
43
        if (($operation = $request->attributes->get('_api_operation')) && 'api_platform.symfony.main_controller' === $operation->getController()) {
78✔
44
            return;
28✔
45
        }
46

47
        // Prevent issues with NelmioCorsBundle
48
        if ($this->isPreflightRequest($request)) {
54✔
49
            return;
3✔
50
        }
51

52
        $apiDocUrl = $this->urlGenerator->generate('api_doc', ['_format' => 'jsonld'], UrlGeneratorInterface::ABS_URL);
51✔
53
        $apiDocLink = new Link(ContextBuilder::HYDRA_NS.'apiDocumentation', $apiDocUrl);
51✔
54
        $linkProvider = $request->attributes->get('_api_platform_links', new GenericLinkProvider());
51✔
55

56
        if (!$linkProvider instanceof EvolvableLinkProviderInterface) {
51✔
57
            return;
×
58
        }
59

60
        foreach ($linkProvider->getLinks() as $link) {
51✔
61
            if ($link->getHref() === $apiDocUrl) {
30✔
62
                return;
30✔
63
            }
64
        }
65

66
        $request->attributes->set('_api_platform_links', $linkProvider->withLink($apiDocLink));
27✔
67
    }
68
}
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