WalletJournalUpdater   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B update() 0 52 5
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\CharWalletJournal;
9
10
/**
11
 * @DI\Service("tarioch.eveapi.char.WalletJournal")
12
 */
13
class WalletJournalUpdater 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
23
        $accountRepo = $this->entityManager->getRepository('TariochEveapiFetcherBundle:CharAccountBalance');
24
        $accounts = $accountRepo->findByOwnerId($charId);
25
26
        $cached = 'now';
27
        foreach ($accounts as $account) {
28
            $accountKey = $account->getAccountKey();
29
30
            $api = $pheal->charScope->WalletJournal(array(
31
                'characterID' => $charId,
32
                'rowCount' => 2560,
33
                'accountKey' => $accountKey
34
            ));
35
            $cached = $api->cached_until;
36
37
            $repo = $this->entityManager->getRepository('TariochEveapiFetcherBundle:CharWalletJournal');
38
            foreach ($api->transactions as $entry) {
39
                $refId = $entry->refID;
40
41
                $entity = $repo->findOneBy(array('refId' => $refId, 'ownerId' => $charId));
42
                if ($entity === null) {
43
                    $entity = new CharWalletJournal($refId, $charId);
44
                    $this->entityManager->persist($entity);
45
46
                    $entity->setAccountKey($accountKey);
47
                    $entity->setDate(new \DateTime($entry->date));
48
                    $entity->setRefTypeId($entry->refTypeID);
49
                    $entity->setOwnerName1($entry->ownerName1);
50
                    $entity->setOwnerId1($entry->ownerID1);
51
                    $entity->setOwnerName2($entry->ownerName2);
52
                    $entity->setOwnerId2($entry->ownerID2);
53
                    $entity->setArgName1($entry->argName1);
54
                    $entity->setArgId1($entry->argID1);
55
                    $entity->setAmount($entry->amount);
56
                    $entity->setBalance($entry->balance);
57
                    $entity->setReason($entry->reason);
58
                    $entity->setOwner1TypeId($entry->owner1TypeID);
59
                    $entity->setOwner2TypeId($entry->owner2TypeID);
60
                    $entity->setTaxAmount((float) $entry->taxAmount);
61
                    $entity->setTaxReceiverId($entry->taxReceiverID === '' ? null : $entry->taxReceiverID);
62
63
                    $this->entityManager->flush($entity);
64
                }
65
            }
66
        }
67
68
        return $cached;
69
    }
70
}
71