1
0

Task.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\TextProcessing\Db;
  24. use OCP\AppFramework\Db\Entity;
  25. use OCP\TextProcessing\Task as OCPTask;
  26. /**
  27. * @method setType(string $type)
  28. * @method string getType()
  29. * @method setLastUpdated(int $lastUpdated)
  30. * @method int getLastUpdated()
  31. * @method setInput(string $type)
  32. * @method string getInput()
  33. * @method setOutput(string $type)
  34. * @method string getOutput()
  35. * @method setStatus(int $type)
  36. * @method int getStatus()
  37. * @method setUserId(?string $userId)
  38. * @method string|null getUserId()
  39. * @method setAppId(string $type)
  40. * @method string getAppId()
  41. * @method setIdentifier(string $identifier)
  42. * @method string getIdentifier()
  43. * @method setCompletionExpectedAt(null|\DateTime $completionExpectedAt)
  44. * @method null|\DateTime getCompletionExpectedAt()
  45. */
  46. class Task extends Entity {
  47. protected $lastUpdated;
  48. protected $type;
  49. protected $input;
  50. protected $output;
  51. protected $status;
  52. protected $userId;
  53. protected $appId;
  54. protected $identifier;
  55. protected $completionExpectedAt;
  56. /**
  57. * @var string[]
  58. */
  59. public static array $columns = ['id', 'last_updated', 'type', 'input', 'output', 'status', 'user_id', 'app_id', 'identifier', 'completion_expected_at'];
  60. /**
  61. * @var string[]
  62. */
  63. public static array $fields = ['id', 'lastUpdated', 'type', 'input', 'output', 'status', 'userId', 'appId', 'identifier', 'completionExpectedAt'];
  64. public function __construct() {
  65. // add types in constructor
  66. $this->addType('id', 'integer');
  67. $this->addType('lastUpdated', 'integer');
  68. $this->addType('type', 'string');
  69. $this->addType('input', 'string');
  70. $this->addType('output', 'string');
  71. $this->addType('status', 'integer');
  72. $this->addType('userId', 'string');
  73. $this->addType('appId', 'string');
  74. $this->addType('identifier', 'string');
  75. $this->addType('completionExpectedAt', 'datetime');
  76. }
  77. public function toRow(): array {
  78. return array_combine(self::$columns, array_map(function ($field) {
  79. return $this->{'get'.ucfirst($field)}();
  80. }, self::$fields));
  81. }
  82. public static function fromPublicTask(OCPTask $task): Task {
  83. /** @var Task $task */
  84. $task = Task::fromParams([
  85. 'id' => $task->getId(),
  86. 'type' => $task->getType(),
  87. 'lastUpdated' => time(),
  88. 'status' => $task->getStatus(),
  89. 'input' => $task->getInput(),
  90. 'output' => $task->getOutput(),
  91. 'userId' => $task->getUserId(),
  92. 'appId' => $task->getAppId(),
  93. 'identifier' => $task->getIdentifier(),
  94. 'completionExpectedAt' => $task->getCompletionExpectedAt(),
  95. ]);
  96. return $task;
  97. }
  98. public function toPublicTask(): OCPTask {
  99. $task = new OCPTask($this->getType(), $this->getInput(), $this->getAppId(), $this->getuserId(), $this->getIdentifier());
  100. $task->setId($this->getId());
  101. $task->setStatus($this->getStatus());
  102. $task->setOutput($this->getOutput());
  103. $task->setCompletionExpectedAt($this->getCompletionExpectedAt());
  104. return $task;
  105. }
  106. }