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

api-platform / core / 7971496386

20 Feb 2024 09:50AM UTC coverage: 66.694%. Remained the same
7971496386

push

github

web-flow
test(symfony): empty collection without fields (#6165)

see https://github.com/symfony/symfony/commit/b34153555

16308 of 24452 relevant lines covered (66.69%)

37.73 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 as LegacyUrlGeneratorInterface;
17
use ApiPlatform\JsonLd\ContextBuilder;
18
use ApiPlatform\Metadata\UrlGeneratorInterface;
19
use ApiPlatform\State\Util\CorsTrait;
20
use Psr\Link\EvolvableLinkProviderInterface;
21
use Symfony\Component\HttpKernel\Event\ResponseEvent;
22
use Symfony\Component\WebLink\GenericLinkProvider;
23
use Symfony\Component\WebLink\Link;
24

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

34
    public function __construct(private readonly UrlGeneratorInterface|LegacyUrlGeneratorInterface $urlGenerator)
35
    {
36
    }
120✔
37

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

48
        // Prevent issues with NelmioCorsBundle
49
        if ($this->isPreflightRequest($request)) {
75✔
50
            return;
4✔
51
        }
52

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

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

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

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