|
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) { |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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
|
|
|
|