src/Entity/Client.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Repository\ClientRepository;
  5. use App\Entity\Model\ClientConfiguration;
  6. use ApiPlatform\Core\Annotation\ApiFilter;
  7. use App\Entity\Traits\TimestampableEntity;
  8. use Doctrine\Common\Collections\Collection;
  9. use ApiPlatform\Core\Annotation\ApiProperty;
  10. use ApiPlatform\Core\Annotation\ApiResource;
  11. use App\Entity\Model\ClientConfigurationType;
  12. use App\Validator\Constraints as CustomAssert;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  17. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  18. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  19. /**
  20.  * @ApiResource(
  21.  *      attributes={"security"="is_granted('ROLE_ADMIN')"},
  22.  *      normalizationContext={"groups"={"Client:Read"}, "skip_null_values"=false},
  23.  *      denormalizationContext={"groups"={"Client:Write"}},
  24.  *      collectionOperations={
  25.  *          "get"={
  26.  *              "normalization_context"={"groups"={"Client:Read-List"}, "skip_null_values"=false}
  27.  *          },
  28.  *          "get_my_client"={
  29.  *              "route_name"="api_pub_clients_my_client_collection",
  30.  *              "method"="GET",
  31.  *              "security"="is_granted('PUBLIC_ACCESS')",
  32.  *              "normalization_context"={"groups"={"Client:MyClient"}, "skip_null_values"=false}
  33.  *          },
  34.  *          "post"
  35.  *      },
  36.  *      itemOperations={
  37.  *          "get"={
  38.  *              "normalization_context"={"groups"={"Client:Read", "Client:ReadSecure"}, "skip_null_values"=false}
  39.  *          },
  40.  *          "get_secure"={
  41.  *              "path"="/clients/{id}/secure",
  42.  *              "method"="GET",
  43.  *              "security"="is_granted('ROLE_SUPER_ADMIN')",
  44.  *              "normalization_context"={"groups"={"Client:Read", "Client:ReadSecure"}, "skip_null_values"=false}
  45.  *          },
  46.  *          "put",
  47.  *          "patch",
  48.  *          "delete"
  49.  *      }
  50.  * )
  51.  * @ApiFilter(SearchFilter::class, properties={"name": "partial"})
  52.  * @ApiFilter(OrderFilter::class, properties={"id", "name": "ASC"})
  53.  * @ORM\Entity(repositoryClass=ClientRepository::class)
  54.  * @UniqueEntity(fields={"name"})
  55.  */
  56. class Client
  57. {
  58.     /**
  59.      * Hook timestampable behavior
  60.      * updates createdAt, updatedAt fields
  61.      */
  62.     use TimestampableEntity;
  63.     /**
  64.      * @ORM\Id
  65.      * @ORM\GeneratedValue
  66.      * @ORM\Column(type="integer")
  67.      * @Groups({"Client:Read", "Client:Read-List", "Container:Read", "Client:MyClient", "Container:PRead", "User:PRead"})
  68.      */
  69.     private $id;
  70.     /**
  71.      * @ORM\Column(type="string", length=255, unique=true)
  72.      * @Groups({"Client:Read", "Client:Read-List", "Client:Write", "Container:Read", "Client:MyClient", "Container:PRead", "User:PRead"})
  73.      *
  74.      * @Assert\NotBlank(message="validation.client:name.notBlank")
  75.      * @Assert\Length(max=255, maxMessage="validation.client:name.max")
  76.      */
  77.     private $name;
  78.     /**
  79.      * @ORM\Column(type="text", nullable=true)
  80.      * @Groups({"Client:Read", "Client:Read-List", "Client:Write"})
  81.      */
  82.     private $notes;
  83.     /**
  84.      * @ORM\OneToMany(targetEntity=Container::class, mappedBy="client", orphanRemoval=true)
  85.      * @Groups({"Client:Read", "Client:MyClient"})
  86.      */
  87.     private $containers;
  88.     /**
  89.      * @ORM\Column(type="string", length=16)
  90.      * @ApiProperty(
  91.      *     attributes={
  92.      *         "openapi_context"={
  93.      *             "type"="string",
  94.      *             "enum"={self::STORAGE_LOCAL, self::STORAGE_S3}
  95.      *         }
  96.      *     }
  97.      * )
  98.      * @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
  99.      *
  100.      * @Assert\NotBlank(message="validation.client:storage.notBlank")
  101.      * @Assert\Choice({self::STORAGE_LOCAL, self::STORAGE_S3})
  102.      */
  103.     private $storage self::STORAGE_LOCAL;
  104.     public const STORAGE_LOCAL "Local";
  105.     public const STORAGE_S3 "S3";
  106.     /**
  107.      * @ORM\Column(type="string", length=255, nullable=true)
  108.      * @Groups({"Client:ReadSecure", "Client:Write"})
  109.      *
  110.      * @Assert\Length(max=255, maxMessage="validation.client:bucketKey.max")
  111.      * @CustomAssert\CheckPropertyValues(field = "getStorage", constant ={self::STORAGE_S3}, constraints = {
  112.      *      @Assert\NotBlank(message="validation.client:bucketKey.notBlank")
  113.      * })
  114.      */
  115.     private $bucketKey;
  116.     /**
  117.      * @ORM\Column(type="string", length=255, nullable=true)
  118.      * @Groups({"Client:ReadSecure", "Client:Write"})
  119.      *
  120.      * @Assert\Length(max=255, maxMessage="validation.client:bucketSecret.max")
  121.      * @CustomAssert\CheckPropertyValues(field = "getStorage", constant ={self::STORAGE_S3}, constraints = {
  122.      *      @Assert\NotBlank(message="validation.client:bucketSecret.notBlank")
  123.      * })
  124.      */
  125.     private $bucketSecret;
  126.     /**
  127.      * @ORM\Column(type="string", length=255, nullable=true)
  128.      * @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
  129.      *
  130.      * @Assert\Length(max=255, maxMessage="validation.client:bucketName.max")
  131.      * @CustomAssert\CheckPropertyValues(field = "getStorage", constant ={self::STORAGE_S3}, constraints = {
  132.      *      @Assert\NotBlank(message="validation.client:bucketName.notBlank")
  133.      * })
  134.      */
  135.     private $bucketName;
  136.     /**
  137.      * @ORM\Column(type="string", length=64, nullable=true)
  138.      * @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
  139.      *
  140.      * @Assert\Length(max=64, maxMessage="validation.client:bucketRegion.max")
  141.      * @CustomAssert\CheckPropertyValues(field = "getStorage", constant ={self::STORAGE_S3}, constraints = {
  142.      *      @Assert\NotBlank(message="validation.client:bucketRegion.notBlank")
  143.      * })
  144.      */
  145.     private $bucketRegion;
  146.     /**
  147.      * @ORM\Column(type="string", length=255, nullable=true)
  148.      * @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
  149.      *
  150.      * @Assert\Length(max=255, maxMessage="validation.client:bucketEndpoint.max")
  151.      */
  152.     private $bucketEndpoint;
  153.     /**
  154.      * @ORM\Column(type="json", nullable=true)
  155.      * @Groups({"Client:Read", "Client:Write", "Container:Read"})
  156.      *
  157.      * @Assert\Type(type="array", message="validation.client:configuration.type")
  158.      */
  159.     private $configuration = [];
  160.     /**
  161.      * @ORM\Column(type="string", length=128, nullable=true)
  162.      * @Groups({"Client:Read", "Client:Read-List", "Client:Write", "Container:Read", "Client:MyClient", "User:PRead", "Container:PRead"})
  163.      *
  164.      * @Assert\Length(max=128, maxMessage="validation.client:domain.max")
  165.      */
  166.     private $domain;
  167.     /**
  168.      * @ORM\Column(type="boolean", nullable=true)
  169.      * @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient"})
  170.      */
  171.     private $isShowPublic;
  172.     /**
  173.      * @ORM\Column(type="boolean", nullable=true)
  174.      * @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient"})
  175.      */
  176.     private $isCommunitySharingEnable;
  177.     /**
  178.      * @ORM\Column(type="decimal", precision=5, scale=2, nullable=true)
  179.      * @Groups({"Client:Read", "Client:Write", "Container:Read", "Client:MyClient"})
  180.      */
  181.     private $courseCommission;
  182.     /**
  183.      * @ORM\OneToMany(targetEntity=Transaction::class, mappedBy="client", orphanRemoval=true)
  184.      */
  185.     private $transactions;
  186.     /**
  187.      * @ORM\Column(type="array", nullable=true)
  188.      * @Groups({"Client:Read", "Client:Write", "Client:MyClient"})
  189.      */
  190.     private $syncContainers = [];
  191.     /**
  192.      * @ORM\Column(type="boolean", nullable=true)
  193.      * @Groups({"Client:Write"})
  194.      */
  195.     private $isSyncContainerChanged false;
  196.     public function __construct()
  197.     {
  198.         $configurationModel = new ClientConfiguration();
  199.         $this->configuration $configurationModel->getProperties();
  200.         $this->containers = new ArrayCollection();
  201.         $this->transactions = new ArrayCollection();
  202.     }
  203.     public function getId(): ?int
  204.     {
  205.         return $this->id;
  206.     }
  207.     public function getName(): ?string
  208.     {
  209.         return $this->name;
  210.     }
  211.     public function setName(string $name): self
  212.     {
  213.         $this->name $name;
  214.         return $this;
  215.     }
  216.     public function getNotes(): ?string
  217.     {
  218.         return $this->notes;
  219.     }
  220.     public function setNotes(?string $notes): self
  221.     {
  222.         $this->notes $notes;
  223.         return $this;
  224.     }
  225.     public function getContainers()
  226.     {
  227.         return $this->containers;
  228.     }
  229.     public function addContainer(Container $container): self
  230.     {
  231.         if (!$this->containers->contains($container)) {
  232.             $this->containers[] = $container;
  233.             $container->setClient($this);
  234.         }
  235.         return $this;
  236.     }
  237.     public function removeContainer(Container $container): self
  238.     {
  239.         if ($this->containers->removeElement($container)) {
  240.             // set the owning side to null (unless already changed)
  241.             if ($container->getClient() === $this) {
  242.                 $container->setClient(null);
  243.             }
  244.         }
  245.         return $this;
  246.     }
  247.     public function getStorage(): ?string
  248.     {
  249.         return $this->storage;
  250.     }
  251.     public function setStorage(string $storage): self
  252.     {
  253.         $this->storage $storage;
  254.         return $this;
  255.     }
  256.     public function getBucketKey(): ?string
  257.     {
  258.         return $this->bucketKey;
  259.     }
  260.     public function setBucketKey(?string $bucketKey): self
  261.     {
  262.         $this->bucketKey $bucketKey;
  263.         return $this;
  264.     }
  265.     public function getBucketSecret(): ?string
  266.     {
  267.         return $this->bucketSecret;
  268.     }
  269.     public function setBucketSecret(?string $bucketSecret): self
  270.     {
  271.         $this->bucketSecret $bucketSecret;
  272.         return $this;
  273.     }
  274.     public function getBucketName(): ?string
  275.     {
  276.         return $this->bucketName;
  277.     }
  278.     public function setBucketName(?string $bucketName): self
  279.     {
  280.         $this->bucketName $bucketName;
  281.         return $this;
  282.     }
  283.     public function getBucketRegion(): ?string
  284.     {
  285.         return $this->bucketRegion;
  286.     }
  287.     public function setBucketRegion(?string $bucketRegion): self
  288.     {
  289.         $this->bucketRegion $bucketRegion;
  290.         return $this;
  291.     }
  292.     public function getBucketEndpoint(): ?string
  293.     {
  294.         return $this->bucketEndpoint;
  295.     }
  296.     public function setBucketEndpoint(?string $bucketEndpoint): self
  297.     {
  298.         $this->bucketEndpoint $bucketEndpoint;
  299.         return $this;
  300.     }
  301.     public function getConfiguration($key null)
  302.     {
  303.         if (!empty($key)) {
  304.             return ($this->hasConfiguration($key)) ? $this->configuration[$key] : null;
  305.         }
  306.         return (empty($this->configuration)) ? [] : $this->configuration;
  307.     }
  308.     public function setConfiguration(?array $configuration): self
  309.     {
  310.         if (!is_array($configuration)) {
  311.             return $this;
  312.         }
  313.         $configurationModel = new ClientConfiguration();
  314.         $this->configuration array_merge($configurationModel->getProperties(), $this->getConfiguration(), $configuration);
  315.         return $this;
  316.     }
  317.     public function hasConfiguration($key): bool
  318.     {
  319.         if (isset($this->configuration[$key])) {
  320.             return true;
  321.         }
  322.         return false;
  323.     }
  324.     public function replaceConfiguration(?array $configuration)
  325.     {
  326.         $this->configuration $configuration;
  327.     }
  328.     /**
  329.     * @Groups({"Client:Read", "Container:Read"})
  330.     */
  331.     public function getConfigurationTypes(): ?array
  332.     {
  333.         return ClientConfigurationType::CONFIGURATION_TYPES;
  334.     }
  335.     public function getDomain(): ?string
  336.     {
  337.         return $this->domain;
  338.     }
  339.     public function setDomain(?string $domain): self
  340.     {
  341.         $this->domain $domain;
  342.         return $this;
  343.     }
  344.     public function getVhost()
  345.     {
  346.         if (empty($this->getDomain())) {
  347.             return false;
  348.         }
  349.         return <<<VHOST
  350.         server {
  351.             server_name {$this->getDomain()};
  352.             root /home/srm/web/project/expertshare-public/build;
  353.             index index.html index.htm;
  354.             location / {
  355.                 try_files \$uri \$uri/ /index.html;
  356.             }
  357.         }
  358.         VHOST;
  359.     }
  360.     public function getIsShowPublic(): ?bool
  361.     {
  362.         return $this->isShowPublic;
  363.     }
  364.     public function setIsShowPublic(?bool $isShowPublic): self
  365.     {
  366.         $this->isShowPublic $isShowPublic;
  367.         return $this;
  368.     }
  369.     public function getIsCommunitySharingEnable(): ?bool
  370.     {
  371.         return $this->isCommunitySharingEnable;
  372.     }
  373.     public function setIsCommunitySharingEnable(?bool $isCommunitySharingEnable): self
  374.     {
  375.         $this->isCommunitySharingEnable $isCommunitySharingEnable;
  376.         return $this;
  377.     }
  378.     public function getCourseCommission(): ?string
  379.     {
  380.         return $this->courseCommission;
  381.     }
  382.     public function setCourseCommission(?string $courseCommission): self
  383.     {
  384.         $this->courseCommission $courseCommission;
  385.         return $this;
  386.     }
  387.     /**
  388.      * @return Collection<int, Transaction>
  389.      */
  390.     public function getTransactions(): Collection
  391.     {
  392.         return $this->transactions;
  393.     }
  394.     public function addTransaction(Transaction $transaction): self
  395.     {
  396.         if (!$this->transactions->contains($transaction)) {
  397.             $this->transactions[] = $transaction;
  398.             $transaction->setClient($this);
  399.         }
  400.         return $this;
  401.     }
  402.     public function removeTransaction(Transaction $transaction): self
  403.     {
  404.         if ($this->transactions->removeElement($transaction)) {
  405.             // set the owning side to null (unless already changed)
  406.             if ($transaction->getClient() === $this) {
  407.                 $transaction->setClient(null);
  408.             }
  409.         }
  410.         return $this;
  411.     }
  412.     public function getSyncContainers(): ?array
  413.     {
  414.         return $this->syncContainers;
  415.     }
  416.     public function setSyncContainers(?array $syncContainers): self
  417.     {
  418.         $this->syncContainers $syncContainers;
  419.         return $this;
  420.     }
  421.     public function getIsSyncContainerChanged()
  422.     {
  423.         return $this->isSyncContainerChanged;
  424.     }
  425.     public function setIsSyncContainerChanged($isSyncContainerChanged)
  426.     {
  427.         $this->isSyncContainerChanged $isSyncContainerChanged;
  428.         return $this;
  429.     }
  430. }