Task.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
  5. *
  6. * @author Marcel Klehr <mklehr@gmx.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OC\TaskProcessing\Db;
  24. use OCP\AppFramework\Db\Entity;
  25. use OCP\TaskProcessing\Task as OCPTask;
  26. /**
  27. * @method setType(string $type)
  28. * @method string getType()
  29. * @method setLastUpdated(int $lastUpdated)
  30. * @method int getLastUpdated()
  31. * @method setStatus(int $status)
  32. * @method int getStatus()
  33. * @method setOutput(string $output)
  34. * @method string getOutput()
  35. * @method setInput(string $input)
  36. * @method string getInput()
  37. * @method setUserId(?string $userId)
  38. * @method string|null getUserId()
  39. * @method setAppId(string $type)
  40. * @method string getAppId()
  41. * @method setCustomId(string $customId)
  42. * @method string getCustomId()
  43. * @method setCompletionExpectedAt(null|\DateTime $completionExpectedAt)
  44. * @method null|\DateTime getCompletionExpectedAt()
  45. * @method setErrorMessage(null|string $error)
  46. * @method null|string getErrorMessage()
  47. * @method setProgress(null|float $progress)
  48. * @method null|float getProgress()
  49. */
  50. class Task extends Entity {
  51. protected $lastUpdated;
  52. protected $type;
  53. protected $input;
  54. protected $output;
  55. protected $status;
  56. protected $userId;
  57. protected $appId;
  58. protected $customId;
  59. protected $completionExpectedAt;
  60. protected $errorMessage;
  61. protected $progress;
  62. /**
  63. * @var string[]
  64. */
  65. public static array $columns = ['id', 'last_updated', 'type', 'input', 'output', 'status', 'user_id', 'app_id', 'custom_id', 'completion_expected_at', 'error_message', 'progress'];
  66. /**
  67. * @var string[]
  68. */
  69. public static array $fields = ['id', 'lastUpdated', 'type', 'input', 'output', 'status', 'userId', 'appId', 'customId', 'completionExpectedAt', 'errorMessage', 'progress'];
  70. public function __construct() {
  71. // add types in constructor
  72. $this->addType('id', 'integer');
  73. $this->addType('lastUpdated', 'integer');
  74. $this->addType('type', 'string');
  75. $this->addType('input', 'string');
  76. $this->addType('output', 'string');
  77. $this->addType('status', 'integer');
  78. $this->addType('userId', 'string');
  79. $this->addType('appId', 'string');
  80. $this->addType('customId', 'string');
  81. $this->addType('completionExpectedAt', 'datetime');
  82. $this->addType('errorMessage', 'string');
  83. $this->addType('progress', 'float');
  84. }
  85. public function toRow(): array {
  86. return array_combine(self::$columns, array_map(function ($field) {
  87. return $this->{'get'.ucfirst($field)}();
  88. }, self::$fields));
  89. }
  90. public static function fromPublicTask(OCPTask $task): self {
  91. /** @var Task $taskEntity */
  92. $taskEntity = self::fromParams([
  93. 'id' => $task->getId(),
  94. 'type' => $task->getTaskTypeId(),
  95. 'lastUpdated' => time(),
  96. 'status' => $task->getStatus(),
  97. 'input' => json_encode($task->getInput(), JSON_THROW_ON_ERROR),
  98. 'output' => json_encode($task->getOutput(), JSON_THROW_ON_ERROR),
  99. 'errorMessage' => $task->getErrorMessage(),
  100. 'userId' => $task->getUserId(),
  101. 'appId' => $task->getAppId(),
  102. 'customId' => $task->getCustomId(),
  103. 'completionExpectedAt' => $task->getCompletionExpectedAt(),
  104. 'progress' => $task->getProgress(),
  105. ]);
  106. return $taskEntity;
  107. }
  108. /**
  109. * @return OCPTask
  110. * @throws \JsonException
  111. */
  112. public function toPublicTask(): OCPTask {
  113. $task = new OCPTask($this->getType(), json_decode($this->getInput(), true, 512, JSON_THROW_ON_ERROR), $this->getAppId(), $this->getuserId(), $this->getCustomId());
  114. $task->setId($this->getId());
  115. $task->setStatus($this->getStatus());
  116. $task->setLastUpdated($this->getLastUpdated());
  117. $task->setOutput(json_decode($this->getOutput(), true, 512, JSON_THROW_ON_ERROR));
  118. $task->setCompletionExpectedAt($this->getCompletionExpectedAt());
  119. $task->setErrorMessage($this->getErrorMessage());
  120. $task->setProgress($this->getProgress());
  121. return $task;
  122. }
  123. }