EveWorker::apiUpdate()   B
last analyzed

Complexity

Conditions 4
Paths 16

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 0
Metric Value
dl 0
loc 26
rs 8.5806
c 7
b 2
f 0
cc 4
eloc 19
nc 16
nop 1
1
<?php
2
namespace Tarioch\EveapiFetcherBundle\Component\Worker;
3
4
use Mmoreram\GearmanBundle\Driver\Gearman;
5
use JMS\DiExtraBundle\Annotation as DI;
6
use Doctrine\ORM\EntityManager;
7
use Psr\Log\LoggerInterface;
8
9
/**
10
 * @DI\Service("tarioch.eveapi.worker.eve")
11
 * @Gearman\Work(name = "TariochEveapiFetcherEveWorker", service = "tarioch.eveapi.worker.eve")
12
 */
13
class EveWorker
14
{
15
    private $logger;
16
    private $entityManager;
17
    private $apiUpdater;
18
19
    /**
20
     * @DI\InjectParams({
21
     * "entityManager" = @DI\Inject("doctrine.orm.eveapi_entity_manager"),
22
     * "logger" = @DI\Inject("logger"),
23
     * "apiUpdater" = @DI\Inject("tarioch.eveapi_fetcher_bundle.component.worker.api_updater")
24
     * })
25
     */
26
    public function __construct(EntityManager $entityManager, LoggerInterface $logger, ApiUpdater $apiUpdater)
27
    {
28
        $this->entityManager = $entityManager;
29
        $this->logger = $logger;
30
        $this->apiUpdater = $apiUpdater;
31
    }
32
33
    /**
34
     * @Gearman\Job()
35
     */
36
    public function apiUpdate(\GearmanJob $job)
37
    {
38
        $apiCallId = '?';
39
40
        try {
41
            $apiCallId = $job->workload();
42
            $this->entityManager->getConnection()->beginTransaction();
43
44
            $this->apiUpdater->update($apiCallId);
45
46
            $this->entityManager->flush();
47
            $this->entityManager->getConnection()->commit();
48
        } catch (\Exception $e) {
49
            $this->entityManager->getConnection()->rollback();
50
            $this->entityManager->close();
51
            $details = array('callId' => $apiCallId, 'exception' => $e);
52
            if (strstr($e->getMessage(), '1213 Deadlock found when trying to get lock; try restarting transaction')) {
53
                $this->logger->info('{callId}: Deadlock', $details);
54
            } elseif (strstr($e->getMessage(), '1205 Lock wait timeout exceeded; try restarting transaction')) {
55
                $this->logger->info('{callId}: Lock wait timeout', $details);
56
            } else {
57
                $this->logger->critical('{callId}: Unhandled exception', $details);
58
            }
59
            throw $e;
60
        }
61
    }
62
}
63