1
0

Task.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\TextProcessing\Db;
  8. use OCP\AppFramework\Db\Entity;
  9. use OCP\TextProcessing\Task as OCPTask;
  10. /**
  11. * @method setType(string $type)
  12. * @method string getType()
  13. * @method setLastUpdated(int $lastUpdated)
  14. * @method int getLastUpdated()
  15. * @method setInput(string $type)
  16. * @method string getInput()
  17. * @method setOutput(string $type)
  18. * @method string getOutput()
  19. * @method setStatus(int $type)
  20. * @method int getStatus()
  21. * @method setUserId(?string $userId)
  22. * @method string|null getUserId()
  23. * @method setAppId(string $type)
  24. * @method string getAppId()
  25. * @method setIdentifier(string $identifier)
  26. * @method string getIdentifier()
  27. * @method setCompletionExpectedAt(null|\DateTime $completionExpectedAt)
  28. * @method null|\DateTime getCompletionExpectedAt()
  29. */
  30. class Task extends Entity {
  31. protected $lastUpdated;
  32. protected $type;
  33. protected $input;
  34. protected $output;
  35. protected $status;
  36. protected $userId;
  37. protected $appId;
  38. protected $identifier;
  39. protected $completionExpectedAt;
  40. /**
  41. * @var string[]
  42. */
  43. public static array $columns = ['id', 'last_updated', 'type', 'input', 'output', 'status', 'user_id', 'app_id', 'identifier', 'completion_expected_at'];
  44. /**
  45. * @var string[]
  46. */
  47. public static array $fields = ['id', 'lastUpdated', 'type', 'input', 'output', 'status', 'userId', 'appId', 'identifier', 'completionExpectedAt'];
  48. public function __construct() {
  49. // add types in constructor
  50. $this->addType('id', 'integer');
  51. $this->addType('lastUpdated', 'integer');
  52. $this->addType('type', 'string');
  53. $this->addType('input', 'string');
  54. $this->addType('output', 'string');
  55. $this->addType('status', 'integer');
  56. $this->addType('userId', 'string');
  57. $this->addType('appId', 'string');
  58. $this->addType('identifier', 'string');
  59. $this->addType('completionExpectedAt', 'datetime');
  60. }
  61. public function toRow(): array {
  62. return array_combine(self::$columns, array_map(function ($field) {
  63. return $this->{'get'.ucfirst($field)}();
  64. }, self::$fields));
  65. }
  66. public static function fromPublicTask(OCPTask $task): Task {
  67. /** @var Task $task */
  68. $task = Task::fromParams([
  69. 'id' => $task->getId(),
  70. 'type' => $task->getType(),
  71. 'lastUpdated' => time(),
  72. 'status' => $task->getStatus(),
  73. 'input' => $task->getInput(),
  74. 'output' => $task->getOutput(),
  75. 'userId' => $task->getUserId(),
  76. 'appId' => $task->getAppId(),
  77. 'identifier' => $task->getIdentifier(),
  78. 'completionExpectedAt' => $task->getCompletionExpectedAt(),
  79. ]);
  80. return $task;
  81. }
  82. public function toPublicTask(): OCPTask {
  83. $task = new OCPTask($this->getType(), $this->getInput(), $this->getAppId(), $this->getuserId(), $this->getIdentifier());
  84. $task->setId($this->getId());
  85. $task->setStatus($this->getStatus());
  86. $task->setOutput($this->getOutput());
  87. $task->setCompletionExpectedAt($this->getCompletionExpectedAt());
  88. return $task;
  89. }
  90. }