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; |
|
|
|
|
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) { |
|
|
|
|
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(); |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths