<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Controller\Api\ConfigurationCompleteController;
use App\Controller\Api\ConfigurationExportController;
use App\Controller\Api\ConfigurationProductsController;
use App\Controller\Api\ConfigurationQuotationController;
use App\Model\EntityTime;
use App\Repository\ConfigurationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: ConfigurationRepository::class)]
#[ApiResource(
operations: [
new Get(),
new Get('/configurations/{id}/products', controller: ConfigurationProductsController::class, normalizationContext: [
'groups' => ['identifier:read', 'product:read', 'product-option:read']
]),
new Get('/configurations/{id}/export', controller: ConfigurationExportController::class),
new Get('/configurations/{id}/quotation', controller: ConfigurationQuotationController::class),
new Post('/configurations/{id}/complete', controller: ConfigurationCompleteController::class),
new Put()
],
normalizationContext: ['groups' => ['identifier:read', 'configuration:read', 'configuration-item:read', 'prospect:read']],
denormalizationContext: ['groups' => ['configuration:write', 'configuration-item:write']]
)]
class Configuration extends EntityTime
{
#[ORM\OneToMany(
mappedBy: 'configuration',
targetEntity: ConfigurationItem::class,
orphanRemoval: true,
cascade: ['persist', 'remove']
)]
#[Groups(['configuration:read'])]
private Collection $items;
#[ORM\OneToOne(inversedBy: 'configuration', cascade: ['persist', 'remove'])]
#[Groups(['configuration:read'])]
private ?Prospect $prospect = null;
#[ORM\ManyToOne]
#[Groups(['configuration:read', 'configuration:write'])]
private ?ConfigurationType $type = null;
#[ORM\Column(nullable: true)]
private ?int $notifyLevel = null;
public function __construct()
{
parent::__construct();
$this->items = new ArrayCollection();
}
/**
* @return Collection<int, ConfigurationItem>
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(ConfigurationItem $item): self
{
if (!$this->items->contains($item)) {
$this->items->add($item);
$item->setConfiguration($this);
}
return $this;
}
public function removeItem(ConfigurationItem $item): self
{
if ($this->items->removeElement($item)) {
// set the owning side to null (unless already changed)
if ($item->getConfiguration() === $this) {
$item->setConfiguration(null);
}
}
return $this;
}
public function getProspect(): ?Prospect
{
return $this->prospect;
}
public function setProspect(?Prospect $prospect): self
{
$this->prospect = $prospect;
return $this;
}
public function getData(): array
{
$data = [];
/** @var ConfigurationItem $item */
foreach ($this->getItems() as $item) {
$data[$item->getName()] = $item->getData();
}
return $data;
}
public function getType(): ?ConfigurationType
{
return $this->type;
}
public function setType(?ConfigurationType $type): self
{
$this->type = $type;
return $this;
}
public function getNotifyLevel(): ?int
{
return $this->notifyLevel;
}
public function setNotifyLevel(?int $notifyLevel): self
{
$this->notifyLevel = $notifyLevel;
return $this;
}
/**
* @return Collection|ConfigurationItem[]
*/
public function getConfigurators(): Collection
{
return $this->items->filter(function (ConfigurationItem $item) {
if (explode('_', $item->getName())[0] == 'configurator') {
return $item;
}
return false;
});
}
public function getChronoItem(): ?ConfigurationItemValue
{
return $this->getItems()->filter(
fn(ConfigurationItem $item) => $item->getName() == "chrono_number"
)->first();
}
public function getGrcItem(): ?ConfigurationItemValue
{
return $this->getItems()->filter(
fn(ConfigurationItem $item) => $item->getName() == "grc"
)->first();
}
public function getCompleteItem(): ?ConfigurationItemValue
{
return $this->getItems()->filter(
fn(ConfigurationItem $item) => $item->getName() == "complete"
)->first();
}
}