<?phpnamespace App\Entity;use ApiPlatform\Core\Annotation\ApiResource;use App\Repository\OrderRepository;use Doctrine\ORM\Mapping as ORM;use Gedmo\Mapping\Annotation as Gedmo;use Symfony\Component\Serializer\Annotation\Groups;use Symfony\Component\Validator\Constraints as Assert;/** * @ORM\Table(name="`order`") * @ORM\Entity(repositoryClass=OrderRepository::class) * @ORM\HasLifecycleCallbacks() */class Order{ /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") * @Groups({"order:read"}) */ private $id; /** * @ORM\ManyToOne(targetEntity=Subscription::class, inversedBy="orders") * @ORM\JoinColumn(nullable=false) * @Groups({"order:read"}) */ private $subscription; /** * @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="orders") * @ORM\JoinColumn(nullable=true) * @Groups({"order:read"}) * @var TvCompany */ private $tvCompany; /** * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="orders") * @ORM\JoinColumn(nullable=true) * @Assert\NotNull * @Groups({"order:read"}) * @var Company */ private $company; /** * @ORM\Column(type="datetime") * @Groups({"order:read"}) */ private $start; /** * @ORM\Column(type="datetime") * @Groups({"order:read"}) */ private $end; /** * @ORM\Column(type="boolean", options={"default": "1"}) * @Groups({"order:read"}) */ private $active = true; /** * @ORM\Column(type="float") * @Groups({"order:read"}) */ private $priceHt; /** * @ORM\Column(type="integer") * @Groups({"order:read"}) */ private $credits; /** * @ORM\Column(type="datetime", nullable=true) * @Gedmo\Timestampable(on="create") */ private $createdAt; /** * @ORM\Column(type="datetime") * @Gedmo\Timestampable(on="update") */ private $updatedAt; public function getId(): ?int { return $this->id; } /** * Updates the tvCompany according to the status of the order * @return Order * @ORM\PrePersist */ public function updateTvCompany(): self { if ($this->active){ $this->tvCompany->setSubscriptionStart($this->getStart()); $this->tvCompany->setSubscriptionEnd($this->getEnd()); } return $this; } /** * Updates the company according to the status of the order * @return Order * @ORM\PrePersist */ public function updateCompany(): self { if ($this->active){ $this->company->setSubscriptionStart($this->getStart()); $this->company->setSubscriptionEnd($this->getEnd()); } return $this; } public function getStart(): ?\DateTimeInterface { return $this->start; } public function setStart(\DateTimeInterface $start): self { $this->start = $start; return $this; } public function getEnd(): ?\DateTimeInterface { return $this->end; } public function setEnd(\DateTimeInterface $end): self { $this->end = $end; return $this; } public function getActive(): ?bool { return $this->active; } public function setActive(bool $active): self { $this->active = $active; return $this; } public function getPriceHt(): ?float { return $this->priceHt; } public function setPriceHt(float $priceHt): self { $this->priceHt = $priceHt; return $this; } public function getCredits(): ?int { return $this->credits; } public function setCredits(int $credits): self { $this->credits = $credits; return $this; } public function getSubscription(): ?subscription { return $this->subscription; } public function setSubscription(?subscription $subscription): self { $this->subscription = $subscription; return $this; } public function getTvCompany(): ?TvCompany { return $this->tvCompany; } public function setTvCompany(?TvCompany $tvCompany): self { $this->tvCompany = $tvCompany; return $this; } public function getCompany(): ?Company { return $this->company; } public function setCompany(?Company $company): self { $this->company = $company; return $this; } public function getCreatedAt(): ?\DateTimeInterface { return $this->createdAt; } public function setCreatedAt(\DateTimeInterface $createdAt): self { $this->createdAt = $createdAt; return $this; } public function getUpdatedAt(): ?\DateTimeInterface { return $this->updatedAt; } public function setUpdatedAt(\DateTimeInterface $updatedAt): self { $this->updatedAt = $updatedAt; return $this; } /** * Subtract the given number to the credits score * @param $sub * @return $this */ public function subtractCredits($sub): self { if (($this->credits - $sub) < 0){ throw new \InvalidArgumentException('Credits score cannot be negative'); } $this->credits -= $sub; return $this; } /** * Updates the start and the end of an order considering the duration * @param \DateTimeInterface $start * @return $this */ public function updateStart(\DateTimeInterface $start): self { $this->start = $start; if (!$this->subscription instanceof Subscription){ throw new \InvalidArgumentException("Cannot update start of an order if subscription is not set"); } $this->end = (clone $this->start)->add(\DateInterval::createFromDateString("{$this->subscription->getDuration()} seconds")); return $this; } /** * Creates an order object from a subscription * @param Subscription $subscription * @return static */ static public function createFromSubscription(Subscription $subscription): self { $order = new Order(); $order->subscription = $subscription; $order->priceHt = $subscription->getPriceHt(); $order->credits = $subscription->getCredits(); $order->start = (new \DateTime()); $order->end = (clone $order->start)->add(\DateInterval::createFromDateString("{$subscription->getDuration()} seconds")); return $order; }}