Skip to content

Commit

Permalink
Merge pull request #11 from irontec/CDD-3-add-service-entity
Browse files Browse the repository at this point in the history
Add service entity
  • Loading branch information
danigargar authored Feb 20, 2024
2 parents 8a75c2b + 1b3a5f9 commit b736ead
Show file tree
Hide file tree
Showing 16 changed files with 634 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/config/api/raw/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ Demo\Domain\Model\Administrator\Administrator:
ROLE_ADMIN:
id:
neq: 1

Demo\Domain\Model\Service\Service: ~


4 changes: 3 additions & 1 deletion app/config/packages/orm_target_entities.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ doctrine:
Ivoz\Core\Domain\Model\Changelog\ChangelogInterface:
Ivoz\Core\Domain\Model\Changelog\Changelog
Demo\Domain\Model\Timezone\TimezoneInterface:
Demo\Domain\Model\Timezone\Timezone
Demo\Domain\Model\Timezone\Timezone
Demo\Domain\Model\Service\ServiceInterface:
Demo\Domain\Model\Service\Service
44 changes: 44 additions & 0 deletions app/migrations/Version20240206172427.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Ivoz\Core\Infrastructure\Persistence\Doctrine\LoggableMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240206172427 extends LoggableMigration
{
public function getDescription(): string
{
return 'Add Services table';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE Services (id INT UNSIGNED AUTO_INCREMENT NOT NULL, iden VARCHAR(50) NOT NULL, UNIQUE INDEX services_iden (iden), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->populateFactoryServices();
}

private function populateFactoryServices()
{
$factoryServices = [
'Recording', 'Voicemail', 'Queues'
];

foreach ($factoryServices as $srv) {
$this->addSql('INSERT INTO Services(iden) VALUES(:srv)', ['srv' => $srv]);
}
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE Services');
}
}
31 changes: 31 additions & 0 deletions app/src/Demo/Domain/Model/Service/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Demo\Domain\Model\Service;

/**
* Service
*/
class Service extends ServiceAbstract implements ServiceInterface
{
use ServiceTrait;

const BUILTIN_SERVICES = ['Recording', 'Voicemail', 'Queues'];

/**
* @codeCoverageIgnore
* @return array<string, mixed>
*/
public function getChangeSet(): array
{
return parent::getChangeSet();
}

/**
* Get id
* @codeCoverageIgnore
*/
public function getId(): null|int
{
return $this->id;
}
}
161 changes: 161 additions & 0 deletions app/src/Demo/Domain/Model/Service/ServiceAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

declare(strict_types=1);

namespace Demo\Domain\Model\Service;

use Assert\Assertion;
use Ivoz\Core\Domain\DataTransferObjectInterface;
use Ivoz\Core\Domain\Model\ChangelogTrait;
use Ivoz\Core\Domain\Model\EntityInterface;
use Ivoz\Core\Domain\ForeignKeyTransformerInterface;

/**
* ServiceAbstract
* @codeCoverageIgnore
*/
abstract class ServiceAbstract
{
use ChangelogTrait;

/**
* @var string
*/
protected $iden;

/**
* Constructor
*/
protected function __construct(
string $iden
) {
$this->setIden($iden);
}

abstract public function getId(): null|string|int;

public function __toString(): string
{
return sprintf(
"%s#%s",
"Service",
(string) $this->getId()
);
}

/**
* @throws \Exception
*/
protected function sanitizeValues(): void
{
}

/**
* @param int | null $id
*/
public static function createDto($id = null): ServiceDto
{
return new ServiceDto($id);
}

/**
* @internal use EntityTools instead
* @param null|ServiceInterface $entity
*/
public static function entityToDto(?EntityInterface $entity, int $depth = 0): ?ServiceDto
{
if (!$entity) {
return null;
}

Assertion::isInstanceOf($entity, ServiceInterface::class);

if ($depth < 1) {
return static::createDto($entity->getId());
}

if ($entity instanceof \Doctrine\ORM\Proxy\Proxy && !$entity->__isInitialized()) {
return static::createDto($entity->getId());
}

$dto = $entity->toDto($depth - 1);

return $dto;
}

/**
* Factory method
* @internal use EntityTools instead
* @param ServiceDto $dto
*/
public static function fromDto(
DataTransferObjectInterface $dto,
ForeignKeyTransformerInterface $fkTransformer
): static {
Assertion::isInstanceOf($dto, ServiceDto::class);
$iden = $dto->getIden();
Assertion::notNull($iden, 'getIden value is null, but non null value was expected.');

$self = new static(
$iden
);

;

$self->initChangelog();

return $self;
}

/**
* @internal use EntityTools instead
* @param ServiceDto $dto
*/
public function updateFromDto(
DataTransferObjectInterface $dto,
ForeignKeyTransformerInterface $fkTransformer
): static {
Assertion::isInstanceOf($dto, ServiceDto::class);

$iden = $dto->getIden();
Assertion::notNull($iden, 'getIden value is null, but non null value was expected.');

$this
->setIden($iden);

return $this;
}

/**
* @internal use EntityTools instead
*/
public function toDto(int $depth = 0): ServiceDto
{
return self::createDto()
->setIden(self::getIden());
}

/**
* @return array<string, mixed>
*/
protected function __toArray(): array
{
return [
'iden' => self::getIden()
];
}

protected function setIden(string $iden): static
{
Assertion::maxLength($iden, 50, 'iden value "%s" is too long, it should have no more than %d characters, but has %d characters.');

$this->iden = $iden;

return $this;
}

public function getIden(): string
{
return $this->iden;
}
}
7 changes: 7 additions & 0 deletions app/src/Demo/Domain/Model/Service/ServiceDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Demo\Domain\Model\Service;

class ServiceDto extends ServiceDtoAbstract
{
}
96 changes: 96 additions & 0 deletions app/src/Demo/Domain/Model/Service/ServiceDtoAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Demo\Domain\Model\Service;

use Ivoz\Core\Domain\DataTransferObjectInterface;
use Ivoz\Core\Domain\Model\DtoNormalizer;

/**
* ServiceDtoAbstract
* @codeCoverageIgnore
*/
abstract class ServiceDtoAbstract implements DataTransferObjectInterface
{
use DtoNormalizer;

/**
* @var string|null
*/
private $iden = null;

/**
* @var int|null
*/
private $id = null;

public function __construct(?int $id = null)
{
$this->setId($id);
}

/**
* @inheritdoc
*/
public static function getPropertyMap(string $context = '', string $role = null): array
{
if ($context === self::CONTEXT_COLLECTION) {
return ['id' => 'id'];
}

return [
'iden' => 'iden',
'id' => 'id'
];
}

/**
* @return array<string, mixed>
*/
public function toArray(bool $hideSensitiveData = false): array
{
$response = [
'iden' => $this->getIden(),
'id' => $this->getId()
];

if (!$hideSensitiveData) {
return $response;
}

foreach ($this->sensitiveFields as $sensitiveField) {
if (!array_key_exists($sensitiveField, $response)) {
throw new \Exception($sensitiveField . ' field was not found');
}
$response[$sensitiveField] = '*****';
}

return $response;
}

public function setIden(string $iden): static
{
$this->iden = $iden;

return $this;
}

public function getIden(): ?string
{
return $this->iden;
}

/**
* @param int|null $id
*/
public function setId($id): static
{
$this->id = $id;

return $this;
}

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

0 comments on commit b736ead

Please sign in to comment.