Completed
Push — develop ( f102ca...488b09 )
by Mathias
20:12 queued 11:00
created

FindJobsWithExternalImageJob::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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;
0 ignored issues
show
Bug introduced by
The type Core\Queue\Job\MongoJob was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Core\Queue\LoggerAwareJobTrait;
0 ignored issues
show
Bug introduced by
The type Core\Queue\LoggerAwareJobTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 static function create()
42
    {
43
        return new self();
44
    }
45
46
    public function __construct(Job $repository = null)
47
    {
48
        $this->repository = $repository;
49
    }
50
51
    public function execute()
52
    {
53
        return $this->recoverable('Test recoverable', '+12 days');
54
        if (!$this->repository) {
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
55
            return $this->failure('Cannot execute without repository.');
56
        }
57
58
        $logger = $this->getLogger();
59
        $qb = $this->repository->createQueryBuilder();
60
        $qb->field('logoRef')->equals(new \MongoDB\BSON\Regex('^https?:\/\/', 'i'));
61
        $qb->limit(10);
62
        $query = $qb->getQuery();
63
        $cursor = $query->execute();
64
65
        $queue = $this->getQueue();
66
67
        if (!$cursor->count()) {
68
            $logger->info('No jobs with external images found. Reinsert with delay 2h.');
69
            $queue->push(self::create(), ['delay' => '+2 hours']);
70
71
            return $this->success();
72
        }
73
74
        $invalidJobs = 0;
75
        foreach ($cursor->toArray() as $job) {
76
            if (0 === strpos($job->getLogoRef(), 'http')) {
77
                $queue->push(FetchExternalImageJob::create($job));
78
                $logger->debug('Found external image uri: ' . $job->getLogoRef());
79
                $logger->info('Pushed fetch image job for Job: '  . $job->getId());
80
            } else {
81
                $invalidJobs += 1;
82
            }
83
        }
84
85
        $delay = 0 >= ($cursor->count() - $invalidJobs) ? '+2 hours' : '+5 minutes';
86
        $queue->push(self::create(), ['delay' => $delay]);
87
        $logger->info('Reinserted to fetch more jobs with delay: ' . $delay);
88
    }
89
90
}
91