1
|
|
|
<?php |
2
|
|
|
namespace Tarioch\EveapiFetcherBundle\Component\EveApi\Account\ApiKeyInfo; |
3
|
|
|
|
4
|
|
|
use JMS\DiExtraBundle\Annotation as DI; |
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Tarioch\EveapiFetcherBundle\Entity\ApiCall; |
7
|
|
|
use Pheal\Pheal; |
8
|
|
|
use Pheal\Core\Element; |
9
|
|
|
use Tarioch\EveapiFetcherBundle\Component\EveApi\KeyApi; |
10
|
|
|
use Tarioch\EveapiFetcherBundle\Entity\ApiKey; |
11
|
|
|
use Tarioch\EveapiFetcherBundle\Entity\AccountAPIKeyInfo; |
12
|
|
|
use Tarioch\EveapiFetcherBundle\Entity\AccountCharacter; |
13
|
|
|
use Tarioch\EveapiFetcherBundle\Component\EveApi\Account\ApiKeyInfo\NewApiFactory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @DI\Service("tarioch.eveapi.account.APIKeyInfo") |
17
|
|
|
*/ |
18
|
|
|
class ApiKeyInfoUpdater implements KeyApi |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
private $entityManager; |
22
|
|
|
private $currentApiCallFactory; |
23
|
|
|
private $newApiFactory; |
24
|
|
|
private $diffCalculator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @DI\InjectParams({ |
28
|
|
|
* "entityManager" = @DI\Inject("doctrine.orm.eveapi_entity_manager"), |
29
|
|
|
* "currentApiCallFactory" = @DI\Inject("tarioch.eveapi.account.api_key_info.current_api_call_factory"), |
30
|
|
|
* "newApiFactory" = @DI\Inject("tarioch.eveapi.account.api_key_info.new_api_factory"), |
31
|
|
|
* "diffCalculator" = @DI\Inject("tarioch.eveapi.account.api_key_info.diff_calculator") |
32
|
|
|
* }) |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
EntityManager $entityManager, |
36
|
|
|
CurrentApiCallFactory $currentApiCallFactory, |
37
|
|
|
NewApiFactory $newApiFactory, |
38
|
|
|
DiffCalculator $diffCalculator |
39
|
|
|
) { |
40
|
|
|
$this->entityManager = $entityManager; |
41
|
|
|
$this->currentApiCallFactory = $currentApiCallFactory; |
42
|
|
|
$this->newApiFactory = $newApiFactory; |
43
|
|
|
$this->diffCalculator = $diffCalculator; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function update(ApiCall $call, ApiKey $key, Pheal $pheal) |
50
|
|
|
{ |
51
|
|
|
$api = $pheal->accountScope->APIKeyInfo(); |
52
|
|
|
$apiKey = $api->key; |
53
|
|
|
|
54
|
|
|
$this->updateApiKeyInfo($key, $apiKey); |
55
|
|
|
|
56
|
|
|
// update associated characters |
57
|
|
|
$repository = $this->entityManager->getRepository('TariochEveapiFetcherBundle:AccountCharacter'); |
58
|
|
|
$characterEntities = $repository->findByKey($key); |
59
|
|
|
$charEntityMap = array(); |
60
|
|
|
foreach ($characterEntities as $characterEntity) { |
61
|
|
|
$charEntityMap[$characterEntity->getCharacterId()] = $characterEntity; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$chars = array(); |
65
|
|
|
foreach ($apiKey->characters as $char) { |
66
|
|
|
$charId = $char->characterID; |
67
|
|
|
if (isset($charEntityMap[$charId])) { |
68
|
|
|
$charEntity = $charEntityMap[$charId]; |
69
|
|
|
unset($charEntityMap[$charId]); |
70
|
|
|
} else { |
71
|
|
|
$charEntity = new AccountCharacter($key, $charId); |
72
|
|
|
$this->entityManager->persist($charEntity); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$charEntity->setCharacterName($char->characterName); |
76
|
|
|
|
77
|
|
|
$corpId = $char->corporationID; |
78
|
|
|
$charEntity->setCorporationId($corpId); |
79
|
|
|
$charEntity->setCorporationName($char->corporationName); |
80
|
|
|
|
81
|
|
|
$this->entityManager->flush($charEntity); |
82
|
|
|
$chars[] = $charEntity->getId(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// remove old, no longer valid characters |
86
|
|
|
foreach ($charEntityMap as $characterEntity) { |
87
|
|
|
$this->entityManager->remove($characterEntity); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->updateApiCalls($key, $apiKey->accessMask, $apiKey->type, $chars); |
91
|
|
|
|
92
|
|
|
return $api->cached_until; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function updateApiCalls(ApiKey $key, $accessMask, $keyType, array $chars) |
96
|
|
|
{ |
97
|
|
|
$currentApiCallMap = $this->currentApiCallFactory->createCurrentApiCallMap($key); |
98
|
|
|
$newApiMap = $this->newApiFactory->createNewApiMap($accessMask, $keyType, $chars); |
99
|
|
|
$apisToAdd = $this->diffCalculator->getOnlyInSource($newApiMap, $currentApiCallMap); |
100
|
|
|
|
101
|
|
|
foreach ($apisToAdd as $apis) { |
102
|
|
|
foreach ($apis as $ownerId => $api) { |
103
|
|
|
if ($ownerId != 0) { |
104
|
|
|
$owner = $this->entityManager->find('TariochEveapiFetcherBundle:AccountCharacter', $ownerId); |
105
|
|
|
} else { |
106
|
|
|
$owner = null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->entityManager->persist(new ApiCall($api, $owner, $key)); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$apisToRemove = $this->diffCalculator->getOnlyInSource($currentApiCallMap, $newApiMap); |
114
|
|
|
foreach ($apisToRemove as $apis) { |
115
|
|
|
foreach ($apis as $calls) { |
116
|
|
|
$this->entityManager->remove($calls); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function updateApiKeyInfo(ApiKey $key, Element $apiKey) |
122
|
|
|
{ |
123
|
|
|
$entity = $this->loadOrCreate($key); |
124
|
|
|
$entity->setAccessMask($apiKey->accessMask); |
125
|
|
|
if (! empty($apiKey->expires)) { |
126
|
|
|
$entity->setExpires(new \DateTime($apiKey->expires)); |
127
|
|
|
} |
128
|
|
|
$entity->setType($apiKey->type); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function loadOrCreate(ApiKey $key) |
132
|
|
|
{ |
133
|
|
|
$repository = $this->entityManager->getRepository('TariochEveapiFetcherBundle:AccountAPIKeyInfo'); |
134
|
|
|
$entity = $repository->findOneByKey($key); |
135
|
|
|
if ($entity === null) { |
136
|
|
|
$entity = new AccountAPIKeyInfo($key); |
137
|
|
|
$this->entityManager->persist($entity); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $entity; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|