1
0

Task.php 3.0 KB

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