-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Execute delete and submit actions in database transaction
- Loading branch information
Dominic Tubach
committed
Jul 30, 2024
1 parent
fef1cef
commit 025dd8f
Showing
9 changed files
with
428 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
Civi/RemoteTools/ActionHandler/RemoteActionsHandlerTransactionDecorator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Civi\RemoteTools\ActionHandler; | ||
|
||
use Civi\RemoteTools\Api4\Action\RemoteDeleteAction; | ||
use Civi\RemoteTools\Api4\Action\RemoteSubmitCreateFormAction; | ||
use Civi\RemoteTools\Api4\Action\RemoteSubmitUpdateFormAction; | ||
use Civi\RemoteTools\Database\TransactionFactory; | ||
|
||
final class RemoteActionsHandlerTransactionDecorator extends AbstractRemoteEntityActionsHandlerDecorator { | ||
|
||
private TransactionFactory $transactionFactory; | ||
|
||
public function __construct(RemoteEntityActionsHandlerInterface $handler, TransactionFactory $transactionFactory) { | ||
parent::__construct($handler); | ||
$this->transactionFactory = $transactionFactory; | ||
} | ||
|
||
public function delete(RemoteDeleteAction $action): array { | ||
$transaction = $this->transactionFactory->createTransaction(); | ||
try { | ||
return parent::delete($action); | ||
} | ||
// @phpstan-ignore-next-line Dead catch clause. | ||
catch (\Throwable $e) { | ||
$transaction->rollback(); | ||
|
||
throw $e; | ||
} | ||
finally { | ||
$transaction->commit(); | ||
} | ||
} | ||
|
||
public function submitCreateForm(RemoteSubmitCreateFormAction $action): array { | ||
$transaction = $this->transactionFactory->createTransaction(); | ||
try { | ||
return parent::submitCreateForm($action); | ||
} | ||
// @phpstan-ignore-next-line Dead catch clause. | ||
catch (\Throwable $e) { | ||
$transaction->rollback(); | ||
|
||
throw $e; | ||
} | ||
finally { | ||
$transaction->commit(); | ||
} | ||
} | ||
|
||
public function submitUpdateForm(RemoteSubmitUpdateFormAction $action): array { | ||
$transaction = $this->transactionFactory->createTransaction(); | ||
try { | ||
return parent::submitUpdateForm($action); | ||
} | ||
// @phpstan-ignore-next-line Dead catch clause. | ||
catch (\Throwable $e) { | ||
$transaction->rollback(); | ||
|
||
throw $e; | ||
} | ||
finally { | ||
$transaction->commit(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/* | ||
* Copyright (C) 2022 SYSTOPIA GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation in version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Civi\RemoteTools\Database; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
class TransactionFactory { | ||
|
||
public function createTransaction(): \CRM_Core_Transaction { | ||
return \CRM_Core_Transaction::create(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Civi/RemoteTools/DependencyInjection/Compiler/Traits/DecorateServiceTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Civi\RemoteTools\DependencyInjection\Compiler\Traits; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
trait DecorateServiceTrait { | ||
|
||
/** | ||
* @phpstan-param class-string $decoratorClass | ||
* @param int|string $argumentKey | ||
*/ | ||
protected function decorateService( | ||
ContainerBuilder $container, | ||
string $id, | ||
string $decoratorClass, | ||
string $serviceIdPostfix, | ||
$argumentKey = 0 | ||
): void { | ||
$decoratorId = $decoratorClass . ':' . $serviceIdPostfix; | ||
$container->autowire($decoratorId, $decoratorClass) | ||
->setDecoratedService($id) | ||
->setArgument($argumentKey, new Reference($decoratorId . '.inner')); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.