WalletTransactionUpdater::update()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 50
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
dl 0
loc 50
rs 8.8571
c 4
b 0
f 2
cc 4
eloc 36
nc 4
nop 3
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\CharWalletTransaction;
9
10
/**
11
 * @DI\Service("tarioch.eveapi.char.WalletTransactions")
12
 */
13
class WalletTransactionUpdater 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->WalletTransactions(array(
31
                'characterID' => $charId,
32
                'rowCount' => 2560,
33
                'accountKey' => $accountKey
34
            ));
35
            $cached = $api->cached_until;
36
37
            $repo = $this->entityManager->getRepository('TariochEveapiFetcherBundle:CharWalletTransaction');
38
            foreach ($api->transactions as $transaction) {
39
                $transId = $transaction->transactionID;
40
41
                $entity = $repo->findOneBy(array('transactionId' => $transId, 'ownerId' => $charId));
42
                if ($entity === null) {
43
                    $entity = new CharWalletTransaction($transId, $charId);
44
                    $this->entityManager->persist($entity);
45
46
                    $entity->setAccountKey($accountKey);
47
                    $entity->setJournalTransactionId($transaction->journalTransactionID);
48
                    $entity->setTransactionDateTime(new \DateTime($transaction->transactionDateTime));
49
                    $entity->setQuantity($transaction->quantity);
50
                    $entity->setTypeName($transaction->typeName);
51
                    $entity->setTypeId($transaction->typeID);
52
                    $entity->setPrice($transaction->price);
53
                    $entity->setClientId($transaction->clientID);
54
                    $entity->setClientName($transaction->clientName);
55
                    $entity->setClientTypeId($transaction->clientTypeID);
56
                    $entity->setStationId($transaction->stationID);
57
                    $entity->setStationName($transaction->stationName);
58
                    $entity->setTransactionType($transaction->transactionType);
59
                    $entity->setTransactionFor($transaction->transactionFor);
60
61
                    $this->entityManager->flush($entity);
62
                }
63
            }
64
        }
65
66
        return $cached;
67
    }
68
}
69