1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[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 Silverback\ApiComponentsBundle\Mercure; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Api\IriConverterInterface; |
17
|
|
|
use ApiPlatform\Exception\ItemNotFoundException; |
18
|
|
|
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker; |
19
|
|
|
use Symfony\Component\Mercure\HubInterface; |
20
|
|
|
use Symfony\Component\Mercure\Jwt\TokenFactoryInterface; |
21
|
|
|
use Symfony\Component\Mercure\Jwt\TokenProviderInterface; |
22
|
|
|
use Symfony\Component\Mercure\Update; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @description Force draft resources to be private updates |
26
|
|
|
*/ |
27
|
|
|
class PublishableAwareHub implements HubInterface |
28
|
|
|
{ |
29
|
|
|
public function __construct(private HubInterface $decorated, private PublishableStatusChecker $publishableStatusChecker, private IriConverterInterface $iriConverter) |
30
|
|
|
{ |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getUrl(): string |
34
|
|
|
{ |
35
|
|
|
return $this->decorated->getUrl(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getPublicUrl(): string |
39
|
|
|
{ |
40
|
|
|
return $this->decorated->getPublicUrl(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getProvider(): TokenProviderInterface |
44
|
|
|
{ |
45
|
|
|
return $this->decorated->getProvider(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getFactory(): ?TokenFactoryInterface |
49
|
|
|
{ |
50
|
|
|
return $this->decorated->getFactory(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function publish(Update $update): string |
54
|
|
|
{ |
55
|
|
|
if ($update->getData() && $data = json_decode($update->getData(), true, 512, \JSON_THROW_ON_ERROR)) { |
56
|
|
|
try { |
57
|
|
|
$resource = $this->iriConverter->getResourceFromIri($data['@id']); |
58
|
|
|
} catch (ItemNotFoundException $e) { |
59
|
|
|
return $this->decorated->publish($update); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($this->publishableStatusChecker->getAnnotationReader()->isConfigured($resource) && !$this->publishableStatusChecker->isActivePublishedAt($resource)) { |
63
|
|
|
$update = new Update(topics: $update->getTopics(), data: $update->getData(), private: true, id: $update->getId(), type: $update->getType(), retry: $update->getRetry()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->decorated->publish($update); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|