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

api-platform / core / 14648308077

24 Apr 2025 05:54PM UTC coverage: 7.021%. First build
14648308077

Pull #6904

github

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

55 of 188 new or added lines in 9 files covered. (29.26%)

11165 of 159026 relevant lines covered (7.02%)

6.23 hits per line

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

0.9
/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
        }
NEW
126
        return $id;
×
127
    }
128

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

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

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

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

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

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

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

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

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

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

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

231

232

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