ScheduleApiJobsCommand::execute()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
rs 8.5806
c 1
b 0
f 0
cc 4
eloc 16
nc 4
nop 2
1
<?php
2
namespace Tarioch\EveapiFetcherBundle\Command;
3
4
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Tarioch\EveapiFetcherBundle\Entity\ApiCall;
8
9
class ScheduleApiJobsCommand extends ContainerAwareCommand
10
{
11
    /**
12
     * @inheritdoc
13
     */
14
    protected function configure()
15
    {
16
        $this->setName('eve:api:schedule')->setDescription('Schedule needed api fetches');
17
    }
18
19
    /**
20
     * @inheritdoc
21
     */
22
    protected function execute(InputInterface $input, OutputInterface $output)
23
    {
24
        $gearman = $this->getContainer()->get('gearman');
25
        $entityManager = $this->getContainer()->get('doctrine.orm.eveapi_entity_manager');
26
27
        $newKeys = $entityManager->getRepository('TariochEveapiFetcherBundle:ApiKey')->loadKeysWithoutApiCall();
28
        if (!empty($newKeys)) {
29
            $api = $entityManager->getRepository('TariochEveapiFetcherBundle:Api')->loadApiKeyInfoApi();
30
            foreach ($newKeys as $key) {
31
                $entityManager->persist(new ApiCall($api, null, $key));
32
            }
33
            $entityManager->flush();
34
        }
35
36
        $calls = $entityManager->getRepository('TariochEveapiFetcherBundle:ApiCall')->loadReadyCalls();
37
        foreach ($calls as $call) {
38
            $api = $call->getApi();
39
40
            $worker = $api->getWorker();
41
            $job = $worker . '~apiUpdate';
42
            $callId = $call->getApiCallId();
43
44
            $gearman->doBackgroundJob($job, $callId, $callId);
45
        }
46
    }
47
}
48