|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
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 LoginCidadao\CoreBundle\LongPolling; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use LoginCidadao\APIBundle\Exception\RequestTimeoutException; |
|
15
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
16
|
|
|
use LoginCidadao\CoreBundle\Tests\LongPolling\LongPollableInterface; |
|
17
|
|
|
|
|
18
|
|
|
class LongPollingUtils |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var EntityManagerInterface */ |
|
21
|
|
|
private $em; |
|
22
|
|
|
|
|
23
|
|
|
/** @var int */ |
|
24
|
|
|
private $maxExecutionTime; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* LongPollingUtils constructor. |
|
28
|
|
|
* @param EntityManagerInterface $em |
|
29
|
|
|
* @param null $maxExecutionTime |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(EntityManagerInterface $em, $maxExecutionTime = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->em = $em; |
|
34
|
|
|
$this->maxExecutionTime = $maxExecutionTime; |
|
35
|
|
|
|
|
36
|
|
|
if ($maxExecutionTime === null) { |
|
37
|
|
|
$this->maxExecutionTime = ini_get('max_execution_time'); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function runTimeLimited($callback, $waitTime = 1) |
|
42
|
|
|
{ |
|
43
|
|
|
$maxExecutionTime = $this->maxExecutionTime; |
|
44
|
|
|
$limit = $maxExecutionTime ? $maxExecutionTime - 2 : 60; |
|
45
|
|
|
$startTime = time(); |
|
46
|
|
|
while ($limit > 0) { |
|
47
|
|
|
$result = call_user_func($callback); |
|
48
|
|
|
$delta = time() - $startTime; |
|
49
|
|
|
|
|
50
|
|
|
if ($result !== false) { |
|
51
|
|
|
return $result; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$limit -= $delta; |
|
55
|
|
|
if ($limit <= 0) { |
|
56
|
|
|
break; |
|
57
|
|
|
} |
|
58
|
|
|
$startTime = time(); |
|
59
|
|
|
sleep($waitTime); |
|
60
|
|
|
} |
|
61
|
|
|
throw new RequestTimeoutException("Request Timeout"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param PersonInterface $user |
|
66
|
|
|
* @param \DateTime $updatedAt |
|
67
|
|
|
* @return bool |
|
68
|
|
|
* @throws RequestTimeoutException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function waitValidEmail(PersonInterface $user, \DateTime $updatedAt) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
if ($user->getEmailConfirmedAt() instanceof \DateTime) { |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$person = $this->runTimeLimited( |
|
77
|
|
|
$this->getEntityUpdateCheckerCallback($user, $updatedAt) |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
return $person->getEmailConfirmedAt() instanceof \DateTime; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getEntityUpdateCheckerCallback(LongPollableInterface $entity, $updatedAt) |
|
84
|
|
|
{ |
|
85
|
|
|
$id = $entity->getId(); |
|
86
|
|
|
$em = $this->em; |
|
87
|
|
|
$repository = $em->getRepository(get_class($entity)); |
|
88
|
|
|
|
|
89
|
|
|
return function () use ($em, $repository, $id, $updatedAt) { |
|
90
|
|
|
$em->clear(); |
|
91
|
|
|
|
|
92
|
|
|
/** @var LongPollableInterface $entity */ |
|
93
|
|
|
$entity = $repository->find($id); |
|
94
|
|
|
if (!$entity->getUpdatedAt()) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($entity->getUpdatedAt() > $updatedAt) { |
|
99
|
|
|
return $entity; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return false; |
|
103
|
|
|
}; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.