|
- <?php
-
- namespace App\Kintone;
-
- use App\Kintone\Models\KintoneModel;
- use Exception;
- use Illuminate\Http\Request;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Log;
-
- class KintoneWebHookReceiver
- {
- /**
- * key:appId
- * value:KintoneModelのクラス名
- * @var Collection<string, string>
- */
- private Collection $config;
-
- public ?KintoneModel $model = null;
-
- public ?KintoneWebHookEventType $type = null;
-
- public function __construct()
- {
- $this->config = collect();
- foreach (config("kintone.applications", []) as $className => $config) {
- $appId = $config['appId'];
- $this->config->put($appId, $className);
- }
- }
-
- public function readWebHookRequest(Request $request)
- {
- $data = $request->all();
- $appId = data_get($data, 'app.id');
- $this->type = KintoneWebHookEventType::from(data_get($data, "type", ""));
-
- // 削除とコメント追記は扱わない
- if ($this->type === KintoneWebHookEventType::DELETE_RECORD || $this->type === KintoneWebHookEventType::ADD_RECORD_COMMENT) {
- return;
- }
-
- $model = $this->getModel($appId);
- $record = data_get($data, 'record');
-
- if (!$model->setDataFromRecordResponse($record)) {
- Log::error($data);
- throw new Exception("WebHook読込失敗");
- }
-
- $this->model = $model;
- }
-
- private function getModel(string $appId): KintoneModel
- {
- $className = $this->config->get($appId);
- if ($className === null) {
- throw new Exception("モデル不正");
- }
- return new $className();
- }
- }
|