Passed
Push — master ( e8effc...d6f260 )
by Alan
04:52
created

ItemSubscriptionResolverFactory::__invoke()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 18
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 33
rs 8.4444
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\GraphQl\Resolver\Factory;
15
16
use ApiPlatform\Core\GraphQl\Resolver\Stage\ReadStageInterface;
17
use ApiPlatform\Core\GraphQl\Resolver\Stage\SecurityStageInterface;
18
use ApiPlatform\Core\GraphQl\Resolver\Stage\SerializeStageInterface;
19
use ApiPlatform\Core\GraphQl\Subscription\MercureSubscriptionIriGeneratorInterface;
20
use ApiPlatform\Core\GraphQl\Subscription\SubscriptionManagerInterface;
21
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
22
use ApiPlatform\Core\Util\ClassInfoTrait;
23
use ApiPlatform\Core\Util\CloneTrait;
24
use GraphQL\Type\Definition\ResolveInfo;
25
26
/**
27
 * Creates a function resolving a GraphQL subscription of an item.
28
 *
29
 * @experimental
30
 *
31
 * @author Alan Poulain <[email protected]>
32
 */
33
final class ItemSubscriptionResolverFactory implements ResolverFactoryInterface
34
{
35
    use ClassInfoTrait;
36
    use CloneTrait;
37
38
    private $readStage;
39
    private $securityStage;
40
    private $serializeStage;
41
    private $resourceMetadataFactory;
42
    private $subscriptionManager;
43
    private $mercureSubscriptionIriGenerator;
44
45
    public function __construct(ReadStageInterface $readStage, SecurityStageInterface $securityStage, SerializeStageInterface $serializeStage, ResourceMetadataFactoryInterface $resourceMetadataFactory, SubscriptionManagerInterface $subscriptionManager, ?MercureSubscriptionIriGeneratorInterface $mercureSubscriptionIriGenerator)
46
    {
47
        $this->readStage = $readStage;
48
        $this->securityStage = $securityStage;
49
        $this->serializeStage = $serializeStage;
50
        $this->resourceMetadataFactory = $resourceMetadataFactory;
51
        $this->subscriptionManager = $subscriptionManager;
52
        $this->mercureSubscriptionIriGenerator = $mercureSubscriptionIriGenerator;
53
    }
54
55
    public function __invoke(?string $resourceClass = null, ?string $rootClass = null, ?string $operationName = null): callable
56
    {
57
        return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operationName) {
58
            if (null === $resourceClass || null === $operationName) {
59
                return null;
60
            }
61
62
            $resolverContext = ['source' => $source, 'args' => $args, 'info' => $info, 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => true];
63
64
            $item = ($this->readStage)($resourceClass, $rootClass, $operationName, $resolverContext);
65
            if (null !== $item && !\is_object($item)) {
66
                throw new \LogicException('Item from read stage should be a nullable object.');
67
            }
68
            ($this->securityStage)($resourceClass, $operationName, $resolverContext + [
69
                'extra_variables' => [
70
                    'object' => $item,
71
                ],
72
            ]);
73
74
            $result = ($this->serializeStage)($item, $resourceClass, $operationName, $resolverContext);
75
76
            $subscriptionId = $this->subscriptionManager->retrieveSubscriptionId($resolverContext, $result);
77
78
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
79
80
            if ($subscriptionId && $resourceMetadata->getAttribute('mercure', false)) {
81
                if (!$this->mercureSubscriptionIriGenerator) {
82
                    throw new \LogicException('Cannot use Mercure for subscriptions when MercureBundle is not installed. Try running "composer require mercure".');
83
                }
84
                $result['mercureUrl'] = $this->mercureSubscriptionIriGenerator->generateMercureUrl($subscriptionId);
85
            }
86
87
            return $result;
88
        };
89
    }
90
}
91