<?php
namespace App\Entity\Common;
use App\Entity\Account\User;
use App\Entity\Sync\SyncableInterface;
use App\Repository\Common\ContactRepository;
use Doctrine\ORM\Mapping as ORM;
use DrosalysWeb\ObjectExtensions\Blame\Model\BlameInterface;
use DrosalysWeb\ObjectExtensions\Blame\Model\BlameTrait;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampInterface;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampTrait;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Contact\ContactInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Contact\ContactMethodTrait;
use Nellapp\Bundle\SDKBundle\Entity\Common\Address\Address;
use Nellapp\Bundle\SDKBundle\Entity\Common\Address\AddressInterface;
use Nellapp\Bundle\SDKBundle\Sync\EntityKeys;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation as Serial;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert;
#[UniqueEntity(
fields: ['email'],
message: 'contact.email.UniqueEntity',
errorPath: 'email',
groups: ['check_unique'],
)]
#[
ORM\Entity(repositoryClass: ContactRepository::class),
ORM\UniqueConstraint(fields: ['email']),
ORM\Table(name: 'contacts'),
ORM\Index(fields: ['email'], flags: ['fulltext']),
ORM\Index(fields: ['firstName'], flags: ['fulltext']),
ORM\Index(fields: ['lastName'], flags: ['fulltext']),
]
class Contact implements TimestampInterface, BlameInterface, ContactInterface, SyncableInterface
{
use TimestampTrait;
use BlameTrait;
use ContactMethodTrait;
#[
ORM\Id,
ORM\GeneratedValue(strategy: 'NONE'),
ORM\Column(type: 'string', length: 36)
]
#[Serial\Groups(['Manager', 'Sync'])]
private ?string $id = null;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[Serial\Groups(['Manager', 'Sync'])]
#[
Assert\NotBlank(message: 'contact.email.NotBlank'),
Assert\Email(message: 'contact.email.Email', mode: 'strict'),
Assert\Length(
min: 6,
max: 64,
minMessage: 'contact.email.Length.min',
maxMessage: 'contact.email.Length.max',
)
]
private ?string $email = null;
#[ORM\Column(type: 'string', nullable: true)]
#[Serial\Groups(['Manager', 'Sync'])]
#[
Assert\Length(
min: 2,
max: 255,
minMessage: 'contact.firstName.Length.min',
maxMessage: 'contact.firstName.Length.max',
)
]
private ?string $firstName = null;
#[ORM\Column(type: 'string', nullable: true)]
#[Serial\Groups(['Manager', 'Sync'])]
#[
Assert\Length(
min: 2,
max: 255,
minMessage: 'contact.lastName.Length.min',
maxMessage: 'contact.lastName.Length.max',
)
]
private ?string $lastName = null;
#[ORM\Column(type: 'string', length: 20, nullable: true)]
#[Serial\Groups(['Manager', 'Sync'])]
#[
Assert\Length(
min: 8,
max: 20,
minMessage: 'contact.phoneNumber.Length.min',
maxMessage: 'contact.phoneNumber.Length.max',
)
]
private ?string $phoneNumber = null;
#[ORM\Embedded(class: Address::class)]
#[Assert\Valid()]
#[Serial\Groups(['Sync'])]
private ?AddressInterface $address = null;
#[ORM\OneToOne(mappedBy: 'contact', targetEntity: User::class, fetch: 'LAZY')]
private ?User $user = null;
public function __construct()
{
$this->initContactMethod();
}
protected function initContactMethod(): void
{
$this->id = Uuid::v4()->toRfc4122();
$this->address = new Address();
}
public static function getSyncKey(): string
{
return EntityKeys::CONTACT;
}
public function getSyncDisplayLabel(): string
{
return 'Contact ' . $this->__toString();
}
public function getFullName(): string
{
return trim(($this->getFirstName() ?: '') . ' ' . ($this->getLastName() ?: ''));
}
#[Serial\Groups(['Manager'])]
public function getFullNameWithEmail(): string
{
return trim(($this->getFullName() ?: '') . ' (' . $this->getEmail() . ')');
}
public function getUser(): ?User
{
return $this->user;
}
}