<?php
namespace App\Entity\Channel;
use App\Entity\Common\Contact;
use App\Entity\Sync\SyncableInterface;
use App\Repository\Channel\CompanyContactRepository;
use Doctrine\ORM\Mapping as ORM;
use Drosalys\Bundle\ApiBundle\Serializer\Attributes\IdGroups;
use Nellapp\Bundle\SDKBundle\Channel\Entity\CompanyContact\CompanyContactInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\CompanyContact\CompanyContactMethodTrait;
use Nellapp\Bundle\SDKBundle\Enum\CompanyContact\CompanyContactBehaviorEnum;
use Nellapp\Bundle\SDKBundle\Sync\EntityKeys;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation as Serial;
use Symfony\Component\Validator\Constraints as Assert;
#[
ORM\Entity(repositoryClass: CompanyContactRepository::class),
ORM\Table(name: 'channel_company_contacts'),
UniqueEntity(fields: ['channelContact', 'company'], message: 'channel.company_contact.UniqueEntity')
]
class CompanyContact implements CompanyContactInterface, SyncableInterface
{
use CompanyContactMethodTrait;
#[
ORM\Id,
ORM\GeneratedValue(strategy: 'NONE'),
ORM\Column(type: 'string', length: 36)
]
#[Serial\Groups(['Sync', 'Manager'])]
private ?string $id = null;
#[ORM\Column(type: 'json')]
#[Serial\Groups(['Sync'])]
private array $behaviors = [];
#[ORM\ManyToOne(targetEntity: Company::class, inversedBy: 'companyContacts')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
#[IdGroups(['Sync'])]
private Company $company;
#[ORM\ManyToOne(targetEntity: ChannelContact::class, cascade: ['persist'], inversedBy: 'companyContacts')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
#[
Assert\NotBlank(),
Assert\Valid(),
]
#[IdGroups(['Sync'])]
private ChannelContact $channelContact;
public function __construct()
{
$this->initCompanyContactMethod();
}
public static function getSyncKey(): string
{
return EntityKeys::CHANNEL_COMPANY_CONTACT;
}
public function getSyncDisplayLabel(): string
{
return 'Contact entreprise : ' . $this->getContact()->__toString();
}
public function toggleRepresentative(): static
{
if ($this->isRepresentative()) {
$this->setRepresentative(false);
return $this;
}
$this->setRepresentative(true);
return $this;
}
public function setRepresentative(bool $representative): static
{
if ($representative) {
$this->setBehaviors(array_merge($this->getBehaviors(), [CompanyContactBehaviorEnum::REPRESENTATIVE]));
return $this;
}
$this->setBehaviors(array_filter($this->behaviors, function (string $behavior): bool {
return $behavior !== CompanyContactBehaviorEnum::REPRESENTATIVE;
}));
return $this;
}
}