Task.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. */
  44. class Task extends Entity {
  45. protected $lastUpdated;
  46. protected $type;
  47. protected $input;
  48. protected $output;
  49. protected $status;
  50. protected $userId;
  51. protected $appId;
  52. protected $identifier;
  53. /**
  54. * @var string[]
  55. */
  56. public static array $columns = ['id', 'last_updated', 'type', 'input', 'output', 'status', 'user_id', 'app_id', 'identifier'];
  57. /**
  58. * @var string[]
  59. */
  60. public static array $fields = ['id', 'lastUpdated', 'type', 'input', 'output', 'status', 'userId', 'appId', 'identifier'];
  61. public function __construct() {
  62. // add types in constructor
  63. $this->addType('id', 'integer');
  64. $this->addType('lastUpdated', 'integer');
  65. $this->addType('type', 'string');
  66. $this->addType('input', 'string');
  67. $this->addType('output', 'string');
  68. $this->addType('status', 'integer');
  69. $this->addType('userId', 'string');
  70. $this->addType('appId', 'string');
  71. $this->addType('identifier', 'string');
  72. }
  73. public function toRow(): array {
  74. return array_combine(self::$columns, array_map(function ($field) {
  75. return $this->{'get'.ucfirst($field)}();
  76. }, self::$fields));
  77. }
  78. public static function fromPublicTask(OCPTask $task): Task {
  79. /** @var Task $task */
  80. $task = Task::fromParams([
  81. 'id' => $task->getId(),
  82. 'type' => $task->getType(),
  83. 'lastUpdated' => time(),
  84. 'status' => $task->getStatus(),
  85. 'input' => $task->getInput(),
  86. 'output' => $task->getOutput(),
  87. 'userId' => $task->getUserId(),
  88. 'appId' => $task->getAppId(),
  89. 'identifier' => $task->getIdentifier(),
  90. ]);
  91. return $task;
  92. }
  93. public function toPublicTask(): OCPTask {
  94. $task = new OCPTask($this->getType(), $this->getInput(), $this->getAppId(), $this->getuserId(), $this->getIdentifier());
  95. $task->setId($this->getId());
  96. $task->setStatus($this->getStatus());
  97. $task->setOutput($this->getOutput());
  98. return $task;
  99. }
  100. }