1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Controller; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
16
|
|
|
use Ynlo\GraphQLBundle\Subscription\PubSub\PubSubHandlerInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Subscription\Subscriber; |
18
|
|
|
|
19
|
|
|
class SubscriptionsController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $mercureHubUrl; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var PubSubHandlerInterface |
28
|
|
|
*/ |
29
|
|
|
protected $pubSubHandler; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $ttl = Subscriber::DEFAULT_SUBSCRIPTION_TTL; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* SubscriptionsController constructor. |
38
|
|
|
* |
39
|
|
|
* @param PubSubHandlerInterface $pubSubHandler |
40
|
|
|
*/ |
41
|
|
|
public function __construct(PubSubHandlerInterface $pubSubHandler) |
42
|
|
|
{ |
43
|
|
|
$this->pubSubHandler = $pubSubHandler; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param int $ttl |
48
|
|
|
* |
49
|
|
|
* @return SubscriptionsController |
50
|
|
|
*/ |
51
|
|
|
public function setTtl(int $ttl): SubscriptionsController |
52
|
|
|
{ |
53
|
|
|
$this->ttl = $ttl; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $mercureHubsUrls |
60
|
|
|
* @param string $hub |
61
|
|
|
*/ |
62
|
|
|
public function setMercureHubUrl(array $mercureHubsUrls, $hub) |
63
|
|
|
{ |
64
|
|
|
$this->mercureHubUrl = $mercureHubsUrls[$hub]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
*/ |
70
|
|
|
public function __invoke(Request $request, string $subscription) |
71
|
|
|
{ |
72
|
|
|
if (!$this->pubSubHandler->exists($subscription)) { |
73
|
|
|
throw new NotFoundHttpException(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->pubSubHandler->touch($subscription, date_create_from_format('U', time() + $this->ttl)); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return new RedirectResponse(sprintf('%s?topic=%s', $this->mercureHubUrl, $subscription)); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|