<?php
namespace Whater\Domain\User\Model;
use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
use Whater\Domain\Common\Exception\InvalidUUIDException;
use Ramsey\Uuid\Uuid;
use Whater\Domain\User\Exception\InvalidLicenseTypeException;
/**
* Class OrganizationLicenseFee
*
* @package Whater\Domain\User\Model
*/
class OrganizationLicenseFee implements ContainsRecordedEvents
{
use EventRecorderCapabilities;
/**
* @var string
*/
private $uuid;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @var \DateTime
*/
private $updatedAt;
/**
* @var string
*/
private $licenseType;
/**
* @var int
*/
private $maximumPopulation;
/**
* @var string
*/
private $establishmentType;
/**
* @var string
*/
private $licenseScope;
/**
* @var float
*/
private $licenseFee;
/**
* @var string
*/
private $paymentPlan;
/**
* @var string
*/
private $checksum;
public function __construct(
string $organizationLicenseFeeId = null,
string $licenseType,
string $licenseScope,
float $licenseFee,
string $paymentPlan,
int $maximumPopulation = null,
string $establishmentType = null
) {
try {
$this->uuid = Uuid::fromString($organizationLicenseFeeId ?: Uuid::uuid4())->toString();
} catch (\InvalidArgumentException $e) {
throw new InvalidUUIDException();
}
switch ($licenseType) {
case OrganizationLicense::LICENSE_TYPE_BASIC:
case OrganizationLicense::LICENSE_TYPE_STANDARD:
case OrganizationLicense::LICENSE_TYPE_PREMIUM:
$this->licenseType = $licenseType;
break;
default:
throw new InvalidLicenseTypeException();
}
switch ($licenseScope) {
case OrganizationLicense::LICENSE_SCOPE_ESTABLISHMENTS:
$this->establishmentType = $establishmentType;
case OrganizationLicense::LICENSE_SCOPE_TOWNS:
case OrganizationLicense::LICENSE_SCOPE_COUNTRIES_COUNTRY_AREAS_AGRUPATIONS:
case OrganizationLicense::LICENSE_SCOPE_PROFESSIONALS:
case OrganizationLicense::LICENSE_SCOPE_SELLERS:
$this->licenseScope = $licenseScope;
break;
default:
throw new InvalidLicenseTypeException();
}
$this->licenseFee = $licenseFee;
if ($this->licenseFee < 0) {
$this->licenseFee = 0;
}
switch ($paymentPlan) {
case OrganizationLicense::LICENSE_PAYMENT_PLAN_MONTHLY:
case OrganizationLicense::LICENSE_PAYMENT_PLAN_ANNUAL:
$this->paymentPlan = $paymentPlan;
break;
default:
throw new InvalidLicenseTypeException();
}
$this->maximumPopulation = $maximumPopulation;
if ($this->maximumPopulation != null && $this->maximumPopulation < 0) {
$this->maximumPopulation = 0;
}
//Calculate checksum
$hash = md5("LT" . $this->licenseType);
$hash = md5($hash . "PP" . $this->paymentPlan);
$hash = md5($hash . "LS" . $this->licenseScope);
if ($this->maximumPopulation != null) {
$hash = md5($hash . "MP" . $this->maximumPopulation);
}
if ($this->establishmentType != null) {
$hash = md5($hash . "ET" . $this->establishmentType);
}
$this->checksum = $hash;
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
public function editLicenseFee(
string $licenseType,
float $licenseFee,
string $paymentPlan,
int $maximumPopulation = null,
string $establishmentType = null
) {
switch ($licenseType) {
case OrganizationLicense::LICENSE_TYPE_BASIC:
case OrganizationLicense::LICENSE_TYPE_STANDARD:
case OrganizationLicense::LICENSE_TYPE_PREMIUM:
$this->licenseType = $licenseType;
break;
default:
throw new InvalidLicenseTypeException();
}
$this->licenseFee = $licenseFee;
if ($this->licenseFee < 0) {
$this->licenseFee = 0;
}
switch ($paymentPlan) {
case OrganizationLicense::LICENSE_PAYMENT_PLAN_MONTHLY:
case OrganizationLicense::LICENSE_PAYMENT_PLAN_ANNUAL:
$this->paymentPlan = $paymentPlan;
break;
default:
throw new InvalidLicenseTypeException();
}
$this->maximumPopulation = $maximumPopulation;
if ($this->maximumPopulation != null && $this->maximumPopulation < 0) {
$this->maximumPopulation = 0;
}
if ($this->licenseScope == OrganizationLicense::LICENSE_SCOPE_ESTABLISHMENTS) {
$this->establishmentType = $establishmentType;
}
//Calculate checksum
$hash = md5("LT" . $this->licenseType);
$hash = md5($hash . "PP" . $this->paymentPlan);
$hash = md5($hash . "LS" . $this->licenseScope);
if ($this->maximumPopulation != null) {
$hash = md5($hash . "MP" . $this->maximumPopulation);
}
$this->checksum = $hash;
$this->updatedAt = new \DateTime();
}
/**
* @return string
*/
public function id(): string
{
return $this->uuid;
}
/**
* @return string
*/
public function licenseType(): string
{
return $this->licenseType;
}
/**
* @return string
*/
public function licenseScope(): string
{
return $this->licenseScope;
}
/**
* @return string
*/
public function establishmentType(): ?string
{
return $this->establishmentType;
}
/**
* @return float
*/
public function licenseFee(): float
{
return $this->licenseFee;
}
/**
* @return string
*/
public function paymentPlan(): string
{
return $this->paymentPlan;
}
/**
* @return int
*/
public function maximumPopulation(): ?int
{
return $this->maximumPopulation;
}
/**
* @return string
*/
public function checksum(): string
{
return $this->checksum;
}
/**
* @return \DateTime
*/
public function createdAt(): \DateTime
{
return $this->createdAt;
}
/**
* @return \DateTime|null
*/
public function updatedAt()
{
return $this->updatedAt;
}
/**
* @return bool
*/
public function equals(OrganizationLicenseFee $organizationLicenseFee)
{
return $this->id() === $organizationLicenseFee->id();
}
}