Completed
Pull Request — master (#13)
by Rafael
06:46
created

SubscriptionsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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));
0 ignored issues
show
Bug introduced by
It seems like date_create_from_format('U', time() + $this->ttl) can also be of type false; however, parameter $expireAt of Ynlo\GraphQLBundle\Subsc...ndlerInterface::touch() does only seem to accept DateTime, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $this->pubSubHandler->touch($subscription, /** @scrutinizer ignore-type */ date_create_from_format('U', time() + $this->ttl));
Loading history...
77
78
        return new RedirectResponse(sprintf('%s?topic=%s', $this->mercureHubUrl, $subscription));
79
    }
80
}
81