<?php
namespace App\Entity\Channel;
use App\Entity\Account\User;
use App\Entity\Sync\SyncableInterface;
use App\Repository\Channel\ServiceRepository;
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\Auth\Entity\UserInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\ChannelInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Service\ServiceInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Service\ServiceMethodTrait;
use Nellapp\Bundle\SDKBundle\Permission\Entity\ChannelResourceInterface;
use Nellapp\Bundle\SDKBundle\Permission\UsersResource\UsersResourceInterface;
use Nellapp\Bundle\SDKBundle\Sync\EntityKeys;
use Symfony\Component\Serializer\Annotation as Serial;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: ServiceRepository::class)]
#[ORM\Table(name: 'channel_services')]
class Service implements TimestampInterface, SyncableInterface, ServiceInterface, ChannelResourceInterface, UsersResourceInterface
{
use TimestampTrait;
use ServiceMethodTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Serial\Groups(['Sync', 'Manager'])]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 255)]
#[
Assert\NotBlank(message: 'entity.channel.service.name.NotBlank'),
Assert\Length(
min: 2,
max: 255,
minMessage: 'entity.channel.service.name.min',
maxMessage: 'entity.channel.service.name.max',
)
]
#[Serial\Groups(['Sync', 'Manager'])]
private ?string $label = null;
#[
ORM\ManyToMany(targetEntity: User::class, inversedBy: 'services', cascade: ['persist']),
ORM\JoinTable('channel_service_users')
]
#[IdGroups(['Sync'])]
private Collection $users;
#[
ORM\ManyToOne(targetEntity: Channel::class),
ORM\JoinColumn(nullable: false, onDelete: 'CASCADE'),
]
#[IdGroups(['Sync'])]
private ?Channel $channel = null;
#[ORM\Column(type: 'boolean', nullable: false)]
#[Serial\Groups(['Sync'])]
private ?bool $enableOnNim = null;
public function __construct()
{
$this->users = new ArrayCollection();
}
public static function getSyncKey(): string
{
return EntityKeys::CHANNEL_SERVICE;
}
public function getSyncDisplayLabel(): string
{
return (string) $this->getLabel();
}
public static function getPermissionResourceClass(): ?string
{
return ChannelUserServicePermission::class;
}
public function getOwnerChannel(): ?ChannelInterface
{
return $this->getChannel();
}
public function __toString(): string
{
return $this->getLabel();
}
public function hasUser(?UserInterface $user, array $tags = []): bool
{
if (!$user instanceof User) {
return false;
}
return $this->getUsers()->contains($user);
}
}