Skip to content

Commit d4912fe

Browse files
authored
Merge pull request #427 from systopia/refactor-validate-form-actions
Refactor validate form actions to get rid of some legacy code
2 parents fee12d3 + 386affc commit d4912fe

25 files changed

+459
-975
lines changed

Civi/Api4/RemoteFundingApplicationProcess.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static function submitForm(): SubmitFormAction {
6969
}
7070

7171
public static function validateForm(): ValidateFormAction {
72-
return \Civi::service(ValidateFormAction::class);
72+
return new ValidateFormAction();
7373
}
7474

7575
}

Civi/Api4/RemoteFundingCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static function submitNewApplicationForm(): SubmitNewApplicationFormActio
7575
}
7676

7777
public static function validateNewApplicationForm(): ValidateNewApplicationFormAction {
78-
return \Civi::service(ValidateNewApplicationFormAction::class);
78+
return new ValidateNewApplicationFormAction();
7979
}
8080

8181
}

Civi/Funding/Api4/Action/FundingApplicationProcess/ValidateFormAction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function _run(Result $result): void {
6868
*/
6969
protected function createCommand(): ApplicationFormValidateCommand {
7070
$applicationProcessBundle = $this->applicationProcessBundleLoader->get($this->getId());
71-
Assert::notNull($applicationProcessBundle);
71+
Assert::notNull($applicationProcessBundle, sprintf('Application process with ID "%d" not found', $this->getId()));
7272
$statusList = $this->applicationProcessBundleLoader->getStatusList($applicationProcessBundle);
7373

7474
return new ApplicationFormValidateCommand($applicationProcessBundle, $statusList, $this->getData(), 20);

Civi/Funding/Api4/Action/Remote/ApplicationProcess/ValidateFormAction.php

+5-50
Original file line numberDiff line numberDiff line change
@@ -19,64 +19,19 @@
1919

2020
namespace Civi\Funding\Api4\Action\Remote\ApplicationProcess;
2121

22-
use Civi\Api4\Generic\Result;
23-
use Civi\Core\CiviEventDispatcherInterface;
22+
use Civi\Api4\RemoteFundingApplicationProcess;
23+
use Civi\Funding\Api4\Action\Remote\AbstractRemoteFundingAction;
2424
use Civi\Funding\Api4\Action\Traits\ApplicationProcessIdParameterTrait;
25-
use Civi\Funding\ApplicationProcess\ApplicationProcessBundleLoader;
26-
use Civi\Funding\Event\Remote\ApplicationProcess\ValidateApplicationFormEvent;
27-
use Civi\Funding\Event\Remote\FundingEvents;
28-
use Civi\Funding\Exception\FundingException;
2925
use Civi\RemoteTools\Api4\Action\Traits\DataParameterTrait;
3026

31-
/**
32-
* @method $this setData(array $data)
33-
*/
34-
class ValidateFormAction extends AbstractFormAction {
27+
class ValidateFormAction extends AbstractRemoteFundingAction {
3528

3629
use ApplicationProcessIdParameterTrait;
3730

3831
use DataParameterTrait;
3932

40-
public function __construct(
41-
ApplicationProcessBundleLoader $applicationProcessBundleLoader,
42-
CiviEventDispatcherInterface $eventDispatcher
43-
) {
44-
parent::__construct('validateForm', $applicationProcessBundleLoader, $eventDispatcher);
45-
$this->_eventDispatcher = $eventDispatcher;
46-
$this->_authorizeRequestEventName = FundingEvents::REQUEST_AUTHORIZE_EVENT_NAME;
47-
$this->_initRequestEventName = FundingEvents::REQUEST_INIT_EVENT_NAME;
48-
}
49-
50-
/**
51-
* @inheritDoc
52-
*
53-
* @throws \CRM_Core_Exception
54-
*/
55-
public function _run(Result $result): void {
56-
$event = $this->createEvent();
57-
$this->dispatchEvent($event);
58-
59-
$result->debug['event'] = $event->getDebugOutput();
60-
61-
if (NULL === $event->isValid()) {
62-
throw new FundingException('Form not validated');
63-
}
64-
65-
$result->rowCount = 1;
66-
$result->exchangeArray([
67-
'valid' => $event->isValid(),
68-
'errors' => $event->getErrors(),
69-
]);
70-
}
71-
72-
/**
73-
* @throws \CRM_Core_Exception
74-
*/
75-
private function createEvent(): ValidateApplicationFormEvent {
76-
return ValidateApplicationFormEvent::fromApiRequest(
77-
$this,
78-
$this->createEventParams($this->getApplicationProcessId())
79-
);
33+
public function __construct() {
34+
parent::__construct(RemoteFundingApplicationProcess::getEntityName(), 'validateForm');
8035
}
8136

8237
}

Civi/Funding/Api4/Action/Remote/FundingCase/ValidateNewApplicationFormAction.php

+5-57
Original file line numberDiff line numberDiff line change
@@ -19,74 +19,22 @@
1919

2020
namespace Civi\Funding\Api4\Action\Remote\FundingCase;
2121

22-
use Civi\Api4\Generic\Result;
23-
use Civi\Core\CiviEventDispatcherInterface;
22+
use Civi\Api4\RemoteFundingCase;
23+
use Civi\Funding\Api4\Action\Remote\AbstractRemoteFundingAction;
2424
use Civi\Funding\Api4\Action\Traits\FundingCaseTypeIdParameterTrait;
2525
use Civi\Funding\Api4\Action\Traits\FundingProgramIdParameterTrait;
26-
use Civi\Funding\Event\Remote\FundingCase\ValidateNewApplicationFormEvent;
27-
use Civi\Funding\Exception\FundingException;
28-
use Civi\Funding\FundingProgram\FundingCaseTypeManager;
29-
use Civi\Funding\FundingProgram\FundingCaseTypeProgramRelationChecker;
30-
use Civi\Funding\FundingProgram\FundingProgramManager;
3126
use Civi\RemoteTools\Api4\Action\Traits\DataParameterTrait;
3227

33-
/**
34-
* @method $this setData(array $data)
35-
*/
36-
class ValidateNewApplicationFormAction extends AbstractNewApplicationFormAction {
28+
class ValidateNewApplicationFormAction extends AbstractRemoteFundingAction {
3729

3830
use DataParameterTrait;
3931

4032
use FundingCaseTypeIdParameterTrait;
4133

4234
use FundingProgramIdParameterTrait;
4335

44-
public function __construct(
45-
FundingCaseTypeManager $fundingCaseTypeManager,
46-
FundingProgramManager $fundingProgramManager,
47-
CiviEventDispatcherInterface $eventDispatcher,
48-
FundingCaseTypeProgramRelationChecker $relationChecker
49-
) {
50-
parent::__construct(
51-
'validateNewApplicationForm',
52-
$fundingCaseTypeManager,
53-
$fundingProgramManager,
54-
$eventDispatcher,
55-
$relationChecker,
56-
);
57-
}
58-
59-
/**
60-
* @inheritDoc
61-
*
62-
* @throws \CRM_Core_Exception
63-
*/
64-
public function _run(Result $result): void {
65-
$this->assertFundingCaseTypeAndProgramRelated($this->getFundingCaseTypeId(), $this->getFundingProgramId());
66-
$event = $this->createEvent();
67-
$this->dispatchEvent($event);
68-
69-
$result->debug['event'] = $event->getDebugOutput();
70-
71-
if (NULL === $event->isValid()) {
72-
throw new FundingException('Form not validated');
73-
}
74-
75-
$result->rowCount = 1;
76-
$result->exchangeArray([
77-
'valid' => $event->isValid(),
78-
'errors' => $event->getErrors(),
79-
]);
80-
}
81-
82-
/**
83-
* @throws \CRM_Core_Exception
84-
*/
85-
private function createEvent(): ValidateNewApplicationFormEvent {
86-
return ValidateNewApplicationFormEvent::fromApiRequest(
87-
$this,
88-
$this->createEventParams($this->getFundingCaseTypeId(), $this->getFundingProgramId()),
89-
);
36+
public function __construct() {
37+
parent::__construct(RemoteFundingCase::getEntityName(), 'validateNewApplicationForm');
9038
}
9139

9240
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/*
3+
* Copyright (C) 2025 SYSTOPIA GmbH
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation in version 3.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
declare(strict_types = 1);
19+
20+
namespace Civi\Funding\ApplicationProcess\Api4\ActionHandler;
21+
22+
use Civi\Api4\FundingApplicationProcess;
23+
use Civi\Funding\Api4\Action\Remote\ApplicationProcess\ValidateFormAction;
24+
use Civi\RemoteTools\ActionHandler\ActionHandlerInterface;
25+
use Civi\RemoteTools\Api4\Api4Interface;
26+
27+
final class RemoteValidateActionHandler implements ActionHandlerInterface {
28+
29+
public const ENTITY_NAME = 'RemoteFundingApplicationProcess';
30+
31+
private Api4Interface $api4;
32+
33+
public function __construct(Api4Interface $api4) {
34+
$this->api4 = $api4;
35+
}
36+
37+
/**
38+
* @phpstan-return array{
39+
* valid: bool,
40+
* errors: array<string, non-empty-list<string>>|\stdClass,
41+
* }
42+
*
43+
* @throws \CRM_Core_Exception
44+
*/
45+
public function validateForm(ValidateFormAction $action): array {
46+
$result = $this->api4->execute(FundingApplicationProcess::getEntityName(), 'validateForm', [
47+
'data' => $action->getData(),
48+
'id' => $action->getApplicationProcessId(),
49+
]);
50+
51+
// @phpstan-ignore return.type
52+
return [
53+
'valid' => $result['valid'],
54+
'errors' => $result['errors'],
55+
];
56+
}
57+
58+
}

Civi/Funding/ApplicationProcess/Command/ApplicationFormNewValidateCommand.php

-8
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
final class ApplicationFormNewValidateCommand {
2626

27-
private int $contactId;
28-
2927
private FundingProgramEntity $fundingProgram;
3028

3129
private FundingCaseTypeEntity $fundingCaseType;
@@ -39,21 +37,15 @@ final class ApplicationFormNewValidateCommand {
3937
* @phpstan-param array<string, mixed> $data JSON serializable.
4038
*/
4139
public function __construct(
42-
int $contactId,
4340
FundingProgramEntity $fundingProgram,
4441
FundingCaseTypeEntity $fundingCaseType,
4542
array $data
4643
) {
47-
$this->contactId = $contactId;
4844
$this->fundingProgram = $fundingProgram;
4945
$this->fundingCaseType = $fundingCaseType;
5046
$this->data = $data;
5147
}
5248

53-
public function getContactId(): int {
54-
return $this->contactId;
55-
}
56-
5749
public function getFundingProgram(): FundingProgramEntity {
5850
return $this->fundingProgram;
5951
}

Civi/Funding/ApplicationProcess/Handler/ApplicationFormNewSubmitHandler.php

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public function __construct(
5656
*/
5757
public function handle(ApplicationFormNewSubmitCommand $command): ApplicationFormNewSubmitResult {
5858
$validationResult = $this->validateHandler->handle(new ApplicationFormNewValidateCommand(
59-
$command->getContactId(),
6059
$command->getFundingProgram(),
6160
$command->getFundingCaseType(),
6261
$command->getData(),

Civi/Funding/Event/Remote/AbstractFundingValidateFormEvent.php

-81
This file was deleted.

Civi/Funding/Event/Remote/ApplicationProcess/ValidateApplicationFormEvent.php

-35
This file was deleted.

0 commit comments

Comments
 (0)