| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\GoogleSheets; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 6 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 7 |
use IvyForms\Controllers\Controller; |
| 8 |
use IvyForms\Services\GoogleSheets\GoogleSheetsService; |
| 9 |
use WP_REST_Request; |
| 10 |
use WP_REST_Response; |
| 11 |
|
| 12 |
class GoogleSheetsPickerConfigController extends Controller |
| 13 |
{ |
| 14 |
private GoogleSheetsService $googleSheetsService; |
| 15 |
|
| 16 |
public function __construct(GoogleSheetsService $googleSheetsService) |
| 17 |
{ |
| 18 |
$this->googleSheetsService = $googleSheetsService; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @throws ForbiddenException |
| 23 |
*/ |
| 24 |
protected function handle(WP_REST_Request $data): WP_REST_Response |
| 25 |
{ |
| 26 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 27 |
|
| 28 |
$config = $this->googleSheetsService->getPickerConfig(); |
| 29 |
|
| 30 |
if (empty($config['success'])) { |
| 31 |
return new WP_REST_Response([ |
| 32 |
'code' => $config['code'] ?? GoogleSheetsService::PICKER_ERROR_CODE, |
| 33 |
'message' => $config['message'] ?? '', |
| 34 |
], 200); |
| 35 |
} |
| 36 |
|
| 37 |
return new WP_REST_Response([ |
| 38 |
'developerKey' => $config['developerKey'], |
| 39 |
'appId' => $config['appId'], |
| 40 |
'accessToken' => $config['accessToken'], |
| 41 |
], 200); |
| 42 |
} |
| 43 |
} |
| 44 |
|