Task.php 3.7 KB

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