Completed
Push — develop ( 0de0ee...1049f6 )
by Mathias
114:58 queued 107:28
created

FindJobsWithExternalImageJob   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
eloc 29
dl 0
loc 54
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B execute() 0 38 6
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2019 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Jobs\Queue;
12
13
use Core\Queue\Exception\FatalJobException;
14
use Core\Queue\Job\MongoJob;
15
use Core\Queue\LoggerAwareJobTrait;
16
use Jobs\Repository\Job;
17
use SlmQueue\Job\AbstractJob;
18
use SlmQueue\Queue\QueueAwareInterface;
19
use SlmQueue\Queue\QueueAwareTrait;
20
use SlmQueue\Queue\QueueInterface;
21
use Zend\Log\LoggerAwareInterface;
22
use Zend\Log\LoggerInterface;
23
24
/**
25
 * ${CARET}
26
 * 
27
 * @author Mathias Gelhausen <[email protected]>
28
 * @todo write test 
29
 */
30
class FindJobsWithExternalImageJob extends MongoJob implements QueueAwareInterface, LoggerAwareInterface
31
{
32
    use QueueAwareTrait, LoggerAwareJobTrait;
33
34
    /**
35
     *
36
     *
37
     * @var Job
38
     */
39
    private $repository;
40
41
    public function __construct(Job $repository = null)
42
    {
43
        $this->repository = $repository;
44
    }
45
46
    public function execute()
47
    {
48
        if (!$this->repository) {
49
            return $this->failure('Cannot execute without repository.');
50
        }
51
52
        $logger = $this->getLogger();
53
        $qb = $this->repository->createQueryBuilder();
54
        $qb->field('logoRef')->equals(new \MongoDB\BSON\Regex('^https?:\/\/', 'i'));
55
        $qb->limit(10);
56
        $query = $qb->getQuery();
57
        $cursor = $query->execute();
58
59
        $queue = $this->getQueue();
60
61
        if (!$cursor->count()) {
62
            $logger->info('No jobs with external images found. Reinsert with delay 2h.');
63
            $queue->push(self::create(), ['delay' => '+2 hours']);
64
65
            return $this->success();
66
        }
67
68
        $invalidJobs = 0;
69
        foreach ($cursor->toArray() as $job) {
70
            if (0 === strpos($job->getLogoRef(), 'http')) {
71
                $queue->push(FetchExternalImageJob::create($job));
72
                $logger->debug('Found external image uri: ' . $job->getLogoRef());
73
                $logger->info('Pushed fetch image job for Job: '  . $job->getId());
74
            } else {
75
                $invalidJobs += 1;
76
            }
77
        }
78
79
        $delay = 0 >= ($cursor->count() - $invalidJobs) ? '+2 hours' : '+5 minutes';
80
        $queue->push(self::create(), ['delay' => $delay]);
81
        $logger->info('Reinserted to fetch more jobs with delay: ' . $delay);
82
83
        return $this->success();
84
    }
85
86
}
87