src/Entity/Order.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Repository\OrderRepository;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use Symfony\Component\Serializer\Annotation\Groups;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. /**
  10.  * @ORM\Table(name="`order`")
  11.  * @ORM\Entity(repositoryClass=OrderRepository::class)
  12.  * @ORM\HasLifecycleCallbacks()
  13.  */
  14. class Order
  15. {
  16.     /**
  17.      * @ORM\Id
  18.      * @ORM\GeneratedValue
  19.      * @ORM\Column(type="integer")
  20.      * @Groups({"order:read"})
  21.      */
  22.     private $id;
  23.     /**
  24.      * @ORM\ManyToOne(targetEntity=Subscription::class, inversedBy="orders")
  25.      * @ORM\JoinColumn(nullable=false)
  26.      * @Groups({"order:read"})
  27.      */
  28.     private $subscription;
  29.     /**
  30.      * @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="orders")
  31.      * @ORM\JoinColumn(nullable=true)
  32.      * @Groups({"order:read"})
  33.      * @var TvCompany
  34.      */
  35.     private $tvCompany;
  36.     /**
  37.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="orders")
  38.      * @ORM\JoinColumn(nullable=true)
  39.      * @Assert\NotNull
  40.      * @Groups({"order:read"})
  41.      * @var Company
  42.      */
  43.     private $company;
  44.     /**
  45.      * @ORM\Column(type="datetime")
  46.      * @Groups({"order:read"})
  47.      */
  48.     private $start;
  49.     /**
  50.      * @ORM\Column(type="datetime")
  51.      * @Groups({"order:read"})
  52.      */
  53.     private $end;
  54.     /**
  55.      * @ORM\Column(type="boolean", options={"default": "1"})
  56.      * @Groups({"order:read"})
  57.      */
  58.     private $active true;
  59.     /**
  60.      * @ORM\Column(type="float")
  61.      * @Groups({"order:read"})
  62.      */
  63.     private $priceHt;
  64.     /**
  65.      * @ORM\Column(type="integer")
  66.      * @Groups({"order:read"})
  67.      */
  68.     private $credits;
  69.     /**
  70.      * @ORM\Column(type="datetime", nullable=true)
  71.      * @Gedmo\Timestampable(on="create")
  72.      */
  73.     private $createdAt;
  74.     /**
  75.      * @ORM\Column(type="datetime")
  76.      * @Gedmo\Timestampable(on="update")
  77.      */
  78.     private $updatedAt;
  79.     public function getId(): ?int
  80.     {
  81.         return $this->id;
  82.     }
  83.     /**
  84.      * Updates the tvCompany according to the status of the order
  85.      * @return Order
  86.      * @ORM\PrePersist
  87.      */
  88.     public function updateTvCompany(): self
  89.     {
  90.         if ($this->active){
  91.             $this->tvCompany->setSubscriptionStart($this->getStart());
  92.             $this->tvCompany->setSubscriptionEnd($this->getEnd());
  93.         }
  94.         return $this;
  95.     }
  96.     /**
  97.      * Updates the company according to the status of the order
  98.      * @return Order
  99.      * @ORM\PrePersist
  100.      */
  101.     public function updateCompany(): self
  102.     {
  103.         if ($this->active){
  104.             $this->company->setSubscriptionStart($this->getStart());
  105.             $this->company->setSubscriptionEnd($this->getEnd());
  106.         }
  107.         return $this;
  108.     }
  109.     public function getStart(): ?\DateTimeInterface
  110.     {
  111.         return $this->start;
  112.     }
  113.     public function setStart(\DateTimeInterface $start): self
  114.     {
  115.         $this->start $start;
  116.         return $this;
  117.     }
  118.     public function getEnd(): ?\DateTimeInterface
  119.     {
  120.         return $this->end;
  121.     }
  122.     public function setEnd(\DateTimeInterface $end): self
  123.     {
  124.         $this->end $end;
  125.         return $this;
  126.     }
  127.     public function getActive(): ?bool
  128.     {
  129.         return $this->active;
  130.     }
  131.     public function setActive(bool $active): self
  132.     {
  133.         $this->active $active;
  134.         return $this;
  135.     }
  136.     public function getPriceHt(): ?float
  137.     {
  138.         return $this->priceHt;
  139.     }
  140.     public function setPriceHt(float $priceHt): self
  141.     {
  142.         $this->priceHt $priceHt;
  143.         return $this;
  144.     }
  145.     public function getCredits(): ?int
  146.     {
  147.         return $this->credits;
  148.     }
  149.     public function setCredits(int $credits): self
  150.     {
  151.         $this->credits $credits;
  152.         return $this;
  153.     }
  154.     public function getSubscription(): ?subscription
  155.     {
  156.         return $this->subscription;
  157.     }
  158.     public function setSubscription(?subscription $subscription): self
  159.     {
  160.         $this->subscription $subscription;
  161.         return $this;
  162.     }
  163.     public function getTvCompany(): ?TvCompany
  164.     {
  165.         return $this->tvCompany;
  166.     }
  167.     public function setTvCompany(?TvCompany $tvCompany): self
  168.     {
  169.         $this->tvCompany $tvCompany;
  170.         return $this;
  171.     }
  172.     public function getCompany(): ?Company
  173.     {
  174.         return $this->company;
  175.     }
  176.     public function setCompany(?Company $company): self
  177.     {
  178.         $this->company $company;
  179.         return $this;
  180.     }
  181.     public function getCreatedAt(): ?\DateTimeInterface
  182.     {
  183.         return $this->createdAt;
  184.     }
  185.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  186.     {
  187.         $this->createdAt $createdAt;
  188.         return $this;
  189.     }
  190.     public function getUpdatedAt(): ?\DateTimeInterface
  191.     {
  192.         return $this->updatedAt;
  193.     }
  194.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  195.     {
  196.         $this->updatedAt $updatedAt;
  197.         return $this;
  198.     }
  199.     /**
  200.      * Subtract the given number to the credits score
  201.      * @param $sub
  202.      * @return $this
  203.      */
  204.     public function subtractCredits($sub): self
  205.     {
  206.         if (($this->credits $sub) < 0){
  207.             throw new \InvalidArgumentException('Credits score cannot be negative');
  208.         }
  209.         $this->credits -= $sub;
  210.         return $this;
  211.     }
  212.     /**
  213.      * Updates the start and the end of an order considering the duration
  214.      * @param \DateTimeInterface $start
  215.      * @return $this
  216.      */
  217.     public function updateStart(\DateTimeInterface $start): self
  218.     {
  219.         $this->start $start;
  220.         if (!$this->subscription instanceof Subscription){
  221.             throw new \InvalidArgumentException("Cannot update start of an order if subscription is not set");
  222.         }
  223.         $this->end = (clone $this->start)->add(\DateInterval::createFromDateString("{$this->subscription->getDuration()} seconds"));
  224.         return $this;
  225.     }
  226.     /**
  227.      * Creates an order object from a subscription
  228.      * @param Subscription $subscription
  229.      * @return static
  230.      */
  231.     static public function createFromSubscription(Subscription $subscription): self
  232.     {
  233.         $order = new Order();
  234.         $order->subscription $subscription;
  235.         $order->priceHt $subscription->getPriceHt();
  236.         $order->credits $subscription->getCredits();
  237.         $order->start = (new \DateTime());
  238.         $order->end = (clone $order->start)->add(\DateInterval::createFromDateString("{$subscription->getDuration()} seconds"));
  239.         return $order;
  240.     }
  241. }