|
- <?php
-
- namespace App\Kintone;
-
- use App\Exceptions\ConfigException;
- use App\Kintone\Models\KintoneModel;
- use Exception;
- use Illuminate\Http\Client\Response;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Http;
- use Log;
-
- class KintoneAccess
- {
-
- private string $appName;
- private string $host;
- private string $apiToken;
- private int $appId;
-
- private string|null $cursor = null;
- private int $cursorDataCount = 0;
- private bool $hasNext = false;
-
- private const URL_RECORD = "/k/v1/record.json";
- private const URL_CURSOL = "/k/v1/records/cursor.json";
-
-
- public function __construct(string $appName)
- {
- $this->appName = $appName;
-
- $key = "kintone.host";
- $host = config($key);
- if (!$host) {
- throw new ConfigException("kintone.host", $host);
- }
-
- $key = "kintone.applications." . $appName . ".apiToken";
- $apiToken = config($key);
- if (!$apiToken) {
- throw new ConfigException($key, $apiToken);
- }
-
- $key = "kintone.applications." . $appName . ".appId";
- $appId = config($key);
- if (!$appId) {
- throw new ConfigException($key, $appId);
- }
-
- $this->host = $host;
- $this->apiToken = $apiToken;
- $this->appId = $appId;
- }
-
- public function __destruct()
- {
- $this->deleteCursor();
- }
-
-
- public function find(int $id, KintoneModel &$result): Response
- {
-
- $response = Http::withHeaders([
- "X-Cybozu-API-Token" => $this->apiToken,
- ])->get($this->getRecordUrl(), [
- "app" => $this->appId,
- "id" => $id,
- ]);
-
- if ($response->failed()) {
- $e = $response->toException();
- if ($e instanceof Exception) {
- throw $e;
- }
- }
-
- $result->setDataFromRecordResponse($response['record']);
- return $response;
- }
-
- public function update(KintoneModel &$model)
- {
- $response = Http::withHeaders([
- "X-Cybozu-API-Token" => $this->apiToken,
- ])->put($this->getRecordUrl(), [
- "app" => $this->appId,
- "id" => $model->getRecordId(),
- "record" => $model->getApiLayout(),
- "revision" => $model->getRevision(),
- ]);
-
- if ($response->failed()) {
- $e = $response->toException();
- if ($e instanceof Exception) {
- throw $e;
- }
- }
-
- $model->clean($response["revision"]);
- return $response;
- }
-
- public function create(KintoneModel &$model)
- {
- $response = Http::withHeaders([
- "X-Cybozu-API-Token" => $this->apiToken,
- ])->post($this->getRecordUrl(), [
- "app" => $this->appId,
- "record" => $model->getApiLayout(),
- ]);
-
- if ($response->failed()) {
- $e = $response->toException();
- if ($e instanceof Exception) {
- Log::error($e->getMessage());
- throw $e;
- }
- }
-
- $model->clean($response["revision"]);
- $model->setRecordId($response["id"]);
- return $response;
- }
-
- public function createCursor(KintoneRecordQuery|null $query = null, array|null $fields = null)
- {
- $data = [
- "app" => $this->appId,
- ];
- if ($query !== null) {
- $data["query"] = $query->toQuery();
- }
- if ($fields !== null) {
- $data["fields"] = $fields;
- }
-
- $response = Http::withHeaders([
- "X-Cybozu-API-Token" => $this->apiToken,
- ])->post($this->getCursorUrl(), $data);
-
- if ($response->failed()) {
- $e = $response->toException();
- if ($e instanceof Exception) {
- Log::error($e->getMessage());
- throw $e;
- }
- }
-
- $this->cursor = $response["id"];
- $this->cursorDataCount = $response["totalCount"];
-
- if (0 < $this->cursorDataCount) {
- $this->hasNext = true;
- }
-
- return $response;
- }
-
-
- /**
- * @return Collection<KintoneModel>
- */
- public function next()
- {
- if (!$this->hasNext) {
- return collect();
- }
-
- $response = Http::withHeaders([
- "X-Cybozu-API-Token" => $this->apiToken,
- ])->get($this->getCursorUrl(), [
- "id" => $this->cursor,
- ]);
-
- if ($response->failed()) {
- $e = $response->toException();
- if ($e instanceof Exception) {
- Log::error($e->getMessage());
- throw $e;
- }
- }
-
- $ret = collect();
-
- $hasNext = $response["next"];
- if (!$hasNext) {
- $this->cursor = null;
- $this->hasNext = false;
- $this->cursorDataCount = 0;
- }
-
- foreach ($response["records"] as $data) {
- /**
- * @var KintoneModel $model
- */
- $model = new $this->appName();
-
- $model->setDataFromRecordResponse($data);
- $ret->push($model);
- }
-
-
-
- return $ret;
- }
-
- /**
- * @return Collection<KintoneModel>
- */
- public function all(KintoneRecordQuery|null $query = null, array|null $fields = null)
- {
-
- if ($this->cursor !== null) {
- $this->deleteCursor();
- }
- $this->createCursor($query, $fields);
-
- $list = collect();
- while (true) {
- $ret = $this->next();
-
- foreach ($ret as $ele) {
- $list->push($ele);
- }
-
- if ($this->cursor === null) {
- break;
- }
- }
- return $list;
- }
-
- public function deleteCursor()
- {
- if ($this->cursor === null) {
- return;
- }
-
- $response = Http::withHeaders([
- "X-Cybozu-API-Token" => $this->apiToken,
- ])->delete($this->getCursorUrl(), [
- "id" => $this->cursor,
- ]);
-
- if ($response->failed()) {
- $e = $response->toException();
- if ($e instanceof Exception) {
- Log::error($e->getMessage());
- throw $e;
- }
- }
-
- $this->cursor = null;
- $this->hasNext = false;
- $this->cursorDataCount = 0;
- }
-
- private function getRecordUrl()
- {
- return $this->getUrl(self::URL_RECORD);
- }
-
- private function getCursorUrl()
- {
- return $this->getUrl(self::URL_CURSOL);
- }
-
- private function getUrl(string $path)
- {
- return $this->host . $path;
- }
- }
|