<?phpnamespace Whater\Domain\Product\Model;use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;use Ramsey\Uuid\Uuid;use Whater\Domain\Common\Exception\InvalidUUIDException;/** * Class ProductVariation * * @package Whater\Domain\Product\Model */class ProductVariation implements ContainsRecordedEvents{ use EventRecorderCapabilities; /** * @var string */ private $uuid; /** * @var string */ private $productVariationShortDescription; /** * @var string */ private $productVariationDescription; /** * @var float */ private $productVariationPrice; /** * @var Product */ private $product; /** * @var array */ private $productVariationAttributes; /** * @var string */ protected $checksum; /** * @var \DateTime */ private $createdAt; /** * @var null|\DateTime */ private $updatedAt; /** * @param string $productVariationId */ public function __construct( string $productVariationId = null, Product $product, float $productVariationPrice, array $productVariationAttributes, string $productVariationShortDescription = null, string $productVariationDescription = null ) { try { $this->uuid = Uuid::fromString($productVariationId ?: Uuid::uuid4())->toString(); } catch (\InvalidArgumentException $e) { throw new InvalidUUIDException(); } $this->productVariationShortDescription = $productVariationShortDescription; $this->productVariationDescription = $productVariationDescription; $this->productVariationPrice = $productVariationPrice; $this->productVariationAttributes = $productVariationAttributes; $this->product = $product; $this->createdAt = new \DateTime(); $this->updatedAt = new \DateTime(); // Calculate checksum $hash = md5($this->product()->id()); $hash = md5($hash . json_encode($this->productVariationAttributes())); $this->checksum = $hash; } public function editProductVariation( float $productVariationPrice, array $productVariationAttributes, string $productVariationShortDescription = null, string $productVariationDescription = null ) { $this->productVariationAttributes = $productVariationAttributes; $this->productVariationShortDescription = $productVariationShortDescription; $this->productVariationDescription = $productVariationDescription; $this->productVariationPrice = $productVariationPrice; $hash = md5($this->product()->id()); $hash = md5($hash . json_encode($this->productVariationAttributes())); $this->checksum = $hash; $this->updatedAt = new \DateTime(); } /** * @return string */ public function id(): string { return $this->uuid; } /** * @return string */ public function productVariationShortDescription(): ?string { return $this->productVariationShortDescription; } /** * @return string */ public function productVariationDescription(): ?string { return $this->productVariationDescription; } /** * @return float */ public function productVariationPrice(): float { return $this->productVariationPrice; } /** * @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; } public function productVariationAttributes(): ?array { return $this->productVariationAttributes; } /** * @return Product */ public function product() { return $this->product; } /** * @return bool */ public function equals(ProductVariation $productVariation) { return $this->id() === $productVariation->id(); }}