<?php
namespace App\Entity\Channel;
use App\Entity\Account\User;
use App\Entity\Sync\SyncableInterface;
use App\Repository\Channel\GroupRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Drosalys\Bundle\ApiBundle\Serializer\Attributes\IdGroups;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampInterface;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampTrait;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Group\GroupInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Group\GroupMethodTrait;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Place\PlaceInterface;
use Nellapp\Bundle\SDKBundle\Sync\EntityKeys;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Component\Serializer\Annotation as Serial;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: GroupRepository::class), ORM\Table(name: 'channel_groups')]
class Group implements SyncableInterface, GroupInterface, TimestampInterface
{
use GroupMethodTrait;
use TimestampTrait;
#[ORM\Id, ORM\GeneratedValue(strategy: 'CUSTOM'), ORM\CustomIdGenerator(class: UuidGenerator::class), ORM\Column(type: 'string', length: 36)]
#[Serial\Groups(['Manager', 'Sync'])]
private ?string $id = null;
#[ORM\Column(type: 'string', length: 255)]
#[Serial\Groups(['Manager', 'Sync', 'GroupPostManager'])]
#[
Assert\NotBlank(message: 'channel.group.label.NotBlank', groups: ['Default', 'flow_sessionCreate_step1']),
Assert\Length(
min: 2,
max: 255,
minMessage: 'channel.group.label.Length.min',
maxMessage: 'channel.group.label.Length.max',
groups: ['Default', 'flow_sessionCreate_step1']
)
]
private ?string $label = null;
#[ORM\ManyToOne(targetEntity: Place::class)]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
#[Assert\NotBlank(groups: ['Default', 'flow_sessionCreate_step1'])]
#[IdGroups(groups: 'Sync')]
private ?Place $place = null;
#[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'groups', cascade: ['persist']), ORM\JoinTable(name: 'channel_group_users')]
#[IdGroups(['Sync'])]
private ?Collection $users;
#[ORM\ManyToOne(targetEntity: Channel::class), ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
#[IdGroups(['Sync'])]
private ?Channel $channel;
#[ORM\OneToOne(mappedBy: 'group', targetEntity: Session::class, cascade: ['persist', 'remove'])]
private ?Session $session = null;
public static function getSyncKey(): string
{
return EntityKeys::CHANNEL_GROUP;
}
public function getSyncDisplayLabel(): string
{
return (string) $this->getLabel();
}
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* @return int[]
*/
public function getUserIds(): array
{
return array_map(function (User $user) {
return $user->getId();
}, $this->getUsers()->toArray());
}
public function getSession(): ?Session
{
return $this->session;
}
public function setSession(?Session $session): static
{
$this->session = $session;
return $this;
}
public function getPlace(): ?Place
{
return $this->place;
}
public function setPlace(?PlaceInterface $place): static
{
$this->place = $place;
return $this;
}
}