src/Entity/SmsBroadcast.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Enum\SmsBroadcastStateEnum;
  4. use App\Repository\SmsBroadcastRepository;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. /**
  10.  * @ORM\Entity(repositoryClass=SmsBroadcastRepository::class)
  11.  * @ORM\EntityListeners({"App\Listener\SmsBroadcastListener"})
  12.  */
  13. class SmsBroadcast
  14. {
  15.     const MAX_SMS_SIZE 150;
  16.     /**
  17.      * @ORM\Id()
  18.      * @ORM\GeneratedValue()
  19.      * @ORM\Column(type="integer")
  20.      */
  21.     private $id;
  22.     /**
  23.      * @ORM\Column(type="string", length=255)
  24.      * @Assert\NotBlank(groups={"front"})
  25.      * @Assert\Length(
  26.      *      max = 255,
  27.      *      minMessage = "Le message doit faire {{ limit }} caractères minimum",
  28.      *      maxMessage = "Le message doit faire {{ limit }} caractères maximum"
  29.      * )
  30.      *
  31.      */
  32.     private $zipCode;
  33.     /**
  34.      * @ORM\Column(type="integer")
  35.      */
  36.     private $distance;
  37.     /**
  38.      * @ORM\Column(type="integer")
  39.      */
  40.     private $volume;
  41.     /**
  42.      * @ORM\Column(type="date")
  43.      */
  44.     private $sendingDate;
  45.     /**
  46.      * @ORM\Column(type="string", length=330)
  47.      * @Assert\Length(
  48.      *      min = 1,
  49.      *      max = 150,
  50.      *      minMessage = "Le message doit faire {{ limit }} caractères minimum",
  51.      *      maxMessage = "Le message doit faire {{ limit }} caractères maximum"
  52.      * )
  53.      */
  54.     private $message;
  55.     /**
  56.      * @ORM\ManyToOne(targetEntity=User::class)
  57.      * @ORM\JoinColumn(nullable=false)
  58.      *
  59.      */
  60.     private $senderUser;
  61.     /**
  62.      * @ORM\Column(type="string", length=11)
  63.      * @Assert\Length(
  64.      *      min = 2,
  65.      *      max = 11,
  66.      *      minMessage = "Le nom de l'émetteur doit faire {{ limit }} caractères minimum",
  67.      *      maxMessage = "Le nom de l'émetteur doit faire {{ limit }} caractères maximum"
  68.      * )
  69.      */
  70.     private $senderName;
  71.     /**
  72.      * @ORM\Column(type="string", length=5)
  73.      */
  74.     private $state=SmsBroadcastStateEnum::NEW;
  75.     /**
  76.      * @ORM\Column(type="datetime")
  77.      * @Gedmo\Timestampable(on="create")
  78.      */
  79.     private $createdAt;
  80.     /**
  81.      * @ORM\Column(type="datetime", nullable=true)
  82.      */
  83.     private $sentAt;
  84.     /**
  85.      * @Assert\Callback(groups={"front"})
  86.      */
  87.     public function validate(ExecutionContextInterface $context$payload)
  88.     {
  89.         //check sendingdate
  90.         $now = new \DateTime();
  91.         $now->modify('+4 weekdays');
  92.         $this->getSendingDate();
  93.         if ($this->getSendingDate() < $now) {
  94.             $context->buildViolation('La date doit être postérieure à 4 jours ouvrés')
  95.                 ->atPath('sendingDate')
  96.                 ->addViolation();
  97.         }
  98.         //check zipcode (not using regex annotation to prevent html5 generation
  99.         if(false == preg_match('/^([0-9]{5},?)*$/'$this->getZipCode())){
  100.             $context->buildViolation('Un ou plusieurs codes postaux ne sont pas valides')
  101.                 ->atPath('zipCode')
  102.                 ->addViolation();
  103.         }
  104.     }
  105.     /**
  106.      * Représente l'entité dans le BO
  107.      * @return string
  108.      */
  109.     public function getLabel(){
  110.         return '#'.$this->getId().' for '.$this->getSenderUser()->getFullname();
  111.     }
  112.     public function getId(): ?int
  113.     {
  114.         return $this->id;
  115.     }
  116.     public function getZipCode(): ?string
  117.     {
  118.         return $this->zipCode;
  119.     }
  120.     public function setZipCode(string $zipCode): self
  121.     {
  122.         $this->zipCode $zipCode;
  123.         return $this;
  124.     }
  125.     public function getDistance(): ?int
  126.     {
  127.         return $this->distance;
  128.     }
  129.     public function setDistance(int $distance): self
  130.     {
  131.         $this->distance $distance;
  132.         return $this;
  133.     }
  134.     public function getVolume(): ?int
  135.     {
  136.         return $this->volume;
  137.     }
  138.     public function setVolume(int $volume): self
  139.     {
  140.         $this->volume $volume;
  141.         return $this;
  142.     }
  143.     public function getSendingDate(): ?\DateTimeInterface
  144.     {
  145.         return $this->sendingDate;
  146.     }
  147.     public function setSendingDate(\DateTimeInterface $sendingDate): self
  148.     {
  149.         $this->sendingDate $sendingDate;
  150.         return $this;
  151.     }
  152.     public function getMessage(): ?string
  153.     {
  154.         return $this->message;
  155.     }
  156.     public function setMessage(string $message): self
  157.     {
  158.         $this->message $message;
  159.         return $this;
  160.     }
  161.     public function getSenderUser(): ?User
  162.     {
  163.         return $this->senderUser;
  164.     }
  165.     public function setSenderUser(?User $senderUser): self
  166.     {
  167.         $this->senderUser $senderUser;
  168.         return $this;
  169.     }
  170.     public function getSenderName(): ?string
  171.     {
  172.         return $this->senderName;
  173.     }
  174.     public function setSenderName(string $senderName): self
  175.     {
  176.         $this->senderName $senderName;
  177.         return $this;
  178.     }
  179.     public function getState(): ?string
  180.     {
  181.         return $this->state;
  182.     }
  183.     public function setState(string $state): self
  184.     {
  185.         $this->state $state;
  186.         return $this;
  187.     }
  188.     public function getCreatedAt(): ?\DateTimeInterface
  189.     {
  190.         return $this->createdAt;
  191.     }
  192.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  193.     {
  194.         $this->createdAt $createdAt;
  195.         return $this;
  196.     }
  197.     public function getSentAt(): ?\DateTimeInterface
  198.     {
  199.         return $this->sentAt;
  200.     }
  201.     public function setSentAt(?\DateTimeInterface $sentAt): self
  202.     {
  203.         $this->sentAt $sentAt;
  204.         return $this;
  205.     }
  206.     public function getSize(){
  207.         return mb_strlen($this->getMessage(), 'UTF-8');
  208.     }
  209.     public function getSmsCost(): int
  210.     {
  211.         if($this->getSize() > self::MAX_SMS_SIZE){
  212.             return 2;
  213.         }
  214.         return 1;
  215.     }
  216.     public function getTotalCost(){
  217.         return $this->getSmsCost() * $this->getVolume();
  218.     }
  219. }