Skip to content

Commit

Permalink
TDD - create project entity
Browse files Browse the repository at this point in the history
  • Loading branch information
rigonlucas committed Aug 25, 2024
1 parent 64560ab commit db200d5
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 0 deletions.
88 changes: 88 additions & 0 deletions core/Domain/Entities/Project/ProjectEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Core\Domain\Entities\Project;

use Carbon\CarbonInterface;
use Core\Domain\Entities\Account\AccountEntity;
use Core\Domain\Entities\Project\Traits\HasProjectEntityBuilder;
use Core\Domain\Entities\Project\Traits\ProjectEntityAcessors;
use Core\Domain\Entities\User\UserEntity;
use Core\Support\Exceptions\Dates\DateMustBeBeforeOtherException;
use Core\Support\Exceptions\Dates\DateMustBeInCurrentDayException;
use Core\Support\Exceptions\Dates\DateRequiredException;
use Core\Support\Exceptions\Dates\DatesMustBeDifferntsException;
use Core\Support\Exceptions\ForbidenException;
use Core\Support\Permissions\UserRoles;
use Ramsey\Uuid\UuidInterface;

class ProjectEntity
{
use ProjectEntityAcessors;
use HasProjectEntityBuilder;

private int $id;
private UuidInterface $uuid;
private string $name;
private string $description;
private ?UserEntity $user = null;
private ?AccountEntity $account = null;
private ?CarbonInterface $startAt = null;
private ?CarbonInterface $finishAt = null;

private function __construct()
{
}

/**
* @throws ForbidenException
*/
private function canCreateProject(): void
{
if (is_null($this->user)) {
throw new ForbidenException('An user is required to create a project');
}

if ($this->user->hasNotAnyPermissionFromArray([UserRoles::ADMIN, UserRoles::EDITOR])) {
throw new ForbidenException('You do not have permission to create a project');
}

if (is_null($this->account)) {
throw new ForbidenException('An account is required to create a project');
}

if ($this->account->getId() !== $this->user->getAccount()->getId()) {
throw new ForbidenException('Your account is not allowed to create a project in this account');
}
}

/**
* @throws DateMustBeBeforeOtherException
* @throws DateMustBeInCurrentDayException
* @throws DateRequiredException
* @throws DatesMustBeDifferntsException
*/
private function datesValidation(): void
{
if (!is_null($this->startAt) && !is_null($this->finishAt)) {
if ($this->startAt->isAfter($this->finishAt)) {
throw new DateMustBeBeforeOtherException('The start date must be before the end date');
}

if ($this->startAt->isSameDay($this->finishAt)) {
throw new DatesMustBeDifferntsException('The start date and finish date must be differents days');
}
}

if (is_null($this->startAt)) {
if (!is_null($this->finishAt)) {
throw new DateRequiredException('The start date is required when the end date is informed');
}
}

if ($this->startAt) {
if ($this->startAt->isBefore(now()->startOfDay())) {
throw new DateMustBeInCurrentDayException('The start date must be before the current day');
}
}
}
}
59 changes: 59 additions & 0 deletions core/Domain/Entities/Project/Traits/HasProjectEntityBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Core\Domain\Entities\Project\Traits;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use Core\Domain\Entities\Account\AccountEntity;
use Core\Domain\Entities\Project\ProjectEntity;
use Core\Domain\Entities\User\UserEntity;
use Core\Support\Exceptions\Dates\DateMustBeBeforeOtherException;
use Core\Support\Exceptions\Dates\DateMustBeInCurrentDayException;
use Core\Support\Exceptions\Dates\DateRequiredException;
use Core\Support\Exceptions\Dates\DatesMustBeDifferntsException;
use Core\Support\Exceptions\ForbidenException;
use Ramsey\Uuid\UuidInterface;

trait HasProjectEntityBuilder
{
/**
* @param string $name
* @param string $description
* @param UserEntity $user
* @param AccountEntity $account
* @param UuidInterface $uuid
* @param Carbon|null $startAt
* @param Carbon|null $finishAt
* @return ProjectEntity
* @throws ForbidenException
* @throws DateMustBeBeforeOtherException
* @throws DateMustBeInCurrentDayException
* @throws DateRequiredException
* @throws DatesMustBeDifferntsException
*/
public static function create(
string $name,
string $description,
UserEntity $user,
AccountEntity $account,
UuidInterface $uuid,
CarbonInterface $startAt = null,
CarbonInterface $finishAt = null
): ProjectEntity {
$project = new ProjectEntity();
$project->setUser($user);
$project->setAccount($account);
$project->canCreateProject();

$project->setStartAt($startAt);
$project->setFinishAt($finishAt);
$project->datesValidation();

$project->setName($name);
$project->setDescription($description);
$project->setUuid($uuid);

return $project;
}

}
93 changes: 93 additions & 0 deletions core/Domain/Entities/Project/Traits/ProjectEntityAcessors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Core\Domain\Entities\Project\Traits;

use Carbon\CarbonInterface;
use Core\Domain\Entities\Account\AccountEntity;
use Core\Domain\Entities\User\UserEntity;
use Ramsey\Uuid\UuidInterface;

trait ProjectEntityAcessors
{

public function getId(): int
{
return $this->id;
}

public function setId(int $id): void
{
$this->id = $id;
}

public function getUuid(): UuidInterface
{
return $this->uuid;
}

public function setUuid(UuidInterface $uuid): void
{
$this->uuid = $uuid;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): void
{
$this->name = $name;
}

public function getDescription(): string
{
return $this->description;
}

public function setDescription(string $description): void
{
$this->description = $description;
}

public function getUser(): UserEntity
{
return $this->user;
}

public function setUser(UserEntity $user): void
{
$this->user = $user;
}

public function getAccount(): AccountEntity
{
return $this->account;
}

public function setAccount(AccountEntity $account): void
{
$this->account = $account;
}


public function getStartAt(): ?CarbonInterface
{
return $this->startAt;
}

public function setStartAt(?CarbonInterface $startAt): void
{
$this->startAt = $startAt;
}

public function getFinishAt(): ?CarbonInterface
{
return $this->finishAt;
}

public function setFinishAt(?CarbonInterface $finishAt): void
{
$this->finishAt = $finishAt;
}
}
19 changes: 19 additions & 0 deletions core/Support/Exceptions/Dates/DateMustBeBeforeOtherException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Core\Support\Exceptions\Dates;

use Core\Support\Exceptions\OutputErrorException;
use Core\Support\Http\ResponseStatus;
use Exception;

class DateMustBeBeforeOtherException extends OutputErrorException
{
public function __construct(
string $message,
int $code = ResponseStatus::FORBIDDEN->value,
Exception $previous = null,
?array $errors = []
) {
parent::__construct($message, $code, $previous, $errors);
}
}
19 changes: 19 additions & 0 deletions core/Support/Exceptions/Dates/DateMustBeInCurrentDayException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Core\Support\Exceptions\Dates;

use Core\Support\Exceptions\OutputErrorException;
use Core\Support\Http\ResponseStatus;
use Exception;

class DateMustBeInCurrentDayException extends OutputErrorException
{
public function __construct(
string $message,
int $code = ResponseStatus::FORBIDDEN->value,
Exception $previous = null,
?array $errors = []
) {
parent::__construct($message, $code, $previous, $errors);
}
}
19 changes: 19 additions & 0 deletions core/Support/Exceptions/Dates/DateRequiredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Core\Support\Exceptions\Dates;

use Core\Support\Exceptions\OutputErrorException;
use Core\Support\Http\ResponseStatus;
use Exception;

class DateRequiredException extends OutputErrorException
{
public function __construct(
string $message,
int $code = ResponseStatus::FORBIDDEN->value,
Exception $previous = null,
?array $errors = []
) {
parent::__construct($message, $code, $previous, $errors);
}
}
19 changes: 19 additions & 0 deletions core/Support/Exceptions/Dates/DatesMustBeDifferntsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Core\Support\Exceptions\Dates;

use Core\Support\Exceptions\OutputErrorException;
use Core\Support\Http\ResponseStatus;
use Exception;

class DatesMustBeDifferntsException extends OutputErrorException
{
public function __construct(
string $message,
int $code = ResponseStatus::FORBIDDEN->value,
Exception $previous = null,
?array $errors = []
) {
parent::__construct($message, $code, $previous, $errors);
}
}
Loading

0 comments on commit db200d5

Please sign in to comment.