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

api-platform / core / 15904482964

26 Jun 2025 02:22PM UTC coverage: 21.957%. First build
15904482964

Pull #6904

github

web-flow
Merge cea37cac5 into 4ecde01e8
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

55 of 252 new or added lines in 9 files covered. (21.83%)

11494 of 52347 relevant lines covered (21.96%)

21.6 hits per line

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

0.68
/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
    }
262✔
42

43
    public function retrieveSubscriptionId(array $context, ?array $result, ?Operation $operation = null): ?string
44
    {
NEW
45
        $iri = $operation ? $this->getIdentifierFromOperation($operation, $context['args'] ?? []) : $this->getIdentifierFromContext($context);
×
NEW
46
        if (empty($iri)) {
×
NEW
47
            return null;
×
48
        }
49

50
        /** @var ResolveInfo $info */
51
        $info = $context['info'];
×
52
        $fields = $info->getFieldSelection(\PHP_INT_MAX);
×
53
        $this->arrayRecursiveSort($fields, 'ksort');
×
54

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

NEW
84
        return $subscriptionId;
×
85
    }
86

87
    public function getPushPayloads(object $object, string $type): array
88
    {
NEW
89
        if ('delete' === $type) {
×
NEW
90
            $payloads = $this->getDeletePushPayloads($object);
×
91
        } else {
NEW
92
            $payloads = $this->getCreatedOrUpdatedPayloads($object);
×
93
        }
94

NEW
95
        return $payloads;
×
96
    }
97

98
    /**
99
     * @return array<array>
100
     */
101
    private function getSubscriptionsFromIri(string $iri, array $fields = []): array
102
    {
NEW
103
        $subscriptionsCacheItem = $this->subscriptionsCache->getItem(
×
NEW
104
            $this->generatePrivateCacheKeyPart(
×
NEW
105
                $this->encodeIriToCacheKey($iri),
×
NEW
106
                $fields
×
NEW
107
            )
×
108

NEW
109
        );
×
110

111
        if ($subscriptionsCacheItem->isHit()) {
×
NEW
112
            return $subscriptionsCacheItem->get();
×
113
        }
114

NEW
115
        return [];
×
116
    }
117

118
    private function removeItemFromSubscriptionCache(string $iri): void
119
    {
NEW
120
        $cacheKey = $this->encodeIriToCacheKey($iri);
×
NEW
121
        if ($this->subscriptionsCache->hasItem($cacheKey)) {
×
NEW
122
            $this->subscriptionsCache->deleteItem($cacheKey);
×
123
        }
124
    }
125

126
    private function encodeIriToCacheKey(string $iri): string
127
    {
NEW
128
        return str_replace('/', '_', $iri);
×
129
    }
130

131
    private function getResourceId(mixed $privateField, object $previousObject): string
132
    {
NEW
133
        $id = $previousObject->{'get'.ucfirst($privateField)}()->getId();
×
NEW
134
        if ($id instanceof \Stringable || is_numeric($id)) {
×
NEW
135
            return (string) $id;
×
136
        }
137

NEW
138
        return $id;
×
139
    }
140

141
    private function getCollectionIri(string $iri): string
142
    {
NEW
143
        return substr($iri, 0, strrpos($iri, '/'));
×
144
    }
145

146
    private function getCreatedOrUpdatedPayloads(object $object): array
147
    {
148
        $resourceClass = $this->getObjectClass($object);
×
149
        $resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
×
150
        $shortName = $resourceMetadata->getOperation()->getShortName();
×
151

152
        $payloads = [];
×
NEW
153
        foreach ($resourceMetadata as $apiResource) {
×
NEW
154
            foreach ($apiResource->getGraphQlOperations() as $operation) {
×
NEW
155
                if (!$operation instanceof Subscription) {
×
NEW
156
                    continue;
×
157
                }
NEW
158
                $mercure = $resourceMetadata->getOperation()->getMercure() ?? false;
×
NEW
159
                $operationMercure = $operation->getMercure() ?? false;
×
NEW
160
                if ($mercure !== false && $operationMercure !== false) {
×
161
                    /** @noinspection SlowArrayOperationsInLoopInspection */
NEW
162
                    $mercure = array_merge($mercure, $operationMercure);
×
163
                }
NEW
164
                $private = $mercure['private'] ?? false;
×
NEW
165
                $privateFieldsConfig = $mercure['private_fields'] ?? [];
×
NEW
166
                $privateFieldData = [];
×
NEW
167
                if ($private && $privateFieldsConfig) {
×
NEW
168
                    foreach ($privateFieldsConfig as $privateField) {
×
NEW
169
                        $privateFieldData['__private_field_' . $privateField] = $this->getResourceId(
×
NEW
170
                            $privateField,
×
NEW
171
                            $object
×
NEW
172
                        );
×
173
                    }
174
                }
175

NEW
176
                $iri = $this->iriConverter->getIriFromResource($object);
×
177
                // Add collection subscriptions
NEW
178
                $subscriptions = array_merge(
×
NEW
179
                    $this->getSubscriptionsFromIri($this->getCollectionIri($iri), $privateFieldData),
×
NEW
180
                    $this->getSubscriptionsFromIri($iri)
×
NEW
181
                );
×
182

NEW
183
                foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) {
×
NEW
184
                    if ($privateFieldData) {
×
NEW
185
                        $fieldDiff = array_intersect_assoc($subscriptionFields, $privateFieldData);
×
NEW
186
                        if ($fieldDiff !== $privateFieldData) {
×
NEW
187
                            continue;
×
188
                        }
189
                    }
NEW
190
                    $resolverContext = [
×
NEW
191
                        'fields'          => $subscriptionFields,
×
NEW
192
                        'is_collection'   => false,
×
NEW
193
                        'is_mutation'     => false,
×
NEW
194
                        'is_subscription' => true
×
NEW
195
                    ];
×
NEW
196
                    $subscriptionOperation = (new Subscription())->withName('mercure_subscription')->withShortName(
×
NEW
197
                        $shortName
×
NEW
198
                    );
×
NEW
199
                    $data = $this->normalizeProcessor->process($object, $subscriptionOperation, [], $resolverContext);
×
200

NEW
201
                    unset($data['clientSubscriptionId']);
×
202

NEW
203
                    if ($data !== $subscriptionResult) {
×
NEW
204
                        $payloads[] = [$subscriptionId, $data];
×
205
                    }
206
                }
207
            }
208
        }
209

210
        return $payloads;
×
211
    }
212

213
    private function getDeletePushPayloads(object $object): array
214
    {
NEW
215
        $iri = $object->id;
×
NEW
216
        $subscriptions = array_merge(
×
NEW
217
            $this->getSubscriptionsFromIri($iri),
×
NEW
218
            $this->getSubscriptionsFromIri($this->getCollectionIri($iri), $object->private),
×
NEW
219
        );
×
220

NEW
221
        $payloads = [];
×
NEW
222
        $payload = ['type' => 'delete', 'payload' => ['id' => $object->id, 'iri' => $object->iri, 'type' => $object->type]];
×
NEW
223
        foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) {
×
NEW
224
            $payloads[] = [$subscriptionId, $payload];
×
225
        }
NEW
226
        $this->removeItemFromSubscriptionCache($iri);
×
227

NEW
228
        return $payloads;
×
229
    }
230

231
    private function updateSubscriptionItemCacheData(
232
        string $iri,
233
        array $fields,
234
        ?array $result,
235
        bool $private,
236
        array $privateFields,
237
        ?object $previousObject,
238
    ): string {
NEW
239
        $subscriptionsCacheItem = $this->subscriptionsCache->getItem($this->encodeIriToCacheKey($iri));
×
NEW
240
        $subscriptions = [];
×
241
        if ($subscriptionsCacheItem->isHit()) {
×
242
            /*
243
             * @var array<array{string, array<string, string|array>, array<string, string|array>}>
244
             */
NEW
245
            $subscriptions = $subscriptionsCacheItem->get();
×
NEW
246
            foreach ($subscriptions as [$subscriptionId, $subscriptionFields, $subscriptionResult]) {
×
NEW
247
                if ($subscriptionFields === $fields) {
×
NEW
248
                    return $subscriptionId;
×
249
                }
250
            }
251
        }
252

NEW
253
        unset($result['clientSubscriptionId']);
×
NEW
254
        if ($private && $privateFields && $previousObject) {
×
NEW
255
            $subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields);
×
NEW
256
            foreach ($privateFields as $privateField) {
×
NEW
257
                unset($result['__private_field_'.$privateField]);
×
258
            }
259
        } else {
NEW
260
            $subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields);
×
261
        }
NEW
262
        $subscriptions[] = [$subscriptionId, $fields, $result];
×
NEW
263
        $subscriptionsCacheItem->set($subscriptions);
×
NEW
264
        $this->subscriptionsCache->save($subscriptionsCacheItem);
×
265

NEW
266
        return $subscriptionId;
×
267
    }
268

269
    private function updateSubscriptionCollectionCacheData(
270
        string $iri,
271
        array  $fields,
272
        array  $privateFieldData,
273
    ): string {
274

NEW
275
        $subscriptionCollectionCacheItem = $this->subscriptionsCache->getItem(
×
NEW
276
            $this->generatePrivateCacheKeyPart(
×
NEW
277
                $this->encodeIriToCacheKey($this->getCollectionIri($iri)),
×
NEW
278
                $privateFieldData
×
NEW
279
            ),
×
NEW
280
        );
×
NEW
281
        $collectionSubscriptions = [];
×
NEW
282
        if ($subscriptionCollectionCacheItem->isHit()) {
×
NEW
283
            $collectionSubscriptions = $subscriptionCollectionCacheItem->get();
×
NEW
284
            foreach ($collectionSubscriptions as [$subscriptionId, $subscriptionFields, $result]) {
×
NEW
285
                if ($subscriptionFields === $fields) {
×
NEW
286
                    return $subscriptionId;
×
287
                }
288
            }
289
        }
NEW
290
        $subscriptionId = $this->subscriptionIdentifierGenerator->generateSubscriptionIdentifier($fields + ['__collection' => true]);
×
NEW
291
        $collectionSubscriptions[] = [$subscriptionId, $fields, []];
×
NEW
292
        $subscriptionCollectionCacheItem->set($collectionSubscriptions);
×
NEW
293
        $this->subscriptionsCache->save($subscriptionCollectionCacheItem);
×
294

NEW
295
        return $subscriptionId;
×
296
    }
297

298
    private function generatePrivateCacheKeyPart(string $iriKey, array $fields = []): string
299
    {
NEW
300
        if (empty($fields)) {
×
NEW
301
            return $iriKey;
×
302
        }
NEW
303
        return $iriKey.'_'.implode('_', $fields);
×
304
    }
305

306
}
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