src/Entity/Configuration.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\Get;
  5. use ApiPlatform\Metadata\Post;
  6. use ApiPlatform\Metadata\Put;
  7. use App\Controller\Api\ConfigurationCompleteController;
  8. use App\Controller\Api\ConfigurationExportController;
  9. use App\Controller\Api\ConfigurationProductsController;
  10. use App\Controller\Api\ConfigurationQuotationController;
  11. use App\Model\EntityTime;
  12. use App\Repository\ConfigurationRepository;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. #[ORM\Entity(repositoryClassConfigurationRepository::class)]
  18. #[ApiResource(
  19.     operations: [
  20.         new Get(),
  21.         new Get('/configurations/{id}/products'controllerConfigurationProductsController::class, normalizationContext: [
  22.             'groups' => ['identifier:read''product:read''product-option:read']
  23.         ]),
  24.         new Get('/configurations/{id}/export'controllerConfigurationExportController::class),
  25.         new Get('/configurations/{id}/quotation'controllerConfigurationQuotationController::class),
  26.         new Post('/configurations/{id}/complete'controllerConfigurationCompleteController::class),
  27.         new Put()
  28.     ],
  29.     normalizationContext: ['groups' => ['identifier:read''configuration:read''configuration-item:read''prospect:read']],
  30.     denormalizationContext: ['groups' => ['configuration:write''configuration-item:write']]
  31. )]
  32. class Configuration extends EntityTime
  33. {
  34.     #[ORM\OneToMany(
  35.         mappedBy'configuration',
  36.         targetEntityConfigurationItem::class,
  37.         orphanRemovaltrue,
  38.         cascade: ['persist''remove']
  39.     )]
  40.     #[Groups(['configuration:read'])]
  41.     private Collection $items;
  42.     #[ORM\OneToOne(inversedBy'configuration'cascade: ['persist''remove'])]
  43.     #[Groups(['configuration:read'])]
  44.     private ?Prospect $prospect null;
  45.     #[ORM\ManyToOne]
  46.     #[Groups(['configuration:read''configuration:write'])]
  47.     private ?ConfigurationType $type null;
  48.     #[ORM\Column(nullabletrue)]
  49.     private ?int $notifyLevel null;
  50.     public function __construct()
  51.     {
  52.         parent::__construct();
  53.         $this->items = new ArrayCollection();
  54.     }
  55.     /**
  56.      * @return Collection<int, ConfigurationItem>
  57.      */
  58.     public function getItems(): Collection
  59.     {
  60.         return $this->items;
  61.     }
  62.     public function addItem(ConfigurationItem $item): self
  63.     {
  64.         if (!$this->items->contains($item)) {
  65.             $this->items->add($item);
  66.             $item->setConfiguration($this);
  67.         }
  68.         return $this;
  69.     }
  70.     public function removeItem(ConfigurationItem $item): self
  71.     {
  72.         if ($this->items->removeElement($item)) {
  73.             // set the owning side to null (unless already changed)
  74.             if ($item->getConfiguration() === $this) {
  75.                 $item->setConfiguration(null);
  76.             }
  77.         }
  78.         return $this;
  79.     }
  80.     public function getProspect(): ?Prospect
  81.     {
  82.         return $this->prospect;
  83.     }
  84.     public function setProspect(?Prospect $prospect): self
  85.     {
  86.         $this->prospect $prospect;
  87.         return $this;
  88.     }
  89.     public function getData(): array
  90.     {
  91.         $data = [];
  92.         /** @var ConfigurationItem $item */
  93.         foreach ($this->getItems() as $item) {
  94.             $data[$item->getName()] = $item->getData();
  95.         }
  96.         return $data;
  97.     }
  98.     public function getType(): ?ConfigurationType
  99.     {
  100.         return $this->type;
  101.     }
  102.     public function setType(?ConfigurationType $type): self
  103.     {
  104.         $this->type $type;
  105.         return $this;
  106.     }
  107.     public function getNotifyLevel(): ?int
  108.     {
  109.         return $this->notifyLevel;
  110.     }
  111.     public function setNotifyLevel(?int $notifyLevel): self
  112.     {
  113.         $this->notifyLevel $notifyLevel;
  114.         return $this;
  115.     }
  116.     /**
  117.      * @return Collection|ConfigurationItem[]
  118.      */
  119.     public function getConfigurators(): Collection
  120.     {
  121.         return $this->items->filter(function (ConfigurationItem $item) {
  122.             if (explode('_'$item->getName())[0] == 'configurator') {
  123.                 return $item;
  124.             }
  125.             return false;
  126.         });
  127.     }
  128.     public function getChronoItem(): ?ConfigurationItemValue
  129.     {
  130.         return $this->getItems()->filter(
  131.             fn(ConfigurationItem $item) => $item->getName() == "chrono_number"
  132.         )->first();
  133.     }
  134.     public function getGrcItem(): ?ConfigurationItemValue
  135.     {
  136.         return $this->getItems()->filter(
  137.             fn(ConfigurationItem $item) => $item->getName() == "grc"
  138.         )->first();
  139.     }
  140.     public function getCompleteItem(): ?ConfigurationItemValue
  141.     {
  142.         return $this->getItems()->filter(
  143.             fn(ConfigurationItem $item) => $item->getName() == "complete"
  144.         )->first();
  145.     }
  146. }