Select Git revision
CapsuleBuilder.php

Camille Simiand authored
CapsuleBuilder.php 4.16 KiB
<?php
namespace App\Builder;
use App\Entity\Capsule;
use App\Entity\Group;
use App\Entity\User;
use App\Helper\ContractHelper;
class CapsuleBuilder
{
public Capsule $capsule;
private bool $hasRequiredName = false;
private bool $hasRequiredCreationAuthor = false;
private bool $hastRequiredCreationDate = false;
private bool $hasRequiredPreviewLink = false;
private bool $hasRequiredEditionLink = false;
private bool $hasRequiredUpdateDate = false;
private bool $hasPasswordSet = false;
public function __construct()
{
$this->capsule = new Capsule();
}
public function withName(string $name): CapsuleBuilder
{
$this->capsule->setName($name);
$this->hasRequiredName = true;
return $this;
}
public function withCreationAuthor(User $creation_author): CapsuleBuilder
{
$this->capsule->setCreationAuthor($creation_author);
$this->hasRequiredCreationAuthor = true;
$this->capsule->addEditor($creation_author);
return $this;
}
public function withCreationDate(\DateTime $creation_date): CapsuleBuilder
{
$this->capsule->setCreationDate($creation_date);
$this->hastRequiredCreationDate = true;
return $this;
}
public function withLinkPath(string $preview_link): CapsuleBuilder
{
$this->capsule->setLinkPath($preview_link);
$this->hasRequiredPreviewLink = true;
return $this;
}
private function createEditionLink(): void
{
ContractHelper::requires(
$this->hasRequiredPreviewLink,
"The call of CapsuleBuilder::withPreviewLink should be " .
"called before CapsuleBuilder::createEditionLink"
);
ContractHelper::requires(
$this->hasPasswordSet,
"The call of CapsuleBuilder::withPassword should be " .
"called before CapsuleBuilder::createEditionLink"
);
$this->capsule->setEditionLink($this->capsule->getLinkPath() .
"/?p=" . $this->capsule->getPassword());
$this->hasRequiredEditionLink = true;
}
public function withUpdateAuthor(User $update_author): CapsuleBuilder
{
$this->capsule->setUpdateAuthor($update_author);
return $this;
}
public function withUpdateDate(\DateTime $update_date): CapsuleBuilder
{
$this->capsule->setUpdatedDate($update_date);
$this->hasRequiredUpdateDate = true;
return $this;
}
public function withPassword(string $password): CapsuleBuilder
{
$this->capsule->setPassword($password);
$this->hasPasswordSet = true;
return $this;
}
public function withGroup(Group $group): CapsuleBuilder
{
$this->capsule->addGroup($group);
return $this;
}
public function createCapsule(): Capsule
{
$this->createEditionLink();
ContractHelper::requires(
$this->hasRequiredName,
"The call of CapsuleBuilder::withName should be called before CapsuleBuilder::create"
);
ContractHelper::requires(
$this->hasRequiredCreationAuthor,
"The call of CapsuleBuilder::withCreationAuthor should be called before CapsuleBuilder::create"
);
ContractHelper::requires(
$this->hastRequiredCreationDate,
"The call of CapsuleBuilder::withCreationDate should be called before CapsuleBuilder::create"
);
ContractHelper::requires(
$this->hasRequiredPreviewLink,
"The call of CapsuleBuilder::withPreviewLink should be called before CapsuleBuilder::create"
);
ContractHelper::requires(
$this->hasRequiredEditionLink,
"The call of CapsuleBuilder::withEditionLink should be called before CapsuleBuilder::create"
);
ContractHelper::requires(
$this->hasRequiredUpdateDate,
"The call of CapsuleBuilder::withUpdateDate should be called before CapsuleBuilder::create"
);
ContractHelper::requires(
$this->hasPasswordSet,
"The capsule should have its password defined"
);
return $this->capsule;
}
}