<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\ClientRepository;
use App\Entity\Model\ClientConfiguration;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Traits\TimestampableEntity;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Model\ClientConfigurationType;
use App\Validator\Constraints as CustomAssert;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ApiResource(
* attributes={"security"="is_granted('ROLE_ADMIN')"},
* normalizationContext={"groups"={"Client:Read"}, "skip_null_values"=false},
* denormalizationContext={"groups"={"Client:Write"}},
* collectionOperations={
* "get"={
* "normalization_context"={"groups"={"Client:Read-List"}, "skip_null_values"=false}
* },
* "get_my_client"={
* "route_name"="api_pub_clients_my_client_collection",
* "method"="GET",
* "security"="is_granted('PUBLIC_ACCESS')",
* "normalization_context"={"groups"={"Client:MyClient"}, "skip_null_values"=false}
* },
* "post"
* },
* itemOperations={
* "get"={
* "normalization_context"={"groups"={"Client:Read", "Client:ReadSecure"}, "skip_null_values"=false}
* },
* "get_secure"={
* "path"="/clients/{id}/secure",
* "method"="GET",
* "security"="is_granted('ROLE_SUPER_ADMIN')",
* "normalization_context"={"groups"={"Client:Read", "Client:ReadSecure"}, "skip_null_values"=false}
* },
* "put",
* "patch",
* "delete"
* }
* )
* @ApiFilter(SearchFilter::class, properties={"name": "partial"})
* @ApiFilter(OrderFilter::class, properties={"id", "name": "ASC"})
* @ORM\Entity(repositoryClass=ClientRepository::class)
* @UniqueEntity(fields={"name"})
*/
class Client
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"Client:Read", "Client:Read-List", "Container:Read", "Client:MyClient", "Container:PRead", "User:PRead"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Groups({"Client:Read", "Client:Read-List", "Client:Write", "Container:Read", "Client:MyClient", "Container:PRead", "User:PRead"})
*
* @Assert\NotBlank(message="validation.client:name.notBlank")
* @Assert\Length(max=255, maxMessage="validation.client:name.max")
*/
private $name;
/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"Client:Read", "Client:Read-List", "Client:Write"})
*/
private $notes;
/**
* @ORM\OneToMany(targetEntity=Container::class, mappedBy="client", orphanRemoval=true)
* @Groups({"Client:Read", "Client:MyClient"})
*/
private $containers;
/**
* @ORM\Column(type="string", length=16)
* @ApiProperty(
* attributes={
* "openapi_context"={
* "type"="string",
* "enum"={self::STORAGE_LOCAL, self::STORAGE_S3}
* }
* }
* )
* @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
*
* @Assert\NotBlank(message="validation.client:storage.notBlank")
* @Assert\Choice({self::STORAGE_LOCAL, self::STORAGE_S3})
*/
private $storage = self::STORAGE_LOCAL;
public const STORAGE_LOCAL = "Local";
public const STORAGE_S3 = "S3";
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"Client:ReadSecure", "Client:Write"})
*
* @Assert\Length(max=255, maxMessage="validation.client:bucketKey.max")
* @CustomAssert\CheckPropertyValues(field = "getStorage", constant ={self::STORAGE_S3}, constraints = {
* @Assert\NotBlank(message="validation.client:bucketKey.notBlank")
* })
*/
private $bucketKey;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"Client:ReadSecure", "Client:Write"})
*
* @Assert\Length(max=255, maxMessage="validation.client:bucketSecret.max")
* @CustomAssert\CheckPropertyValues(field = "getStorage", constant ={self::STORAGE_S3}, constraints = {
* @Assert\NotBlank(message="validation.client:bucketSecret.notBlank")
* })
*/
private $bucketSecret;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
*
* @Assert\Length(max=255, maxMessage="validation.client:bucketName.max")
* @CustomAssert\CheckPropertyValues(field = "getStorage", constant ={self::STORAGE_S3}, constraints = {
* @Assert\NotBlank(message="validation.client:bucketName.notBlank")
* })
*/
private $bucketName;
/**
* @ORM\Column(type="string", length=64, nullable=true)
* @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
*
* @Assert\Length(max=64, maxMessage="validation.client:bucketRegion.max")
* @CustomAssert\CheckPropertyValues(field = "getStorage", constant ={self::STORAGE_S3}, constraints = {
* @Assert\NotBlank(message="validation.client:bucketRegion.notBlank")
* })
*/
private $bucketRegion;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
*
* @Assert\Length(max=255, maxMessage="validation.client:bucketEndpoint.max")
*/
private $bucketEndpoint;
/**
* @ORM\Column(type="json", nullable=true)
* @Groups({"Client:Read", "Client:Write", "Container:Read"})
*
* @Assert\Type(type="array", message="validation.client:configuration.type")
*/
private $configuration = [];
/**
* @ORM\Column(type="string", length=128, nullable=true)
* @Groups({"Client:Read", "Client:Read-List", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
*
* @Assert\Length(max=128, maxMessage="validation.client:domain.max")
*/
private $domain;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient"})
*/
private $isShowPublic;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient"})
*/
private $isCommunitySharingEnable;
/**
* @ORM\Column(type="decimal", precision=5, scale=2, nullable=true)
* @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient"})
*/
private $courseCommission;
/**
* @ORM\OneToMany(targetEntity=Transaction::class, mappedBy="client", orphanRemoval=true)
*/
private $transactions;
/**
* @ORM\Column(type="array", nullable=true)
* @Groups({"Client:Read", "Client:Write", "Client:MyClient"})
*/
private $syncContainers = [];
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Client:Write"})
*/
private $isSyncContainerChanged = false;
public function __construct()
{
$configurationModel = new ClientConfiguration();
$this->configuration = $configurationModel->getProperties();
$this->containers = new ArrayCollection();
$this->transactions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function setNotes(?string $notes): self
{
$this->notes = $notes;
return $this;
}
public function getContainers()
{
return $this->containers;
}
public function addContainer(Container $container): self
{
if (!$this->containers->contains($container)) {
$this->containers[] = $container;
$container->setClient($this);
}
return $this;
}
public function removeContainer(Container $container): self
{
if ($this->containers->removeElement($container)) {
// set the owning side to null (unless already changed)
if ($container->getClient() === $this) {
$container->setClient(null);
}
}
return $this;
}
public function getStorage(): ?string
{
return $this->storage;
}
public function setStorage(string $storage): self
{
$this->storage = $storage;
return $this;
}
public function getBucketKey(): ?string
{
return $this->bucketKey;
}
public function setBucketKey(?string $bucketKey): self
{
$this->bucketKey = $bucketKey;
return $this;
}
public function getBucketSecret(): ?string
{
return $this->bucketSecret;
}
public function setBucketSecret(?string $bucketSecret): self
{
$this->bucketSecret = $bucketSecret;
return $this;
}
public function getBucketName(): ?string
{
return $this->bucketName;
}
public function setBucketName(?string $bucketName): self
{
$this->bucketName = $bucketName;
return $this;
}
public function getBucketRegion(): ?string
{
return $this->bucketRegion;
}
public function setBucketRegion(?string $bucketRegion): self
{
$this->bucketRegion = $bucketRegion;
return $this;
}
public function getBucketEndpoint(): ?string
{
return $this->bucketEndpoint;
}
public function setBucketEndpoint(?string $bucketEndpoint): self
{
$this->bucketEndpoint = $bucketEndpoint;
return $this;
}
public function getConfiguration($key = null)
{
if (!empty($key)) {
return ($this->hasConfiguration($key)) ? $this->configuration[$key] : null;
}
return (empty($this->configuration)) ? [] : $this->configuration;
}
public function setConfiguration(?array $configuration): self
{
if (!is_array($configuration)) {
return $this;
}
$configurationModel = new ClientConfiguration();
$this->configuration = array_merge($configurationModel->getProperties(), $this->getConfiguration(), $configuration);
return $this;
}
public function hasConfiguration($key): bool
{
if (isset($this->configuration[$key])) {
return true;
}
return false;
}
public function replaceConfiguration(?array $configuration)
{
$this->configuration = $configuration;
}
/**
* @Groups({"Client:Read", "Container:Read"})
*/
public function getConfigurationTypes(): ?array
{
return ClientConfigurationType::CONFIGURATION_TYPES;
}
public function getDomain(): ?string
{
return $this->domain;
}
public function setDomain(?string $domain): self
{
$this->domain = $domain;
return $this;
}
public function getVhost()
{
if (empty($this->getDomain())) {
return false;
}
return <<<VHOST
server {
server_name {$this->getDomain()};
root /home/srm/web/project/expertshare-public/build;
index index.html index.htm;
location / {
try_files \$uri \$uri/ /index.html;
}
}
VHOST;
}
public function getIsShowPublic(): ?bool
{
return $this->isShowPublic;
}
public function setIsShowPublic(?bool $isShowPublic): self
{
$this->isShowPublic = $isShowPublic;
return $this;
}
public function getIsCommunitySharingEnable(): ?bool
{
return $this->isCommunitySharingEnable;
}
public function setIsCommunitySharingEnable(?bool $isCommunitySharingEnable): self
{
$this->isCommunitySharingEnable = $isCommunitySharingEnable;
return $this;
}
public function getCourseCommission(): ?string
{
return $this->courseCommission;
}
public function setCourseCommission(?string $courseCommission): self
{
$this->courseCommission = $courseCommission;
return $this;
}
/**
* @return Collection<int, Transaction>
*/
public function getTransactions(): Collection
{
return $this->transactions;
}
public function addTransaction(Transaction $transaction): self
{
if (!$this->transactions->contains($transaction)) {
$this->transactions[] = $transaction;
$transaction->setClient($this);
}
return $this;
}
public function removeTransaction(Transaction $transaction): self
{
if ($this->transactions->removeElement($transaction)) {
// set the owning side to null (unless already changed)
if ($transaction->getClient() === $this) {
$transaction->setClient(null);
}
}
return $this;
}
public function getSyncContainers(): ?array
{
return $this->syncContainers;
}
public function setSyncContainers(?array $syncContainers): self
{
$this->syncContainers = $syncContainers;
return $this;
}
public function getIsSyncContainerChanged()
{
return $this->isSyncContainerChanged;
}
public function setIsSyncContainerChanged($isSyncContainerChanged)
{
$this->isSyncContainerChanged = $isSyncContainerChanged;
return $this;
}
}