Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

64 lignes
1.7KB

  1. <?php
  2. namespace App\Kintone;
  3. use App\Kintone\Models\KintoneModel;
  4. use Exception;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Log;
  8. class KintoneWebHookReceiver
  9. {
  10. /**
  11. * key:appId
  12. * value:KintoneModelのクラス名
  13. * @var Collection<string, string>
  14. */
  15. private Collection $config;
  16. public ?KintoneModel $model = null;
  17. public ?KintoneWebHookEventType $type = null;
  18. public function __construct()
  19. {
  20. $this->config = collect();
  21. foreach (config("kintone.applications", []) as $className => $config) {
  22. $appId = $config['appId'];
  23. $this->config->put($appId, $className);
  24. }
  25. }
  26. public function readWebHookRequest(Request $request)
  27. {
  28. $data = $request->all();
  29. $appId = data_get($data, 'app.id');
  30. $this->type = KintoneWebHookEventType::from(data_get($data, "type", ""));
  31. // 削除とコメント追記は扱わない
  32. if ($this->type === KintoneWebHookEventType::DELETE_RECORD || $this->type === KintoneWebHookEventType::ADD_RECORD_COMMENT) {
  33. return;
  34. }
  35. $model = $this->getModel($appId);
  36. $record = data_get($data, 'record');
  37. if (!$model->setDataFromRecordResponse($record)) {
  38. Log::error($data);
  39. throw new Exception("WebHook読込失敗");
  40. }
  41. $this->model = $model;
  42. }
  43. private function getModel(string $appId): KintoneModel
  44. {
  45. $className = $this->config->get($appId);
  46. if ($className === null) {
  47. throw new Exception("モデル不正");
  48. }
  49. return new $className();
  50. }
  51. }