AssetUpdater::addAssets()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 34
rs 8.439
c 1
b 0
f 0
cc 5
eloc 23
nc 9
nop 5
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\CorpAsset;
9
10
/**
11
 * @DI\Service("tarioch.eveapi.corp.AssetList")
12
 */
13
class AssetUpdater extends AbstractCorpUpdater
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    public function update(ApiCall $call, ApiKey $key, Pheal $pheal)
19
    {
20
        $owner = $call->getOwner();
21
        $corpId = $owner->getCorporationId();
22
        $api = $pheal->corpScope->AssetList();
23
24
        $this->entityManager
25
            ->createQuery('delete from TariochEveapiFetcherBundle:CorpAsset c where c.ownerId=:ownerId')
26
            ->setParameter('ownerId', $corpId)
27
            ->execute();
28
29
        $this->addAssets($api->assets, 0, 0, $corpId);
30
31
        return $api->cached_until;
32
    }
33
34
    private function addAssets($assets, $lvl, $count, $corpId, $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 CorpAsset();
44
            $entity->setOwnerId($corpId);
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, $corpId, $locationId);
60
            }
61
            $entity->setRight($count++);
62
63
            $this->entityManager->persist($entity);
64
        }
65
66
        return $count;
67
    }
68
}
69