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

FetchExternalImageJob   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 41
dl 0
loc 77
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
B execute() 0 58 7
A __construct() 0 3 1
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\Exception\RecoverableJobException;
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 Jobs\Entity\JobInterface as JobEntityInterface;
19
use Zend\Http\Client;
20
use Zend\Log\LoggerAwareInterface;
21
22
/**
23
 * ${CARET}
24
 * 
25
 * @author Mathias Gelhausen <[email protected]>
26
 * @todo write test 
27
 */
28
class FetchExternalImageJob extends AbstractJob implements LoggerAwareInterface
29
{
30
    use LoggerAwareJobTrait;
31
32
    private $repository;
33
34
    public static function create(JobEntityInterface $jobEntity)
35
    {
36
        $job = new self();
37
        $job->setContent(['jobId' => $jobEntity->getId()]);
38
39
        return $job;
40
    }
41
42
    public function __construct(Job $repository = null)
43
    {
44
        $this->repository = $repository;
45
    }
46
47
    public function execute()
48
    {
49
        $logger = $this->getLogger();
50
51
        if (!$this->repository) {
52
            $logger->err('Cannot execute without repository.');
53
54
            throw new FatalJobException('Cannot execute without repository.');
55
        }
56
        $payload = $this->getContent();
57
58
        if (!isset($payload['jobId'])) {
59
            $logger->err('Missing jobId in playload.');
60
            throw new FatalJobException('Missing jobId in playload.');
61
        }
62
        /* @var \Jobs\Entity\Job $jobEntity */
63
        $jobEntity = $this->repository->find($payload['jobId']);
64
65
        if (!$jobEntity) {
0 ignored issues
show
introduced by
$jobEntity is of type Jobs\Entity\Job, thus it always evaluated to true.
Loading history...
66
            $logger->err('No job entity with the id ' . $payload['jobId'] . ' was found.');
67
68
            throw new FatalJobException('No job entity with the id ' . $payload['jobId'] . ' was found.');
69
        }
70
71
        $uri = $jobEntity->getLogoRef();
72
73
        if (0 !== strpos($uri, 'http')) {
74
            $logger->notice('logoRef seems not to be external: ' . $uri);
75
            $logger->info('Skip fetching for this job.');
76
            return;
77
        }
78
79
        $logger->debug('Trying to fetch image from ' . $uri);
80
81
        $client = new Client($uri);
82
        $response = $client->send();
83
84
        if (200 != $response->getStatusCode()) {
85
            $logger->err('Received status code ' . $response->getStatusCode() . ' when trying to fetch ' . $uri);
86
87
            throw new RecoverableJobException('Status code ' . $response->getStatusCode() . ' received.', ['delay' => '+5 minutes']);
88
        }
89
90
        $content = $response->getBody();
91
        $type = $response->getHeaders()->get('Content-Type')->getFieldValue();
0 ignored issues
show
Bug introduced by
The method getFieldValue() does not exist on ArrayIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        $type = $response->getHeaders()->get('Content-Type')->/** @scrutinizer ignore-call */ getFieldValue();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
        list(,$ext) = explode('/', $type, 2);
93
        $imageName = '/static/Jobs/logos/' . $jobEntity->getId() . '.' . $ext;
94
95
        if (false === file_put_contents("public$imageName", $content, FILE_BINARY)) {
96
            $logger->err('Writing image failed.');
97
98
            throw new FatalJobException('Writing image failed.');
99
        }
100
101
        $logger->info('Saved job logo as ' . basename($imageName));
102
        $jobEntity->setLogoRef($imageName);
103
        $this->repository->store($jobEntity);
104
        $logger->info('Saved job logo as ' . basename($imageName));
105
106
    }
107
}
108