AddAdImages.php
3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalAdImage;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
class AddAdImages extends DirectRequest
{
protected $max_count = 100;
protected $timestamp;
/* @var Collection|GoalAdImage[] */
protected $goalAdImages;
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
return count($this->getParams()['AdImages']);
}
public function slice($maxObjects): ?APIRequest
{
$splinter = $this->sliceByKey($maxObjects, 'AdImages');
$splinter->putParams([
'goalAdImages' => $this->goalAdImages->slice($maxObjects)->values(),
]);
$this->putParams([
'goalAdImages' => $this->goalAdImages->slice(0, $maxObjects),
]);
return $splinter;
}
public function handle($response)
{
try {
if (!isset($response['result']['AddResults'])) {
return;
}
foreach ($response['result']['AddResults'] as $key => $add_result) {
$goalAdImage = $this->goalAdImages->get($key);
if (!isset($add_result['AdImageHash'])) {
Log::debug("AddAdImage, empty AdImageHash");
Log::debug($add_result);
Log::debug($this->getParams()['AdImages'][$key]['Name']);
$goalAdImage->where('id', $goalAdImage->getKey())
->update([
'reserve_create_at' => null,
]);
continue;
}
$hash = (string)$add_result['AdImageHash'];
$goalAdImage->where('id', $goalAdImage->getKey())
->update([
'hash' => $hash,
'external_upload_at' => Carbon::now(),
'reserve_create_at' => null,
]);
}
} catch (\Exception $e) {
Log::debug($e);
throw $e;
}
}
public function failed()
{
GoalAdImage::whereIn('id', $this->goalAdImages->pluck('id')->toArray())
->update([
'reserve_create_at' => null,
]);
}
public function putParams($params)
{
$this->goalAdImages = $params['goalAdImages'];
}
private function requestPrepare($params)
{
$this->setService('AdImages');
$this->setMethod('add');
$this->putParams($params);
$this->setParams([
'AdImages' => $this->goalAdImages->map(function (GoalAdImage $goalAdImage) {
return [
'Name' => $goalAdImage->adImage->name,
'ImageData' => base64_encode($goalAdImage->adImage->imageData),
];
})->toArray(),
]);
}
}