AssetUpdater::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 10
nc 1
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\CharAsset;
9
10
/**
11
 * @DI\Service("tarioch.eveapi.char.AssetList")
12
 */
13
class AssetUpdater 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->AssetList(array('characterID' => $charId));
23
24
        $this->entityManager
25
            ->createQuery('delete from TariochEveapiFetcherBundle:CharAsset c where c.ownerId=:ownerId')
26
            ->setParameter('ownerId', $charId)
27
            ->execute();
28
29
        $this->addAssets($api->assets, 0, 0, $charId);
30
31
        return $api->cached_until;
32
    }
33
34
    private function addAssets($assets, $lvl, $count, $charId, $parentLocationId = null)
35
    {
36
        foreach ($assets as $asset) {
37
            if (isset($asset->locationID)) {
38
                $locationId = $asset->locationID;
39
            } else {
40
                $locationId = $parentLocationId;
41
            }
42
43
            $entity = new CharAsset();
44
            $entity->setOwnerId($charId);
45
            $entity->setLocationId($locationId);
46
            $entity->setItemId($asset->itemID);
47
            $entity->setTypeId($asset->typeID);
48
            $entity->setQuantity($asset->quantity);
49
            if (isset($asset->rawQuantity)) {
50
                $entity->setRawQuantity($asset->rawQuantity);
51
            }
52
            $entity->setFlag($asset->flag);
53
            $entity->setSingleton(filter_var($asset->singleton, FILTER_VALIDATE_BOOLEAN));
54
55
            // Nested Set Algorithm: http://en.wikipedia.org/wiki/Nested_set_model
56
            $entity->setLevel($lvl);
57
            $entity->setLeft($count++);
58
            if (!empty($asset->contents)) {
59
                $count = $this->addAssets($asset->contents, $lvl + 1, $count, $charId, $locationId);
60
            }
61
            $entity->setRight($count++);
62
63
            $this->entityManager->persist($entity);
64
        }
65
66
        return $count;
67
    }
68
}
69