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

api-platform / core / 14664641376

25 Apr 2025 12:30PM UTC coverage: 7.02%. First build
14664641376

Pull #6904

github

web-flow
Merge decfc5b24 into 7c796de0d
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

55 of 192 new or added lines in 9 files covered. (28.65%)

11165 of 159046 relevant lines covered (7.02%)

6.23 hits per line

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

0.89
/src/GraphQl/Subscription/SubscriptionManager.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\GraphQl\Subscription;
15

16
use ApiPlatform\GraphQl\Resolver\Util\IdentifierTrait;
17
use ApiPlatform\Metadata\GraphQl\Operation;
18
use ApiPlatform\Metadata\GraphQl\Subscription;
19
use ApiPlatform\Metadata\IriConverterInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
22
use ApiPlatform\Metadata\Util\SortTrait;
23
use ApiPlatform\State\ProcessorInterface;
24
use GraphQL\Type\Definition\ResolveInfo;
25
use Psr\Cache\CacheItemPoolInterface;
26

27
/**
28
 * Manages all the queried subscriptions by creating their ID
29
 * and saving to a cache the information needed to publish updated data.
30
 *
31
 * @author Alan Poulain <contact@alanpoulain.eu>
32
 */
33
final class SubscriptionManager implements OperationAwareSubscriptionManagerInterface
34
{
35
    use IdentifierTrait;
36
    use ResourceClassInfoTrait;
37
    use SortTrait;
38

39
    public function __construct(private readonly CacheItemPoolInterface $subscriptionsCache, private readonly SubscriptionIdentifierGeneratorInterface $subscriptionIdentifierGenerator, private readonly ProcessorInterface $normalizeProcessor, private readonly IriConverterInterface $iriConverter, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory)
40
    {
41
    }
236✔
42

43
    public function retrieveSubscriptionId(array $context, ?array $result, ?Operation $operation = null): ?string
44
    {
45
        /** @var ResolveInfo $info */
46
        $info = $context['info'];
×
47
        $fields = $info->getFieldSelection(\PHP_INT_MAX);
×
48
        $this->arrayRecursiveSort($fields, 'ksort');
×
49
        $iri = $operation ? $this->getIdentifierFromOperation($operation, $context['args'] ?? []) : $this->getIdentifierFromContext($context);
×
NEW
50
        if (empty($iri)) {
×
51
            return null;
×
52
        }
53

NEW
54
        $options = $operation ? ($operation->getMercure() ?? false) : false;
×
NEW
55
        $private = $options['private'] ?? false;
×
NEW
56
        $privateFields = $options['private_fields'] ?? [];
×
NEW
57
        $previousObject = $context['graphql_context']['previous_object'] ?? null;
×
NEW
58
        if ($private && $privateFields && $previousObject) {
×
NEW
59
            foreach ($options['private_fields'] as $privateField) {
×
NEW
60
                $fields['__private_field_'.$privateField] = $this->getResourceId($privateField, $previousObject);
×
61
            }
62
        }
NEW
63
        if ($operation instanceof Subscription && $operation->isCollection()) {
×
NEW
64
            $subscriptionId = $this->updateSubscriptionCollectionCacheData(
×
NEW
65
                $iri,
×
NEW
66
                $fields,
×
NEW
67
            );
×
68
        } else {
NEW
69
            $subscriptionId = $this->updateSubscriptionItemCacheData(
×
NEW
70
                $iri,
×
NEW
71
                $fields,
×
NEW
72
                $result,
×
NEW
73
                $private,
×
NEW
74
                $privateFields,
×
NEW
75
                $previousObject
×
NEW
76
            );
×
77
        }
78

NEW
79
        return $subscriptionId;
×
80
    }
81

82
    public function getPushPayloads(object $object, string $type): array
83
    {
NEW
84
        if ('delete' === $type) {
×
NEW
85
            $payloads = $this->getDeletePushPayloads($object);
×
86
        } else {
NEW
87
            $payloads = $this->getCreatedOrUpdatedPayloads($object);
×
88
        }
89

NEW
90
        return $payloads;
×
91
    }
92

93
    /**
94
     * @return array<array>
95
     */
96
    private function getSubscriptionsFromIri(string $iri): array
97
    {
98
        $subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri));
×
99

100
        if ($subscriptionsCacheItem->isHit()) {
×
NEW
101
            return $subscriptionsCacheItem->get();
×
102
        }
103

NEW
104
        return [];
×
105
    }
106

107
    private function removeItemFromSubscriptionCache(string $iri): void
108
    {
NEW
109
        $cacheKey = $this->encodeIriToCacheKey($iri);
×
NEW
110
        if ($this->subscriptionsCache->hasItem($cacheKey)) {
×
NEW
111
            $this->subscriptionsCache->deleteItem($cacheKey);
×
112
        }
113
    }
114

115
    private function encodeIriToCacheKey(string $iri): string
116
    {
NEW
117
        return str_replace('/', '_', $iri);
×
118
    }
119

120
    private function getResourceId(mixed $privateField, object $previousObject): string
121
    {
NEW
122
        $id = $previousObject->{'get'.ucfirst($privateField)}()->getId();
×
NEW
123
        if ($id instanceof \Stringable) {
×
NEW
124
            return (string) $id;
×
125
        }
126

NEW
127
        return $id;
×
128
    }
129

130
    private function getCollectionIri(string $iri): string
131
    {
NEW
132
        return substr($iri, 0, strrpos($iri, '/'));
×
133
    }
134

135
    private function getCreatedOrUpdatedPayloads(object $object): array
136
    {
137
        $iri = $this->iriConverter->getIriFromResource($object);
×
138
        // Add collection subscriptions
NEW
139
        $subscriptions = array_merge(
×
NEW
140
            $this->getSubscriptionsFromIri($this->getCollectionIri($iri)),
×
NEW
141
            $this->getSubscriptionsFromIri($iri)
×
NEW
142
        );
×
143

144
        $resourceClass = $this->getObjectClass($object);
×
145
        $resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
×
146
        $shortName = $resourceMetadata->getOperation()->getShortName();
×
147

NEW
148
        $mercure = $resourceMetadata->getOperation()->getMercure() ?? false;
×
NEW
149
        $private = $mercure['private'] ?? false;
×
NEW
150
        $privateFieldsConfig = $mercure['private_fields'] ?? [];
×
NEW
151
        $privateFieldData = [];
×
NEW
152
        if ($private && $privateFieldsConfig) {
×
NEW
153
            foreach ($privateFieldsConfig as $privateField) {
×
NEW
154
                $privateFieldData['__private_field_'.$privateField] = $this->getResourceId($privateField, $object);
×
155
            }
156
        }
157

158
        $payloads = [];
×
159
        foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) {
×
NEW
160
            if ($privateFieldData) {
×
NEW
161
                $fieldDiff = array_intersect_assoc($subscriptionFields, $privateFieldData);
×
NEW
162
                if ($fieldDiff !== $privateFieldData) {
×
NEW
163
                    continue;
×
164
                }
165
            }
166
            $resolverContext = ['fields' => $subscriptionFields, 'is_collection' => false, 'is_mutation' => false, 'is_subscription' => true];
×
NEW
167
            $operation = (new Subscription())->withName('mercure_subscription')->withShortName($shortName);
×
168
            $data = $this->normalizeProcessor->process($object, $operation, [], $resolverContext);
×
169

170
            unset($data['clientSubscriptionId']);
×
171

172
            if ($data !== $subscriptionResult) {
×
173
                $payloads[] = [$subscriptionId, $data];
×
174
            }
175
        }
176

177
        return $payloads;
×
178
    }
179

180
    private function getDeletePushPayloads(object $object): array
181
    {
NEW
182
        $iri = $object->id;
×
NEW
183
        $subscriptions = $this->getSubscriptionsFromIri($iri);
×
NEW
184
        if ([] === $subscriptions) {
×
185
            // Get subscriptions from collection Iri
NEW
186
            $subscriptions = $this->getSubscriptionsFromIri($this->getCollectionIri($iri));
×
187
        }
188

NEW
189
        $payloads = [];
×
NEW
190
        foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) {
×
NEW
191
            $payloads[] = [$subscriptionId, ['type' => 'delete', 'payload' => $object]];
×
192
        }
NEW
193
        $this->removeItemFromSubscriptionCache($iri);
×
194

NEW
195
        return $payloads;
×
196
    }
197

198
    private function updateSubscriptionItemCacheData(
199
        string $iri,
200
        array $fields,
201
        ?array $result,
202
        bool $private,
203
        array $privateFields,
204
        ?object $previousObject,
205
    ): string {
NEW
206
        $subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri));
×
207
        $subscriptions = [];
×
NEW
208
        if ($subscriptionsCacheItem->isHit()) {
×
209
            /*
210
             * @var array<array{string, array<string, string|array>, array<string, string|array>}>
211
             */
NEW
212
            $subscriptions = $subscriptionsCacheItem->get();
×
NEW
213
            foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) {
×
NEW
214
                if ($subscriptionFields === $fields) {
×
NEW
215
                    return $subscriptionId;
×
216
                }
217
            }
218
        }
219

NEW
220
        $subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields);
×
NEW
221
        unset($result['clientSubscriptionId']);
×
NEW
222
        if ($private && $privateFields && $previousObject) {
×
NEW
223
            foreach ($privateFields as $privateField) {
×
NEW
224
                unset($result['__private_field_'.$privateField]);
×
225
            }
226
        }
NEW
227
        $subscriptions[] = [$subscriptionId, $fields, $result];
×
NEW
228
        $subscriptionsCacheItem->set($subscriptions);
×
NEW
229
        $this->subscriptionsCache->save($subscriptionsCacheItem);
×
230

231
        return $subscriptionId;
×
232
    }
233

234
    private function updateSubscriptionCollectionCacheData(
235
        string $iri,
236
        array $fields,
237
    ): string {
NEW
238
        $subscriptionCollectionCacheItem = $this->subscriptionsCache->getItem(
×
NEW
239
            $this->encodeIriToCacheKey($this->getCollectionIri($iri)),
×
NEW
240
        );
×
NEW
241
        $collectionSubscriptions = [];
×
NEW
242
        if ($subscriptionCollectionCacheItem->isHit()) {
×
NEW
243
            $collectionSubscriptions = $subscriptionCollectionCacheItem->get();
×
NEW
244
            foreach ($collectionSubscriptions as [$subscriptionId, $subscriptionFields]) {
×
NEW
245
                if ($subscriptionFields === $fields) {
×
NEW
246
                    return $subscriptionId;
×
247
                }
248
            }
249
        }
NEW
250
        $subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields + ['__collection' => true]);
×
NEW
251
        $collectionSubscriptions[] = [$subscriptionId, $fields];
×
NEW
252
        $subscriptionCollectionCacheItem->set($collectionSubscriptions);
×
NEW
253
        $this->subscriptionsCache->save($subscriptionCollectionCacheItem);
×
254

255
        return $subscriptionId;
×
256
    }
257
}
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