AddAdExtensions.php
3.69 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
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace App\Service\Requests\Direct;
use App\Jobs\ProcessCallLimitedAPI;
use App\Models\Pivots\GoalAdExtension;
use App\Models\Variable;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use App\Service\StrReplaceByVariables;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;
class AddAdExtensions extends DirectRequest
{
protected $max_count = 1000;
protected $timestamp;
/* @var Collection|GoalAdExtension[] */
protected $goalAdExtensions;
public function call($params = null)
{
$this->requestPrepare($params);
$process = new ProcessCallLimitedAPI($this);
dispatch($process)->onQueue('limits');
}
public function getObjectsCount()
{
return count($this->getParams()['AdExtensions']);
}
public function slice($maxObjects): ?APIRequest
{
$splinter = $this->sliceByKey($maxObjects, 'AdExtensions');
$splinter->putParams([
'goalAdExtensions' => $this->goalAdExtensions->slice($maxObjects)->values(),
]);
$this->putParams([
'goalAdExtensions' => $this->goalAdExtensions->slice(0, $maxObjects),
]);
return $splinter;
}
public function handle($response)
{
if (!isset($response['result']['AddResults'])) {
return;
}
foreach ($response['result']['AddResults'] as $key => $add_result) {
$goalAdExtension = $this->goalAdExtensions->get($key);
if (!isset($add_result['Id'])) {
Log::debug("AddAdExtension, empty Id, token_id {$this->getToken()->getKey()}");
Log::debug($add_result);
Log::debug($this->getParams()['AdExtensions'][$key]);
GoalAdExtension::where('id', $goalAdExtension->getKey())
->update([
'reserve_create_at' => null,
]);
continue;
}
$external_id = (string)$add_result['Id'];
GoalAdExtension::where('id', $goalAdExtension->getKey())
->update([
'external_id' => $external_id,
'external_upload_at' => Carbon::now(),
'reserve_create_at' => null,
]);
}
}
public function failed()
{
GoalAdExtension::whereIn('id', $this->goalAdExtensions->pluck('id')->toArray())
->update([
'reserve_create_at' => null,
]);
}
public function putParams($params)
{
$this->goalAdExtensions = $params['goalAdExtensions'];
}
private function requestPrepare($params)
{
$this->setService('AdExtensions');
$this->setMethod('add');
$this->putParams($params);
$variables = Variable::all();
$lists = [];
$this->setParams([
'AdExtensions' => $this->goalAdExtensions->map(function (GoalAdExtension $goalAdExtension) use ($variables, &$lists) {
if (!isset($lists[$goalAdExtension->dictionary_campaign_id])) {
$list = Variable::getListVariablesByDictionaryCampaign($goalAdExtension->dictionary_campaign_id, $variables);
$lists[$goalAdExtension->dictionary_campaign_id] = $list;
} else {
$list = $lists[$goalAdExtension->dictionary_campaign_id];
}
return [
'Callout' => [
'CalloutText' => StrReplaceByVariables::getInstance($goalAdExtension->adExtension->callout_text, $list)->get(),
],
];
})->toArray(),
]);
}
}