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

api-platform / core / 9219481874

24 May 2024 06:10AM CUT coverage: 57.41% (+0.02%) from 57.392%
9219481874

push

github

web-flow
feat(openapi): allow optional request body content (#6374)

14 of 14 new or added lines in 1 file covered. (100.0%)

58 existing lines in 14 files now uncovered.

10026 of 17464 relevant lines covered (57.41%)

48.72 hits per line

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

93.75
/src/Symfony/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\Symfony\EventListener;
15

16
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use ApiPlatform\State\Util\CorsTrait;
18
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
19
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
20
use Psr\Link\LinkProviderInterface;
21
use Symfony\Component\HttpKernel\Event\ResponseEvent;
22
use Symfony\Component\Mercure\Discovery;
23
use Symfony\Component\WebLink\HttpHeaderSerializer;
24

25
/**
26
 * Adds the HTTP Link header pointing to the Mercure hub for resources having their updates dispatched.
27
 *
28
 * @deprecated use ApiPlatform\Symfony\State\MercureLinkProcessor instead
29
 *
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 */
32
final class AddLinkHeaderListener
33
{
34
    use CorsTrait;
35
    use OperationRequestInitiatorTrait;
36

37
    public function __construct(
38
        private readonly Discovery $discovery,
39
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
40
        private readonly HttpHeaderSerializer $serializer = new HttpHeaderSerializer()
41
    ) {
42
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
62✔
43
    }
44

45
    /**
46
     * Sends the Mercure header on each response.
47
     */
48
    public function onKernelResponse(ResponseEvent $event): void
49
    {
50
        $request = $event->getRequest();
62✔
51
        $operation = $this->initializeOperation($request);
62✔
52

53
        // API Platform 3.2 has a MainController where everything is handled by processors/providers
54
        if ('api_platform.symfony.main_controller' === $operation?->getController() || $this->isPreflightRequest($request) || $request->attributes->get('_api_platform_disable_listeners')) {
62✔
55
            return;
43✔
56
        }
57

58
        // Does the same as the web-link AddLinkHeaderListener as we want to use `_api_platform_links` not `_links`,
59
        // note that the AddLinkHeaderProcessor is doing it with the MainController
60
        $linkProvider = $event->getRequest()->attributes->get('_api_platform_links');
24✔
61
        if ($operation && $linkProvider instanceof LinkProviderInterface && $links = $linkProvider->getLinks()) {
24✔
UNCOV
62
            $event->getResponse()->headers->set('Link', $this->serializer->serialize($links), false);
3✔
63
        }
64

65
        if (
66
            null === $request->attributes->get('_api_resource_class')
24✔
67
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
24✔
68
        ) {
69
            return;
16✔
70
        }
71

72
        $mercure = $operation?->getMercure() ?? ($attributes['mercure'] ?? false);
8✔
73

74
        if (!$mercure) {
8✔
75
            return;
×
76
        }
77

78
        $hub = \is_array($mercure) ? ($mercure['hub'] ?? null) : null;
8✔
79
        $this->discovery->addLink($request, $hub);
8✔
80
    }
81
}
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