<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\EmailRepository")
* @ORM\HasLifecycleCallbacks()
*/
#[ApiResource(
normalizationContext: ['groups' => ['email:item', 'email:list']],
denormalizationContext: ['allow_extra_attributes' => true, 'groups' => ['pinpointCampaign:write']],
attributes: ['security' => "is_granted('ROLE_USER')"],
collectionOperations: [
'get' => ['normalization_context' => ['groups' => ['email:list']]],
'post' => ['security' => "is_granted('ROLE_ADMIN')"],
],
itemOperations: [
'get' => ['normalization_context' => ['groups' => ['email:item']]],
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
],
)]
class Email
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"email:item","email:list","email:write"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(max="255")
* @Groups({"email:item","email:list","email:write"})
*/
private $label;
/**
* @ORM\Column(type="boolean")
* @Assert\Type(type="boolean")
* @Groups({"email:item","email:list","email:write"})
*/
private $isPinpointTemplate;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(max="255")
* @Groups({"email:item","email:list","email:write"})
*/
private $subject;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
* @Groups({"email:item","email:write"})
*/
private $body;
/**
* @var Collection
*
* @ORM\OneToMany(targetEntity="App\Entity\PinpointCampaign", mappedBy="email", cascade={"persist"})
*/
private $pinpointCampaigns;
/**
* Email constructor.
*/
public function __construct()
{
$this->isPinpointTemplate = false;
$this->pinpointCampaigns = new ArrayCollection();
}
public function __toString()
{
return $this->label;
}
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getIsPinpointTemplate(): bool
{
return $this->isPinpointTemplate;
}
public function setIsPinpointTemplate(bool $isPinpointTemplate): self
{
$this->isPinpointTemplate = $isPinpointTemplate;
return $this;
}
public function getSubject(): ?string
{
return $this->subject;
}
public function setSubject(string $subject): self
{
$this->subject = $subject;
return $this;
}
public function getBody(): ?string
{
return $this->body;
}
public function setBody(string $body): self
{
$this->body = $body;
return $this;
}
/**
* @return Collection|PinpointCampaign[]
*/
public function getPinpointCampaigns(): Collection
{
return $this->pinpointCampaigns;
}
public function addPinpointCampaign(PinpointCampaign $pinpointCampaign): self
{
if (!$this->pinpointCampaigns->contains($pinpointCampaign)) {
$this->pinpointCampaigns[] = $pinpointCampaign;
$pinpointCampaign->setEmail($this);
}
return $this;
}
public function removePinpointCampaign(PinpointCampaign $pinpointCampaign): self
{
if ($this->pinpointCampaigns->contains($pinpointCampaign)) {
$this->pinpointCampaigns->removeElement($pinpointCampaign);
// set the owning side to null (unless already changed)
if ($pinpointCampaign->getEmail() === $this) {
$pinpointCampaign->setEmail(null);
}
}
return $this;
}
}