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

setMercureHubUrl()   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 2
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\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Ynlo\GraphQLBundle\Subscription\PubSub\PubSubHandlerInterface;
17
use Ynlo\GraphQLBundle\Subscription\Subscriber;
18
19
class SubscriptionsHeartbeatController
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
    public function setTtl(int $ttl)
50
    {
51
        $this->ttl = $ttl;
52
    }
53
54
    /**
55
     * @param array  $mercureHubsUrls
56
     * @param string $hub
57
     */
58
    public function setMercureHubUrl(array $mercureHubsUrls, $hub)
59
    {
60
        $this->mercureHubUrl = $mercureHubsUrls[$hub];
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function __invoke(Request $request, string $subscription)
67
    {
68
        if (!$this->pubSubHandler->exists($subscription)) {
69
            throw new NotFoundHttpException();
70
        }
71
72
        $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

72
        $this->pubSubHandler->touch($subscription, /** @scrutinizer ignore-type */ date_create_from_format('U', time() + $this->ttl));
Loading history...
73
74
        return new Response();
75
    }
76
}
77