Skip to content

Commit

Permalink
Create donor types enum
Browse files Browse the repository at this point in the history
Create shared `DonorType` class to indicate usage in use case requests.

Add types to AddDonationRequest and UpdateDonorRequest.
  • Loading branch information
gbirke authored and Abban committed Sep 23, 2020
1 parent 4c9e238 commit c2ff275
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 99 deletions.
49 changes: 49 additions & 0 deletions src/Domain/Model/DonorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare( strict_types = 1 );

namespace WMDE\Fundraising\DonationContext\Domain\Model;

/**
* @method static DonorType PERSON()
* @method static DonorType COMPANY()
* @method static DonorType ANONYMOUS()
*/
class DonorType {

private string $value;

private const TYPES = [
'PERSON' => 'person',
'COMPANY' => 'company',
'ANONYMOUS' => 'anonymous'
];

protected function __construct( $type ) {
if ( !isset( self::TYPES[$type] ) ) {
throw new \UnexpectedValueException( 'Invalid type: ' . $type );
}
$this->value = $type;
}

public static function __callStatic( $name, $arguments ): DonorType {
return new self( $name );
}

public function __toString(): string {
return self::TYPES[$this->value];
}

public static function make( $value ): DonorType {
$valueMap = array_flip( self::TYPES );
if ( !isset( $valueMap[$value] ) ) {
throw new \UnexpectedValueException( 'Invalid value: ' . $value );
}
return new self( $valueMap[$value] );
}

public function is( DonorType $donorType ): bool {
return $this->value === $donorType->value;
}

}
73 changes: 36 additions & 37 deletions src/UseCases/AddDonation/AddDonationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,58 @@
namespace WMDE\Fundraising\DonationContext\UseCases\AddDonation;

use WMDE\Euro\Euro;
use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;
use WMDE\Fundraising\PaymentContext\Domain\Model\BankData;

/**
* @license GPL-2.0-or-later
*/
class AddDonationRequest {

public const TYPE_PERSON = 'person';
public const TYPE_COMPANY = 'company';
public const TYPE_ANONYMOUS = 'anonymous';

private $donorType;
private $donorFirstName;
private $donorLastName;
private $donorSalutation;
private $donorTitle;
private $donorCompany;
private $donorStreetAddress;
private $donorPostalCode;
private $donorCity;
private $donorCountryCode;
private $donorEmailAddress;
private DonorType $donorType;
private string $donorFirstName = '';
private string $donorLastName = '';
private string $donorSalutation = '';
private string $donorTitle = '';
private string $donorCompany = '';
private string $donorStreetAddress = '';
private string $donorPostalCode = '';
private string $donorCity = '';
private string $donorCountryCode = '';
private string $donorEmailAddress = '';

/**
* Newsletter subscription
*
* @var string
*/
private $optIn = '';
private string $optIn = '';

# donation
private $amount;
private $paymentType = '';
private $interval = 0;
private Euro $amount;
private string $paymentType = '';
private int $interval = 0;

# direct debit related
private $bankData;
private ?BankData $bankData;

# tracking
private $tracking = '';
private $source = '';
private $totalImpressionCount = 0;
private $singleBannerImpressionCount = 0;
private string $tracking = '';
private string $source = '';
private int $totalImpressionCount = 0;
private int $singleBannerImpressionCount = 0;
// Legacy values, will be deprecated in the future
private $color = '';
private $skin = '';
private $layout = '';
private string $color = '';
private string $skin = '';
private string $layout = '';

private $optsIntoDonationReceipt = true;
private bool $optsIntoDonationReceipt = true;

/**
* AddDonationRequest constructor.
*/
public function __construct() {
$this->amount = Euro::newFromCents( 0 );
$this->donorType = DonorType::ANONYMOUS();
}

public function getOptIn(): string {
return $this->optIn;
Expand Down Expand Up @@ -148,11 +151,11 @@ public function getLayout(): string {
return $this->layout;
}

public function getDonorType(): string {
public function getDonorType(): DonorType {
return $this->donorType;
}

public function setDonorType( string $donorType ): void {
public function setDonorType( DonorType $donorType ): void {
$this->donorType = $donorType;
}

Expand Down Expand Up @@ -237,11 +240,7 @@ public function setDonorEmailAddress( string $donorEmailAddress ): void {
}

public function donorIsAnonymous(): bool {
return $this->getDonorType() === self::TYPE_ANONYMOUS;
}

public function donorIsCompany(): bool {
return $this->getDonorType() === self::TYPE_COMPANY;
return $this->getDonorType()->is( DonorType::ANONYMOUS() );
}

public function setOptsIntoDonationReceipt( bool $optIn ): void {
Expand Down
45 changes: 23 additions & 22 deletions src/UseCases/AddDonation/AddDonationUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;
use WMDE\Fundraising\DonationContext\Domain\Repositories\DonationRepository;
use WMDE\Fundraising\DonationContext\EventEmitter;
use WMDE\Fundraising\DonationContext\Infrastructure\DonationConfirmationMailer;
Expand Down Expand Up @@ -112,29 +113,29 @@ private function newDonationFromRequest( AddDonationRequest $donationRequest ):
}

private function getPersonalInfoFromRequest( AddDonationRequest $request ): Donor {
switch ( $request->getDonorType() ) {
case AddDonationRequest::TYPE_PERSON:
return new PersonDonor(
new PersonName(
$request->getDonorFirstName(),
$request->getDonorLastName(),
$request->getDonorSalutation(),
$request->getDonorTitle()
),
$this->getPhysicalAddressFromRequest( $request ),
$request->getDonorEmailAddress()
);
case AddDonationRequest::TYPE_COMPANY:
return new CompanyDonor(
new CompanyName( $request->getDonorCompany() ),
$this->getPhysicalAddressFromRequest( $request ),
$request->getDonorEmailAddress()
);
case AddDonationRequest::TYPE_ANONYMOUS:
return new AnonymousDonor();
default:
throw new \InvalidArgumentException( sprintf( 'Unknown donor type: %s', $request->getDonorType() ) );
$donorType = $request->getDonorType();
if ( $donorType->is( DonorType::PERSON() ) ) {

return new PersonDonor(
new PersonName(
$request->getDonorFirstName(),
$request->getDonorLastName(),
$request->getDonorSalutation(),
$request->getDonorTitle()
),
$this->getPhysicalAddressFromRequest( $request ),
$request->getDonorEmailAddress()
);
} elseif ( $donorType->is( DonorType::COMPANY() ) ) {
return new CompanyDonor(
new CompanyName( $request->getDonorCompany() ),
$this->getPhysicalAddressFromRequest( $request ),
$request->getDonorEmailAddress()
);
} elseif ( $donorType->is( DonorType::ANONYMOUS() ) ) {
return new AnonymousDonor();
}
throw new \InvalidArgumentException( sprintf( 'Unknown donor type: %s', $request->getDonorType() ) );
}

private function getPhysicalAddressFromRequest( AddDonationRequest $request ): PostalAddress {
Expand Down
8 changes: 5 additions & 3 deletions src/UseCases/AddDonation/AddDonationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace WMDE\Fundraising\DonationContext\UseCases\AddDonation;

use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;
use WMDE\Fundraising\DonationContext\UseCases\AddDonation\AddDonationValidationResult as Result;
use WMDE\Fundraising\PaymentContext\Domain\BankDataValidationResult;
use WMDE\Fundraising\PaymentContext\Domain\BankDataValidator;
Expand Down Expand Up @@ -118,14 +119,15 @@ private function validateFieldLength( string $value, string $fieldName ): void {

private function validateDonor(): void {
$this->validateFieldLength( $this->request->getDonorEmailAddress(), Result::SOURCE_DONOR_EMAIL );
if ( $this->request->getDonorType() === AddDonationRequest::TYPE_PERSON ) {
$donorType = $this->request->getDonorType();
if ( $donorType->is( DonorType::PERSON() ) ) {
$this->violations = array_merge(
$this->violations,
$this->getPersonNameViolations(),
$this->getAddressViolations(),
$this->validateEmail()->getViolations()
);
} elseif ( $this->request->getDonorType() === AddDonationRequest::TYPE_COMPANY ) {
} elseif ( $donorType->is( DonorType::COMPANY() ) ) {
$this->violations = array_merge(
$this->violations,
$this->getCompanyNameViolations(),
Expand Down Expand Up @@ -179,7 +181,7 @@ private function validateIban( Iban $iban ): void {
}

private function validateEmail(): ValidationResult {
if ( $this->request->getDonorType() === AddDonationRequest::TYPE_ANONYMOUS ) {
if ( $this->request->donorIsAnonymous() ) {
return new ValidationResult();
}
return $this->emailValidator->validate( $this->request->getDonorEmailAddress() );
Expand Down
38 changes: 20 additions & 18 deletions src/UseCases/UpdateDonor/UpdateDonorRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@

namespace WMDE\Fundraising\DonationContext\UseCases\UpdateDonor;

use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;

/**
* @license GPL-2.0-or-later
*/
class UpdateDonorRequest {

public const TYPE_PERSON = 'person';
public const TYPE_COMPANY = 'company';
public const TYPE_ANONYMOUS = 'anonymous';

private $donationId;
private $donorType;
private $firstName;
private $lastName;
private $salutation;
private $title;
private $companyName;
private $streetAddress;
private $postalCode;
private $city;
private $countryCode;
private $emailAddress;
private int $donationId = 0;
private DonorType $donorType;
private string $firstName = '';
private string $lastName = '';
private string $salutation = '';
private string $title = '';
private string $companyName = '';
private string $streetAddress = '';
private string $postalCode = '';
private string $city = '';
private string $countryCode = '';
private string $emailAddress = '';

public function __construct() {
$this->donorType = DonorType::PERSON();
}

public static function newInstance(): self {
return new self();
}

public function withType( string $donorType ): self {
public function withType( DonorType $donorType ): self {
$request = clone $this;
$request->donorType = $donorType;
return $request;
Expand Down Expand Up @@ -102,7 +104,7 @@ public function withDonationId( int $donationId ): self {
return $request;
}

public function getDonorType(): string {
public function getDonorType(): DonorType {
return $this->donorType;
}

Expand Down
5 changes: 3 additions & 2 deletions src/UseCases/UpdateDonor/UpdateDonorUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;
use WMDE\Fundraising\DonationContext\Domain\Repositories\DonationRepository;
use WMDE\Fundraising\DonationContext\Infrastructure\DonationConfirmationMailer;

Expand Down Expand Up @@ -81,7 +82,7 @@ private function getDonorAddressFromRequest( UpdateDonorRequest $updateDonorRequ
}

private function getDonorFromRequest( UpdateDonorRequest $updateDonorRequest ): Donor {
if ( $updateDonorRequest->getDonorType() === UpdateDonorRequest::TYPE_PERSON ) {
if ( $updateDonorRequest->getDonorType()->is( DonorType::PERSON() ) ) {
return new PersonDonor(
new PersonName(
$updateDonorRequest->getFirstName(),
Expand All @@ -93,7 +94,7 @@ private function getDonorFromRequest( UpdateDonorRequest $updateDonorRequest ):
$updateDonorRequest->getEmailAddress()
);

} elseif ( $updateDonorRequest->getDonorType() === UpdateDonorRequest::TYPE_COMPANY ) {
} elseif ( $updateDonorRequest->getDonorType()->is( DonorType::COMPANY() ) ) {
return new CompanyDonor(
new CompanyName( $updateDonorRequest->getCompanyName() ),
$this->getDonorAddressFromRequest( $updateDonorRequest ),
Expand Down
10 changes: 6 additions & 4 deletions src/UseCases/UpdateDonor/UpdateDonorValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace WMDE\Fundraising\DonationContext\UseCases\UpdateDonor;

use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;
use WMDE\FunValidators\ConstraintViolation;
use WMDE\FunValidators\Validators\AddressValidator;
use WMDE\FunValidators\Validators\EmailValidator;
Expand All @@ -25,14 +26,15 @@ public function __construct( AddressValidator $addressValidator, EmailValidator
}

public function validateDonorData( UpdateDonorRequest $donorRequest ): UpdateDonorValidationResult {
if ( $donorRequest->getDonorType() === UpdateDonorRequest::TYPE_PERSON ) {
$donorType = $donorRequest->getDonorType();
if ( $donorType->is( DonorType::PERSON() ) ) {
$nameViolations = $this->getPersonViolations( $donorRequest );
} elseif ( $donorRequest->getDonorType() === UpdateDonorRequest::TYPE_COMPANY ) {
} elseif ( $donorType->is( DonorType::COMPANY() ) ) {
$nameViolations = $this->getCompanyViolations( $donorRequest );
} elseif ( $donorRequest->getDonorType() === UpdateDonorRequest::TYPE_ANONYMOUS ) {
} elseif ( $donorType->is( DonorType::ANONYMOUS() ) ) {
return new UpdateDonorValidationResult( $this->getAnonymousViolation( $donorRequest ) );
} else {
throw new \InvalidArgumentException( sprintf( ' Unknown donor type: %s', $donorRequest->getDonorType() ) );
throw new \InvalidArgumentException( sprintf( ' Unknown donor type: %s', $donorType ) );
}

$violations = array_merge(
Expand Down
3 changes: 2 additions & 1 deletion tests/Data/ValidAddDonationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WMDE\Fundraising\DonationContext\Tests\Data;

use WMDE\Euro\Euro;
use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;
use WMDE\Fundraising\DonationContext\UseCases\AddDonation\AddDonationRequest;
use WMDE\Fundraising\PaymentContext\Domain\Model\BankData;
use WMDE\Fundraising\PaymentContext\Domain\Model\Iban;
Expand All @@ -24,7 +25,7 @@ public static function getRequest(): AddDonationRequest {
$request->setOptIn( (string)ValidDonation::OPTS_INTO_NEWSLETTER );
$request->setPaymentType( PaymentMethod::DIRECT_DEBIT );

$request->setDonorType( AddDonationRequest::TYPE_PERSON );
$request->setDonorType( DonorType::PERSON() );
$request->setDonorSalutation( ValidDonation::DONOR_SALUTATION );
$request->setDonorTitle( ValidDonation::DONOR_TITLE );
$request->setDonorCompany( '' );
Expand Down
Loading

0 comments on commit c2ff275

Please sign in to comment.