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

FetchExternalImageJob::filterPayload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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\Job\MongoJob;
14
use Core\Queue\LoggerAwareJobTrait;
15
use Jobs\Repository\Job;
16
use Jobs\Entity\JobInterface as JobEntityInterface;
17
use Zend\Http\Client;
18
use Zend\Log\LoggerAwareInterface;
19
20
/**
21
 * ${CARET}
22
 * 
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @todo write test 
25
 */
26
class FetchExternalImageJob extends MongoJob implements LoggerAwareInterface
27
{
28
    use LoggerAwareJobTrait;
29
30
    private $repository;
31
32
    protected static function filterPayload($payload)
33
    {
34
        if ($payload instanceOf JobEntityInterface) {
35
            return ['jobId' => $payload->getId()];
36
        }
37
38
        return parent::filterPayload($payload);
39
    }
40
41
    public function __construct(Job $repository = null)
42
    {
43
        $this->repository = $repository;
44
    }
45
46
    public function execute()
47
    {
48
        $logger = $this->getLogger();
49
50
        if (!$this->repository) {
51
            return $this->failure('Cannot execute without repository.');
52
        }
53
        $payload = $this->getContent();
54
55
        if (!isset($payload['jobId'])) {
56
            return $this->failure('Missing jobId in playload.');
57
        }
58
        /* @var \Jobs\Entity\Job $jobEntity */
59
        $jobEntity = $this->repository->find($payload['jobId']);
60
61
        if (!$jobEntity) {
0 ignored issues
show
introduced by
$jobEntity is of type Jobs\Entity\Job, thus it always evaluated to true.
Loading history...
62
            return $this->failure('No job entity with the id ' . $payload['jobId'] . ' was found.');
63
        }
64
65
        $uri = $jobEntity->getLogoRef();
66
67
        if (0 !== strpos($uri, 'http')) {
68
            $logger->notice('logoRef seems not to be external: ' . $uri);
69
            return $this->success('Skip fetching for this job.');
70
        }
71
72
        $logger->debug('Trying to fetch image from ' . $uri);
73
74
        $client = new Client($uri);
75
        $response = $client->send();
76
77
        if (200 != $response->getStatusCode()) {
78
            $logger->err('Received status code ' . $response->getStatusCode() . ' when trying to fetch ' . $uri);
79
80
            return $this->recoverable('Status code ' . $response->getStatusCode() . ' received.', ['delay' => '+5 minutes']);
81
        }
82
83
        $content = $response->getBody();
84
        $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

84
        $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...
85
        list(,$ext) = explode('/', $type, 2);
86
        $imageName = '/static/Jobs/logos/' . $jobEntity->getId() . '.' . $ext;
87
88
        if (false === file_put_contents("public$imageName", $content, FILE_BINARY)) {
89
            return $this->failure('Writing image to "public' . $imageName . '" failed.');
90
        }
91
92
        $logger->info('Saved job logo as ' . basename($imageName));
93
        $jobEntity->setLogoRef($imageName);
94
        $this->repository->store($jobEntity);
95
        $logger->info('Saved job logo as ' . basename($imageName));
96
97
        return $this->success();
98
    }
99
}
100