-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEngine.php
More file actions
186 lines (164 loc) · 4.72 KB
/
Engine.php
File metadata and controls
186 lines (164 loc) · 4.72 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace Acms\Plugins\GoogleSheets;
use Field;
use Google\Service\Sheets;
use Google\Service\Sheets\Request;
use Google\Service\Sheets\RowData;
use Google\Service\Sheets\AppendCellsRequest;
use Google\Service\Sheets\BatchUpdateSpreadsheetRequest;
use Google\Service\Sheets\CellData;
use Google\Service\Sheets\ExtendedValue;
class Engine
{
/**
* @var \ACMS_POST_Form
*/
protected $module;
/**
* @var string
*/
protected $code;
/**
* @var \Field
*/
protected $config;
/**
* @var string
*/
protected $glue;
/**
* Engine constructor.
* @param string $code
* @param \ACMS_POST_Form $module
*/
public function __construct(string $code, $module)
{
$info = $module->loadForm($code);
if ($info === false) {
throw new \RuntimeException('Not Found Form.');
}
$this->module = $module;
$this->code = $code;
$this->config = $info['data']->getChild('mail');
$this->glue = $this->config->get('cell_glue', ',');
}
/**
* Update Google Sheets
* @return void
*/
public function send(): void
{
$field = $this->module->Post->getChild('field');
$checkItems = [
'formId' => $this->config->get('spreadsheet_submit_formid'),
'time' => $this->config->get('spreadsheet_submit_date'),
'url' => $this->config->get('spreadsheet_submit_url'),
'ipAddr' => $this->config->get('spreadsheet_submit_ip'),
'ua' => $this->config->get('spreadsheet_submit_agent'),
];
$values = [];
foreach ($checkItems as $item => $check) {
if ($check !== 'true') {
continue;
}
$method = 'get' . ucwords($item);
if (is_callable([$this, $method])) {
$values[] = $this->$method();
}
}
foreach ($field->_aryField as $key => $val) {
$values[] = $this->getCellData($field->getArray($key), $this->glue);
}
if ($this->config->get('spreadsheet_id')) {
$this->update($values);
}
}
/**
* Send Google Sheets Api
*
* @param array $values
* @return void
*/
protected function update(array $values): void
{
$client = (new Api())->getClient();
if (!$client->getAccessToken()) {
throw new \RuntimeException('Failed to get the access token.');
}
$service = new Sheets($client);
$spreadsheetId = $this->config->get('spreadsheet_id');
$spreadsheetGid = $this->config->get('sheet_id', 0);
// Build the RowData
$rowData = new RowData();
$rowData->setValues($values);
// Prepare the request
$append_request = new AppendCellsRequest();
$append_request->setSheetId($spreadsheetGid);
$append_request->setRows($rowData);
$append_request->setFields('userEnteredValue');
// Set the request
$request = new Request();
$request->setAppendCells($append_request);
// Add the request to the requests array
$requests = [];
$requests[] = $request;
// Prepare the update
$batchUpdateRequest = new BatchUpdateSpreadsheetRequest([
'requests' => $requests
]);
// Execute the request
$service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);
// 例外が発生しなければ成功とみなす(Google APIクライアントが例外を投げる)
}
/**
* @param array|string $value
* @param string $glue
* @return CellData
*/
private function getCellData($value, string $glue = ','): CellData
{
if (is_array($value)) {
$value = implode($glue, $value);
}
$cellData = new CellData();
$extendedValue = new ExtendedValue();
$extendedValue->setStringValue($value);
$cellData->setUserEnteredValue($extendedValue);
return $cellData;
}
/**
* @return CellData
*/
private function getTime(): CellData
{
return $this->getCellData(date('Y-m-d H:i:s', REQUEST_TIME));
}
/**
* @return CellData
*/
private function getFormId(): CellData
{
return $this->getCellData($this->code);
}
/**
* @return CellData
*/
private function getUrl(): CellData
{
return $this->getCellData(REQUEST_URL);
}
/**
* @return CellData
*/
private function getIpAddr(): CellData
{
return $this->getCellData(REMOTE_ADDR);
}
/**
* @return CellData
*/
private function getUa(): CellData
{
return $this->getCellData(UA);
}
}