AccountBalanceUpdater   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 17 2
A loadOrCreate() 0 10 2
1
<?php
2
namespace Tarioch\EveapiFetcherBundle\Component\EveApi\Char;
3
4
use JMS\DiExtraBundle\Annotation as DI;
5
use Tarioch\EveapiFetcherBundle\Entity\ApiCall;
6
use Pheal\Pheal;
7
use Tarioch\EveapiFetcherBundle\Entity\ApiKey;
8
use Tarioch\EveapiFetcherBundle\Entity\CharAccountBalance;
9
10
/**
11
 * @DI\Service("tarioch.eveapi.char.AccountBalance")
12
 */
13
class AccountBalanceUpdater extends AbstractCharUpdater
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    public function update(ApiCall $call, ApiKey $key, Pheal $pheal)
19
    {
20
        $owner = $call->getOwner();
21
        $charId = $owner->getCharacterId();
22
        $api = $pheal->charScope->AccountBalance(array('characterID' => $charId));
23
24
        foreach ($api->accounts as $account) {
25
            $entity = $this->loadOrCreate($account->accountID);
26
27
            $entity->setOwnerId($charId);
28
            $entity->setAccountKey($account->accountKey);
29
            $entity->setBalance($account->balance);
30
            $this->entityManager->flush($entity);
31
        }
32
33
        return $api->cached_until;
34
    }
35
36
    private function loadOrCreate($accountId)
37
    {
38
        $entity = $this->entityManager->find('TariochEveapiFetcherBundle:CharAccountBalance', $accountId);
39
        if ($entity === null) {
40
            $entity = new CharAccountBalance($accountId);
41
            $this->entityManager->persist($entity);
42
        }
43
44
        return $entity;
45
    }
46
}
47