<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Model\Api\ConfigurationItemInput;
use App\Model\EntityTime;
use App\Repository\ConfigurationItemRepository;
use App\State\ConfigurationItemProcessor;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ApiResource(
operations: [
new Get(),
new Post(processor: ConfigurationItemProcessor::class),
new Put(processor: ConfigurationItemProcessor::class),
new Delete(),
],
normalizationContext: ['groups' => ['identifier:read', 'configuration-item:read']],
denormalizationContext: ['groups' => ['configuration-item:write']],
input: ConfigurationItemInput::class,
)]
#[ORM\Entity(repositoryClass: ConfigurationItemRepository::class)]
#[ORM\InheritanceType('JOINED')]
#[ORM\MappedSuperclass()]
#[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
#[ORM\DiscriminatorMap([
ConfigurationItem::DISCRIMINATOR_PARENT => ConfigurationItemParent::class,
ConfigurationItem::DISCRIMINATOR_VALUE => ConfigurationItemValue::class
])]
class ConfigurationItem extends EntityTime
{
const DISCRIMINATOR_ITEM = 'item';
const DISCRIMINATOR_PARENT = 'parent';
const DISCRIMINATOR_VALUE = 'value';
#[ORM\Column(length: 100)]
#[Groups(['configuration-item:read'])]
protected ?string $name = null;
#[ORM\ManyToOne(inversedBy: 'items')]
protected ?Configuration $configuration = null;
#[ORM\ManyToOne(inversedBy: 'nodes')]
protected ?ConfigurationItemParent $parent = null;
public function __construct(string $name = null)
{
parent::__construct();
$this->name = $name;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getConfiguration(): ?Configuration
{
return $this->configuration;
}
public function setConfiguration(?Configuration $configuration): self
{
$this->configuration = $configuration;
return $this;
}
public function getParent(): ?ConfigurationItemParent
{
return $this->parent;
}
public function setParent(?ConfigurationItemParent $parent): self
{
$this->parent = $parent;
return $this;
}
public function getData(): mixed
{
return null;
}
#[Groups(['configuration-item:read'])]
public function getDiscriminator()
{
if ($this::class == ConfigurationItemValue::class)
return self::DISCRIMINATOR_VALUE;
if ($this::class == ConfigurationItemParent::class)
return self::DISCRIMINATOR_PARENT;
return self::DISCRIMINATOR_ITEM;
}
public function getGlobalConfiguration(): Configuration
{
if ($this->configuration)
return $this->configuration;
return $this->getParent()->getGlobalConfiguration();
}
}