Completed
Push — master ( 22f213...24f6ce )
by Guilherme
17:25
created

LongPollingUtils   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
B runTimeLimited() 0 22 5
A waitValidEmail() 0 12 2
A getEntityUpdateCheckerCallback() 0 22 3
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');
0 ignored issues
show
Documentation Bug introduced by
The property $maxExecutionTime was declared of type integer, but ini_get('max_execution_time') is of type string. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
function waitValidEmail() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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