ApiTimeCalculator::isCallStillValid()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 3
eloc 3
nc 3
nop 1
1
<?php
2
namespace Tarioch\EveapiFetcherBundle\Component\Worker;
3
4
use JMS\DiExtraBundle\Annotation as DI;
5
use Tarioch\EveapiFetcherBundle\Entity\ApiCall;
6
7
/**
8
 * @DI\Service(public=false)
9
 */
10
class ApiTimeCalculator
11
{
12
    /**
13
     * @param ApiCall|null $call
14
     * @return boolean
15
     */
16
    public function isCallStillValid(ApiCall $call = null)
17
    {
18
        $now = new \DateTime('now', new \DateTimeZone('UTC'));
19
        return $call !== null && $call->getCachedUntil() < $now && $call->getEarliestNextCall() < $now;
20
    }
21
22
    public function calculateEarliestNextCall(ApiCall $call)
23
    {
24
        $minutesToEarliestNextCall = $call->getApi()->getCallInterval() * (1 + $call->getErrorCount());
25
        $earliestNextCall = new \DateTime('now', new \DateTimeZone('UTC'));
26
        $earliestNextCall->add(new \DateInterval('PT' . $minutesToEarliestNextCall . 'M'));
27
28
        return $earliestNextCall;
29
    }
30
}
31