Skip to content

Commit e78042c

Browse files
author
Dominic Tubach
committed
Refactor get form actions to get rid of some legacy code
1 parent f849f09 commit e78042c

32 files changed

+506
-830
lines changed

Civi/Api4/RemoteFundingApplicationProcess.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function submitAddForm(): SubmitAddFormAction {
6161
}
6262

6363
public static function getForm(): GetFormAction {
64-
return \Civi::service(GetFormAction::class);
64+
return new GetFormAction();
6565
}
6666

6767
public static function submitForm(): SubmitFormAction {

Civi/Api4/RemoteFundingCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function validateNewForm(): ValidateNewFormAction {
6767
}
6868

6969
public static function getNewApplicationForm(): GetNewApplicationFormAction {
70-
return \Civi::service(GetNewApplicationFormAction::class);
70+
return new GetNewApplicationFormAction();
7171
}
7272

7373
public static function submitNewApplicationForm(): SubmitNewApplicationFormAction {

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

+5-44
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,16 @@
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\GetApplicationFormEvent;
27-
use Civi\Funding\Exception\FundingException;
28-
use CRM_Funding_ExtensionUtil as E;
2925

30-
class GetFormAction extends AbstractFormAction {
26+
class GetFormAction extends AbstractRemoteFundingAction {
3127

3228
use ApplicationProcessIdParameterTrait;
3329

34-
public function __construct(
35-
ApplicationProcessBundleLoader $applicationProcessBundleLoader,
36-
CiviEventDispatcherInterface $eventDispatcher
37-
) {
38-
parent::__construct('getForm', $applicationProcessBundleLoader, $eventDispatcher);
39-
}
40-
41-
/**
42-
* @inheritDoc
43-
*
44-
* @throws \CRM_Core_Exception
45-
*/
46-
public function _run(Result $result): void {
47-
$event = $this->createEvent();
48-
$this->dispatchEvent($event);
49-
50-
$result->debug['event'] = $event->getDebugOutput();
51-
if (NULL === $event->getJsonSchema() || NULL === $event->getUiSchema()) {
52-
throw new FundingException(
53-
E::ts('Application process with ID "%1" not found', [1 => $this->applicationProcessId]),
54-
'invalid_arguments'
55-
);
56-
}
57-
58-
$result->rowCount = 1;
59-
$result->exchangeArray([
60-
'jsonSchema' => $event->getJsonSchema(),
61-
'uiSchema' => $event->getUiSchema(),
62-
'data' => $event->getData(),
63-
]);
64-
}
65-
66-
/**
67-
* @throws \CRM_Core_Exception
68-
*/
69-
private function createEvent(): GetApplicationFormEvent {
70-
return GetApplicationFormEvent::fromApiRequest($this, $this->createEventParams($this->getApplicationProcessId()));
30+
public function __construct() {
31+
parent::__construct(RemoteFundingApplicationProcess::getEntityName(), 'getForm');
7132
}
7233

7334
}

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

+5-55
Original file line numberDiff line numberDiff line change
@@ -19,72 +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\Remote\FundingCase\Traits\NewApplicationFormActionTrait;
2525
use Civi\Funding\Api4\Action\Traits\FundingCaseTypeIdParameterTrait;
2626
use Civi\Funding\Api4\Action\Traits\FundingProgramIdParameterTrait;
27-
use Civi\Funding\Event\Remote\FundingCase\GetNewApplicationFormEvent;
28-
use Civi\Funding\Exception\FundingException;
29-
use Civi\Funding\FundingProgram\FundingCaseTypeManager;
30-
use Civi\Funding\FundingProgram\FundingCaseTypeProgramRelationChecker;
31-
use Civi\Funding\FundingProgram\FundingProgramManager;
32-
use CRM_Funding_ExtensionUtil as E;
3327

34-
class GetNewApplicationFormAction extends AbstractNewApplicationFormAction {
28+
class GetNewApplicationFormAction extends AbstractRemoteFundingAction {
3529

3630
use FundingCaseTypeIdParameterTrait;
3731

3832
use FundingProgramIdParameterTrait;
3933

4034
use NewApplicationFormActionTrait;
4135

42-
public function __construct(
43-
FundingCaseTypeManager $fundingCaseTypeManager,
44-
FundingProgramManager $fundingProgramManager,
45-
CiviEventDispatcherInterface $eventDispatcher,
46-
FundingCaseTypeProgramRelationChecker $relationChecker
47-
) {
48-
parent::__construct(
49-
'getNewApplicationForm',
50-
$fundingCaseTypeManager,
51-
$fundingProgramManager,
52-
$eventDispatcher,
53-
$relationChecker,
54-
);
55-
}
56-
57-
/**
58-
* @inheritDoc
59-
*
60-
* @throws \CRM_Core_Exception
61-
*/
62-
public function _run(Result $result): void {
63-
$this->assertFundingCaseTypeAndProgramRelated($this->getFundingCaseTypeId(), $this->getFundingProgramId());
64-
$event = $this->createEvent();
65-
$this->dispatchEvent($event);
66-
67-
$result->debug['event'] = $event->getDebugOutput();
68-
if (NULL === $event->getJsonSchema() || NULL === $event->getUiSchema()) {
69-
throw new FundingException(E::ts('Invalid funding program ID or funding case type ID'), 'invalid_arguments');
70-
}
71-
72-
$result->rowCount = 1;
73-
$result->exchangeArray([
74-
'jsonSchema' => $event->getJsonSchema(),
75-
'uiSchema' => $event->getUiSchema(),
76-
'data' => $event->getData(),
77-
]);
78-
}
79-
80-
/**
81-
* @throws \CRM_Core_Exception
82-
*/
83-
private function createEvent(): GetNewApplicationFormEvent {
84-
return GetNewApplicationFormEvent::fromApiRequest(
85-
$this,
86-
$this->createEventParams($this->getFundingCaseTypeId(), $this->getFundingProgramId()),
87-
);
36+
public function __construct() {
37+
parent::__construct(RemoteFundingCase::getEntityName(), 'getNewApplicationForm');
8838
}
8939

9040
}

Civi/Funding/ApplicationProcess/Api4/ActionHandler/GetFormActionHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(
5656
*/
5757
public function getForm(GetFormAction $action): array {
5858
$applicationProcessBundle = $this->applicationProcessBundleLoader->get($action->getId());
59-
Assert::notNull($applicationProcessBundle, sprintf('Application process with ID %d not found', $action->getId()));
59+
Assert::notNull($applicationProcessBundle, sprintf('Application process with ID "%d" not found', $action->getId()));
6060
$applicationProcessStatusList = $this->applicationProcessBundleLoader->getStatusList($applicationProcessBundle);
6161

6262
$form = $this->createHandler->handle(new ApplicationFormCreateCommand(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Api4\Generic\Result;
24+
use Civi\Funding\Api4\Action\Remote\ApplicationProcess\GetFormAction;
25+
use Civi\RemoteTools\ActionHandler\ActionHandlerInterface;
26+
use Civi\RemoteTools\Api4\Api4Interface;
27+
28+
final class RemoteGetFormActionHandler implements ActionHandlerInterface {
29+
30+
public const ENTITY_NAME = 'RemoteFundingApplicationProcess';
31+
32+
private Api4Interface $api4;
33+
34+
public function __construct(Api4Interface $api4) {
35+
$this->api4 = $api4;
36+
}
37+
38+
public function getForm(GetFormAction $action): Result {
39+
return $this->api4->execute(FundingApplicationProcess::getEntityName(), 'getForm', [
40+
'id' => $action->getApplicationProcessId(),
41+
]);
42+
}
43+
44+
}

Civi/Funding/ApplicationProcess/Command/ApplicationFormNewCreateCommand.php

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

2525
final class ApplicationFormNewCreateCommand {
2626

27-
private int $contactId;
28-
2927
private FundingCaseTypeEntity $fundingCaseType;
3028

3129
private FundingProgramEntity $fundingProgram;
3230

3331
public function __construct(
34-
int $contactId,
3532
FundingCaseTypeEntity $fundingCaseType,
3633
FundingProgramEntity $fundingProgram
3734
) {
38-
$this->contactId = $contactId;
3935
$this->fundingCaseType = $fundingCaseType;
4036
$this->fundingProgram = $fundingProgram;
4137
}
4238

43-
public function getContactId(): int {
44-
return $this->contactId;
45-
}
46-
4739
public function getFundingCaseType(): FundingCaseTypeEntity {
4840
return $this->fundingCaseType;
4941
}

Civi/Funding/ApplicationProcess/Handler/ApplicationFormNewCreateHandler.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,37 @@
2727
use Civi\Funding\Form\JsonSchema\JsonFormsSubmitButtonsFactory;
2828
use Civi\RemoteTools\Form\RemoteForm;
2929
use Civi\RemoteTools\Form\RemoteFormInterface;
30+
use Civi\RemoteTools\RequestContext\RequestContextInterface;
3031

3132
final class ApplicationFormNewCreateHandler implements ApplicationFormNewCreateHandlerInterface {
3233

3334
private ApplicationJsonSchemaCreateHelper $jsonSchemaCreateHelper;
3435

3536
private NonCombinedApplicationJsonSchemaFactoryInterface $jsonSchemaFactory;
3637

38+
private RequestContextInterface $requestContext;
39+
3740
private ApplicationSubmitActionsFactoryInterface $submitActionsFactory;
3841

3942
private NonCombinedApplicationUiSchemaFactoryInterface $uiSchemaFactory;
4043

4144
public function __construct(
4245
ApplicationJsonSchemaCreateHelper $jsonSchemaCreateHelper,
4346
NonCombinedApplicationJsonSchemaFactoryInterface $jsonSchemaFactory,
47+
RequestContextInterface $requestContext,
4448
ApplicationSubmitActionsFactoryInterface $submitActionsFactory,
4549
NonCombinedApplicationUiSchemaFactoryInterface $uiSchemaFactory
4650
) {
4751
$this->jsonSchemaCreateHelper = $jsonSchemaCreateHelper;
4852
$this->jsonSchemaFactory = $jsonSchemaFactory;
53+
$this->requestContext = $requestContext;
4954
$this->submitActionsFactory = $submitActionsFactory;
5055
$this->uiSchemaFactory = $uiSchemaFactory;
5156
}
5257

5358
public function handle(ApplicationFormNewCreateCommand $command): RemoteFormInterface {
5459
$jsonSchema = $this->jsonSchemaFactory->createJsonSchemaInitial(
55-
$command->getContactId(),
60+
$this->requestContext->getContactId(),
5661
$command->getFundingCaseType(),
5762
$command->getFundingProgram(),
5863
);

Civi/Funding/ApplicationProcess/Handler/ApplicationFormNewValidateHandler.php

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function __construct(
4646

4747
public function handle(ApplicationFormNewValidateCommand $command): ApplicationFormValidationResult {
4848
$form = $this->formCreateHandler->handle(new ApplicationFormNewCreateCommand(
49-
$command->getContactId(),
5049
$command->getFundingCaseType(),
5150
$command->getFundingProgram()
5251
));

Civi/Funding/ApplicationProcess/Remote/Api4/ActionHandler/GetAddFormActionHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function getAddForm(GetAddFormAction $action): array {
9292
$applicationProcessBundle = $this->applicationProcessBundleLoader->get($action->getCopyDataFromId());
9393
Assert::notNull(
9494
$applicationProcessBundle,
95-
sprintf('Application process with ID %d not found', $action->getCopyDataFromId())
95+
sprintf('Application process with ID "%d" not found', $action->getCopyDataFromId())
9696
);
9797
Assert::same(
9898
$fundingCaseType->getId(),

Civi/Funding/Event/Remote/AbstractFundingGetFormEvent.php

-72
This file was deleted.

0 commit comments

Comments
 (0)