KeySectionApi::update()   B
last analyzed

Complexity

Conditions 4
Paths 10

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 8.7972
cc 4
eloc 17
nc 10
nop 1
1
<?php
2
namespace Tarioch\EveapiFetcherBundle\Component\Section;
3
4
use JMS\DiExtraBundle\Annotation as DI;
5
use Tarioch\PhealBundle\DependencyInjection\PhealFactory;
6
use Tarioch\EveapiFetcherBundle\Entity\ApiCall;
7
use Tarioch\EveapiFetcherBundle\Entity\Api;
8
use Tarioch\EveapiFetcherBundle\Component\EveApi\SpecificApiFactory;
9
use Pheal\Exceptions\PhealException;
10
11
/**
12
 * @DI\Service(public=false)
13
 */
14
class KeySectionApi implements SectionApi
15
{
16
    const ERROR_MAX = 20;
17
18
    private $phealFactory;
19
    private $specificApiFactory;
20
21
    /**
22
     * @DI\InjectParams({
23
     * "phealFactory" = @DI\Inject("tarioch.pheal.factory"),
24
     * "specificApiFactory" = @DI\Inject("tarioch.eveapi_fetcher_bundle.component.eve_api.specific_api_factory")
25
     * })
26
     */
27
    public function __construct(
28
        PhealFactory $phealFactory,
29
        SpecificApiFactory $specificApiFactory
30
    ) {
31
        $this->phealFactory = $phealFactory;
32
        $this->specificApiFactory = $specificApiFactory;
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function update(ApiCall $call)
39
    {
40
        $key = $call->getKey();
41
        if ($key->isActive()) {
42
            $pheal = $this->phealFactory->createEveOnline($key->getKeyId(), $key->getVcode());
43
44
            try {
45
                $updateService = $this->specificApiFactory->create($call->getApi());
46
                $cachedUntil = $updateService->update($call, $key, $pheal);
47
                $key->clearErrorCount();
48
                return new \DateTime($cachedUntil);
49
            } catch (PhealException $e) {
50
                $key->increaseErrorCount();
51
                if ($key->getErrorCount() > self::ERROR_MAX) {
52
                    $key->setActive(false);
53
                }
54
                throw $e;
55
            }
56
        } else {
57
            $call->setActive(false);
58
            return $call->getCachedUntil();
59
        }
60
    }
61
}
62