loadOrCreateWalletDivision()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 0
loc 12
rs 9.4285
c 2
b 0
f 2
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
namespace Tarioch\EveapiFetcherBundle\Component\EveApi\Corp;
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\CorpCorporationSheet;
9
use Tarioch\EveapiFetcherBundle\Entity\CorpLogo;
10
use Tarioch\EveapiFetcherBundle\Entity\CorpDivision;
11
use Tarioch\EveapiFetcherBundle\Entity\CorpWalletDivision;
12
13
/**
14
 * @DI\Service("tarioch.eveapi.corp.CorporationSheet")
15
 */
16
class CorporationSheetUpdater extends AbstractCorpUpdater
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public function update(ApiCall $call, ApiKey $key, Pheal $pheal)
22
    {
23
        $charId = $call->getOwner()->getCharacterId();
24
        $api = $pheal->corpScope->CorporationSheet(array('characterID' => $charId));
25
26
        $entity = $this->loadOrCreate($api->corporationID);
27
        $entity->setCorporationName($api->corporationName);
28
        $entity->setTicker($api->ticker);
29
        $entity->setCeoId($api->ceoID);
30
        $entity->setCeoName($api->ceoName);
31
        $entity->setStationId($api->stationID);
32
        $entity->setStationName($api->stationName);
33
        $entity->setDescription($api->description);
34
        $entity->setUrl($api->url);
35
        $entity->setAllianceId($api->allianceID);
36
        $entity->setAllianceName($api->allianceName);
37
        $entity->setTaxRate($api->taxRate);
38
        $entity->setMemberCount($api->memberCount);
39
        $entity->setMemberLimit($api->memberLimit);
40
        $entity->setShares($api->shares);
41
42
        $logoEntity = $entity->getLogo();
43
        $logoApi = $api->logo;
44
        $logoEntity->setGraphicId($logoApi->graphicID);
45
        $logoEntity->setShape1($logoApi->shape1);
46
        $logoEntity->setShape2($logoApi->shape2);
47
        $logoEntity->setShape3($logoApi->shape3);
48
        $logoEntity->setColor1($logoApi->color1);
49
        $logoEntity->setColor2($logoApi->color2);
50
        $logoEntity->setColor3($logoApi->color3);
51
52
        foreach ($api->divisions as $division) {
53
            $accountKey = $division->accountKey;
54
55
            $divisionEntity = $this->loadOrCreateDivision($entity, $accountKey);
56
            $divisionEntity->setDescription($division->description);
57
        }
58
59
        foreach ($api->walletDivisions as $division) {
60
            $accountKey = $division->accountKey;
61
62
            $divisionEntity = $this->loadOrCreateWalletDivision($entity, $accountKey);
63
            $divisionEntity->setDescription($division->description);
64
        }
65
66
        return $api->cached_until;
67
    }
68
69
    private function loadOrCreate($corporationId)
70
    {
71
        $entity = $this->entityManager->find('TariochEveapiFetcherBundle:CorpCorporationSheet', $corporationId);
72
        if ($entity === null) {
73
            $entity = new CorpCorporationSheet($corporationId);
74
            $entity->setLogo(new CorpLogo());
75
            $this->entityManager->persist($entity);
76
            $this->entityManager->flush($entity);
77
        }
78
79
        return $entity;
80
    }
81
82
    private function loadOrCreateDivision(CorpCorporationSheet $corporation, $accountKey)
83
    {
84
        $repo = $this->entityManager->getRepository('TariochEveapiFetcherBundle:CorpDivision');
85
        $entity = $repo->findOneBy(array('corporation' => $corporation, 'accountKey' => $accountKey));
86
        if ($entity === null) {
87
            $entity = new CorpDivision($accountKey, $corporation);
88
            $this->entityManager->persist($entity);
89
            $this->entityManager->flush($entity);
90
        }
91
92
        return $entity;
93
    }
94
95
    private function loadOrCreateWalletDivision(CorpCorporationSheet $corporation, $accountKey)
96
    {
97
        $repo = $this->entityManager->getRepository('TariochEveapiFetcherBundle:CorpWalletDivision');
98
        $entity = $repo->findOneBy(array('corporation' => $corporation, 'accountKey' => $accountKey));
99
        if ($entity === null) {
100
            $entity = new CorpWalletDivision($accountKey, $corporation);
101
            $this->entityManager->persist($entity);
102
            $this->entityManager->flush($entity);
103
        }
104
105
        return $entity;
106
    }
107
}
108