AWorkflowController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author blizzz <blizzz@arthur-schiwon.de>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\WorkflowEngine\Controller;
  27. use Doctrine\DBAL\Exception;
  28. use OCA\WorkflowEngine\Helper\ScopeContext;
  29. use OCA\WorkflowEngine\Manager;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCS\OCSBadRequestException;
  32. use OCP\AppFramework\OCS\OCSException;
  33. use OCP\AppFramework\OCS\OCSForbiddenException;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\IRequest;
  36. use Psr\Log\LoggerInterface;
  37. abstract class AWorkflowController extends OCSController {
  38. /** @var Manager */
  39. protected $manager;
  40. /** @var LoggerInterface */
  41. private $logger;
  42. public function __construct(
  43. $appName,
  44. IRequest $request,
  45. Manager $manager,
  46. LoggerInterface $logger
  47. ) {
  48. parent::__construct($appName, $request);
  49. $this->manager = $manager;
  50. $this->logger = $logger;
  51. }
  52. /**
  53. * @throws OCSForbiddenException
  54. */
  55. abstract protected function getScopeContext(): ScopeContext;
  56. /**
  57. * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global?format=json"
  58. *
  59. * @throws OCSForbiddenException
  60. */
  61. public function index(): DataResponse {
  62. $operationsByClass = $this->manager->getAllOperations($this->getScopeContext());
  63. foreach ($operationsByClass as &$operations) {
  64. foreach ($operations as &$operation) {
  65. $operation = $this->manager->formatOperation($operation);
  66. }
  67. }
  68. return new DataResponse($operationsByClass);
  69. }
  70. /**
  71. * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global/OCA\\Workflow_DocToPdf\\Operation?format=json"
  72. *
  73. * @throws OCSForbiddenException
  74. */
  75. public function show(string $id): DataResponse {
  76. $context = $this->getScopeContext();
  77. // The ID corresponds to a class name
  78. $operations = $this->manager->getOperations($id, $context);
  79. foreach ($operations as &$operation) {
  80. $operation = $this->manager->formatOperation($operation);
  81. }
  82. return new DataResponse($operations);
  83. }
  84. /**
  85. * @throws OCSBadRequestException
  86. * @throws OCSForbiddenException
  87. * @throws OCSException
  88. */
  89. public function create(
  90. string $class,
  91. string $name,
  92. array $checks,
  93. string $operation,
  94. string $entity,
  95. array $events
  96. ): DataResponse {
  97. $context = $this->getScopeContext();
  98. try {
  99. $operation = $this->manager->addOperation($class, $name, $checks, $operation, $context, $entity, $events);
  100. $operation = $this->manager->formatOperation($operation);
  101. return new DataResponse($operation);
  102. } catch (\UnexpectedValueException $e) {
  103. throw new OCSBadRequestException($e->getMessage(), $e);
  104. } catch (\DomainException $e) {
  105. throw new OCSForbiddenException($e->getMessage(), $e);
  106. } catch (Exception $e) {
  107. $this->logger->error('Error when inserting flow', ['exception' => $e]);
  108. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  109. }
  110. }
  111. /**
  112. * @throws OCSBadRequestException
  113. * @throws OCSForbiddenException
  114. * @throws OCSException
  115. */
  116. public function update(
  117. int $id,
  118. string $name,
  119. array $checks,
  120. string $operation,
  121. string $entity,
  122. array $events
  123. ): DataResponse {
  124. try {
  125. $context = $this->getScopeContext();
  126. $operation = $this->manager->updateOperation($id, $name, $checks, $operation, $context, $entity, $events);
  127. $operation = $this->manager->formatOperation($operation);
  128. return new DataResponse($operation);
  129. } catch (\UnexpectedValueException $e) {
  130. throw new OCSBadRequestException($e->getMessage(), $e);
  131. } catch (\DomainException $e) {
  132. throw new OCSForbiddenException($e->getMessage(), $e);
  133. } catch (Exception $e) {
  134. $this->logger->error('Error when updating flow with id ' . $id, ['exception' => $e]);
  135. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  136. }
  137. }
  138. /**
  139. * @throws OCSBadRequestException
  140. * @throws OCSForbiddenException
  141. * @throws OCSException
  142. */
  143. public function destroy(int $id): DataResponse {
  144. try {
  145. $deleted = $this->manager->deleteOperation($id, $this->getScopeContext());
  146. return new DataResponse($deleted);
  147. } catch (\UnexpectedValueException $e) {
  148. throw new OCSBadRequestException($e->getMessage(), $e);
  149. } catch (\DomainException $e) {
  150. throw new OCSForbiddenException($e->getMessage(), $e);
  151. } catch (Exception $e) {
  152. $this->logger->error('Error when deleting flow with id ' . $id, ['exception' => $e]);
  153. throw new OCSException('An internal error occurred', $e->getCode(), $e);
  154. }
  155. }
  156. }