<?php
namespace App\Entity;
use App\Entity\Enum\SmsBroadcastStateEnum;
use App\Repository\SmsBroadcastRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity(repositoryClass=SmsBroadcastRepository::class)
* @ORM\EntityListeners({"App\Listener\SmsBroadcastListener"})
*/
class SmsBroadcast
{
const MAX_SMS_SIZE = 150;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(groups={"front"})
* @Assert\Length(
* max = 255,
* minMessage = "Le message doit faire {{ limit }} caractères minimum",
* maxMessage = "Le message doit faire {{ limit }} caractères maximum"
* )
*
*/
private $zipCode;
/**
* @ORM\Column(type="integer")
*/
private $distance;
/**
* @ORM\Column(type="integer")
*/
private $volume;
/**
* @ORM\Column(type="date")
*/
private $sendingDate;
/**
* @ORM\Column(type="string", length=330)
* @Assert\Length(
* min = 1,
* max = 150,
* minMessage = "Le message doit faire {{ limit }} caractères minimum",
* maxMessage = "Le message doit faire {{ limit }} caractères maximum"
* )
*/
private $message;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false)
*
*/
private $senderUser;
/**
* @ORM\Column(type="string", length=11)
* @Assert\Length(
* min = 2,
* max = 11,
* minMessage = "Le nom de l'émetteur doit faire {{ limit }} caractères minimum",
* maxMessage = "Le nom de l'émetteur doit faire {{ limit }} caractères maximum"
* )
*/
private $senderName;
/**
* @ORM\Column(type="string", length=5)
*/
private $state=SmsBroadcastStateEnum::NEW;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $sentAt;
/**
* @Assert\Callback(groups={"front"})
*/
public function validate(ExecutionContextInterface $context, $payload)
{
//check sendingdate
$now = new \DateTime();
$now->modify('+4 weekdays');
$this->getSendingDate();
if ($this->getSendingDate() < $now) {
$context->buildViolation('La date doit être postérieure à 4 jours ouvrés')
->atPath('sendingDate')
->addViolation();
}
//check zipcode (not using regex annotation to prevent html5 generation
if(false == preg_match('/^([0-9]{5},?)*$/', $this->getZipCode())){
$context->buildViolation('Un ou plusieurs codes postaux ne sont pas valides')
->atPath('zipCode')
->addViolation();
}
}
/**
* Représente l'entité dans le BO
* @return string
*/
public function getLabel(){
return '#'.$this->getId().' for '.$this->getSenderUser()->getFullname();
}
public function getId(): ?int
{
return $this->id;
}
public function getZipCode(): ?string
{
return $this->zipCode;
}
public function setZipCode(string $zipCode): self
{
$this->zipCode = $zipCode;
return $this;
}
public function getDistance(): ?int
{
return $this->distance;
}
public function setDistance(int $distance): self
{
$this->distance = $distance;
return $this;
}
public function getVolume(): ?int
{
return $this->volume;
}
public function setVolume(int $volume): self
{
$this->volume = $volume;
return $this;
}
public function getSendingDate(): ?\DateTimeInterface
{
return $this->sendingDate;
}
public function setSendingDate(\DateTimeInterface $sendingDate): self
{
$this->sendingDate = $sendingDate;
return $this;
}
public function getMessage(): ?string
{
return $this->message;
}
public function setMessage(string $message): self
{
$this->message = $message;
return $this;
}
public function getSenderUser(): ?User
{
return $this->senderUser;
}
public function setSenderUser(?User $senderUser): self
{
$this->senderUser = $senderUser;
return $this;
}
public function getSenderName(): ?string
{
return $this->senderName;
}
public function setSenderName(string $senderName): self
{
$this->senderName = $senderName;
return $this;
}
public function getState(): ?string
{
return $this->state;
}
public function setState(string $state): self
{
$this->state = $state;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getSentAt(): ?\DateTimeInterface
{
return $this->sentAt;
}
public function setSentAt(?\DateTimeInterface $sentAt): self
{
$this->sentAt = $sentAt;
return $this;
}
public function getSize(){
return mb_strlen($this->getMessage(), 'UTF-8');
}
public function getSmsCost(): int
{
if($this->getSize() > self::MAX_SMS_SIZE){
return 2;
}
return 1;
}
public function getTotalCost(){
return $this->getSmsCost() * $this->getVolume();
}
}