Skip to content

Commit

Permalink
Tidy up entity handling around enum casting and life sustaining treat…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
cooperaj committed Dec 16, 2024
1 parent 59c0e66 commit 627fca2
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 337 deletions.

This file was deleted.

12 changes: 11 additions & 1 deletion service-api/app/src/App/src/Entity/Casters/CastToCaseSubtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@
use Attribute;
use EventSauce\ObjectHydrator\ObjectMapper;
use EventSauce\ObjectHydrator\PropertyCaster;
use InvalidArgumentException;

#[Attribute(Attribute::TARGET_PARAMETER)]
class CastToCaseSubtype implements PropertyCaster
{
public function cast(mixed $value, ObjectMapper $hydrator): string
{
return LpaType::fromShortName($value)->value;
if (is_null(LpaType::tryFrom($value))) {
$value = match ($value) {
'personal-welfare' => LpaType::PERSONAL_WELFARE->value,
'property-and-affairs' => LpaType::PROPERTY_AND_AFFAIRS->value,

Check warning on line 21 in service-api/app/src/App/src/Entity/Casters/CastToCaseSubtype.php

View check run for this annotation

Codecov / codecov/patch

service-api/app/src/App/src/Entity/Casters/CastToCaseSubtype.php#L21

Added line #L21 was not covered by tests
default =>
throw new InvalidArgumentException('Invalid shorthand name: ' . $value),

Check warning on line 23 in service-api/app/src/App/src/Entity/Casters/CastToCaseSubtype.php

View check run for this annotation

Codecov / codecov/patch

service-api/app/src/App/src/Entity/Casters/CastToCaseSubtype.php#L23

Added line #L23 was not covered by tests
};
}

return LpaType::from($value)->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@
use Attribute;
use EventSauce\ObjectHydrator\ObjectMapper;
use EventSauce\ObjectHydrator\PropertyCaster;
use InvalidArgumentException;

#[Attribute(Attribute::TARGET_PARAMETER)]
class CastToLifeSustainingTreatment implements PropertyCaster
{
public function cast(mixed $value, ObjectMapper $hydrator): mixed
{
if (is_null(LifeSustainingTreatment::tryFrom($value))) {
$value = match ($value) {
'Option A' => LifeSustainingTreatment::OPTION_A->value,
'Option B' => LifeSustainingTreatment::OPTION_B->value,
default => throw new InvalidArgumentException('Invalid shorthand name: ' . $value),

Check warning on line 22 in service-api/app/src/App/src/Entity/Casters/CastToLifeSustainingTreatment.php

View check run for this annotation

Codecov / codecov/patch

service-api/app/src/App/src/Entity/Casters/CastToLifeSustainingTreatment.php#L21-L22

Added lines #L21 - L22 were not covered by tests
};
}

return LifeSustainingTreatment::from($value)->value;
}
}
3 changes: 0 additions & 3 deletions service-api/app/src/App/src/Entity/LpaStore/LpaStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

use App\Entity\Casters\CastToCaseSubtype;
use App\Entity\Casters\CastToLifeSustainingTreatment;
use App\Entity\Casters\CastToAttorneyActDecisions;
use App\Entity\Lpa;
use App\Enum\ActorStatus;
use App\Enum\HowAttorneysMakeDecisions;
use App\Enum\LifeSustainingTreatment;
use App\Enum\LpaType;
use App\Enum\WhenTheLpaCanBeUsed;
use App\Service\Lpa\GetAttorneyStatus\AttorneyStatus;
use App\Service\Lpa\GetAttorneyStatus\GetAttorneyStatusInterface;
use App\Service\Lpa\ResolveActor\ResolveActorInterface;
use DateTimeImmutable;
Expand All @@ -31,7 +29,6 @@ public function __construct(
LpaType $caseSubtype,
string $channel,
LpaStoreDonor $donor,
#[CastToAttorneyActDecisions]
?HowAttorneysMakeDecisions $howAttorneysMakeDecisions,
#[MapFrom('lifeSustainingTreatmentOption')]
#[CastToLifeSustainingTreatment]
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions service-api/app/src/App/src/Entity/Sirius/SiriusLpa.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Entity\Sirius;

use App\Entity\Casters\CastToLifeSustainingTreatment;
use App\Entity\Casters\CastToWhenTheLpaCanBeUsed;
use App\Entity\Lpa;
use App\Enum\HowAttorneysMakeDecisions;
Expand All @@ -17,7 +18,6 @@
use DateTimeImmutable;
use EventSauce\ObjectHydrator\MapFrom;
use EventSauce\ObjectHydrator\PropertyCasters\CastListToType;
use App\Entity\Sirius\Casters\CastToSiriusLifeSustainingTreatment;
use Exception;

class SiriusLpa extends Lpa implements FindActorInLpaInterface
Expand All @@ -37,7 +37,7 @@ public function __construct(
?SiriusLpaDonor $donor,
?bool $hasSeveranceWarning,
?DateTimeImmutable $invalidDate,
#[CastToSiriusLifeSustainingTreatment]
#[CastToLifeSustainingTreatment]
?LifeSustainingTreatment $lifeSustainingTreatment,
?DateTimeImmutable $lpaDonorSignatureDate,
?bool $lpaIsCleansed,
Expand Down
11 changes: 0 additions & 11 deletions service-api/app/src/App/src/Enum/LifeSustainingTreatment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@

namespace App\Enum;

use InvalidArgumentException;

enum LifeSustainingTreatment: string
{
case OPTION_A = 'option-a';
case OPTION_B = 'option-b';

public static function fromShortName(string $shortName): self
{
return match ($shortName) {
'Option A' => self::OPTION_A,
'Option B' => self::OPTION_B,
default => throw new InvalidArgumentException('Invalid shorthand name: ' . $shortName),
};
}
}
11 changes: 0 additions & 11 deletions service-api/app/src/App/src/Enum/LpaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@

namespace App\Enum;

use InvalidArgumentException;

enum LpaType: string
{
case PERSONAL_WELFARE = 'hw';
case PROPERTY_AND_AFFAIRS = 'pfa';

public static function fromShortName(string $shortName): self
{
return match ($shortName) {
'personal-welfare' => self::PERSONAL_WELFARE,
'property-and-affairs' => self::PROPERTY_AND_AFFAIRS,
default => throw new InvalidArgumentException('Invalid shorthand name: ' . $shortName),
};
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function expectedSiriusLpa(): SiriusLpa
caseAttorneyJointly: false,
caseAttorneyJointlyAndJointlyAndSeverally: false,
caseAttorneyJointlyAndSeverally: true,
caseSubtype: LpaType::fromShortName('personal-welfare'),
caseSubtype: LpaType::PERSONAL_WELFARE,
channel: null,
dispatchDate: null,
donor: new SiriusLpaDonor(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getExpectedLpa(): SiriusLpa
),
hasSeveranceWarning: null,
invalidDate: null,
lifeSustainingTreatment: LifeSustainingTreatment::fromShortName('Option A'),
lifeSustainingTreatment: LifeSustainingTreatment::OPTION_A,
lpaDonorSignatureDate: new DateTimeImmutable('2012-12-12'),
lpaIsCleansed: true,
onlineLpaId: 'A33718377316',
Expand Down
Loading

0 comments on commit 627fca2

Please sign in to comment.