src/Entity/Restriction.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Repository\RestrictionRepository;
  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.  * Restrictions are limitations in time to connection by users of the tvCompany
  11.  * Ex: a restriction with a code to "WEEKLY", a field to "saturday", and a value to "10:00 - 12:00", means that
  12.  * the users will not be allowed in between those hours
  13.  * Every date will be stored in a timestamp fashion
  14.  * @ORM\Entity(repositoryClass=RestrictionRepository::class)
  15.  * Réservé aux admins (#3214 : sans opération déclarée, le CRUD complet était ouvert sans connexion ; le back-office
  16.  * passe par /api/restrictions/manager).
  17.  * @ApiResource(
  18.  *     attributes={"security"="is_granted('ROLE_ADMIN')"},
  19.  *     normalizationContext={"groups"={"restriction:read"}},
  20.  *     denormalizationContext={"groups"={"restriction:write"}}
  21.  * )
  22.  * @ORM\HasLifecycleCallbacks()
  23.  */
  24. class Restriction
  25. {
  26.     /**
  27.      * With this code the will register the start and
  28.      * the value will register the end, but we will only take into
  29.      * account the day
  30.      */
  31.     const WEEKLY = 'weekly';
  32.     /**
  33.      * With this code the will register the start and
  34.      * the value will register the end, but we will ignore the year who will
  35.      * be replaced by the current year
  36.      */
  37.     const ANNUALLY = 'annually';
  38.     /**
  39.      * With this code the field will register the start and
  40.      * the value will register the end
  41.      */
  42.     const PUNCTUAL = 'punctual';
  43.     /**
  44.      * @ORM\Id
  45.      * @ORM\GeneratedValue
  46.      * @ORM\Column(type="integer")
  47.      */
  48.     private $id;
  49.     /**
  50.      * @ORM\Column(type="string", length=255)
  51.      * @Groups({"restriction:read"})
  52.      */
  53.     private $code;
  54.     /**
  55.      * @ORM\Column(type="string", length=255)
  56.      * @Groups({"restriction:read"})
  57.      */
  58.     private $field;
  59.     /**
  60.      * @ORM\Column(type="text", nullable=true)
  61.      * @Groups({"restriction:read", "restriction:write"})
  62.      * @Assert\Regex("/^(\d{2}:\d{2}) - (\d{2}:\d{2})$/")
  63.      */
  64.     private $value;
  65.     /**
  66.      * @ORM\Column(type="datetime", nullable=true)
  67.      * @Gedmo\Timestampable(on="create")
  68.      */
  69.     private $createdAt;
  70.     /**
  71.      * @ORM\Column(type="datetime")
  72.      * @Gedmo\Timestampable(on="update")
  73.      */
  74.     private $updatedAt;
  75.     /**
  76.      * @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="restrictions")
  77.      * @ORM\JoinColumn(nullable=true)
  78.      * @Groups({"restriction:read", "restriction:write"})
  79.      */
  80.     private $tvCompany;
  81.     /**
  82.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="restrictions")
  83.      * @ORM\JoinColumn(nullable=true)
  84.      * @Assert\NotNull
  85.      * @Groups({"restriction:read", "restriction:write"})
  86.      */
  87.     private $company;
  88.     /**
  89.      * Puts the yearly date to the present if necessary
  90.      * @param \DateTime $date
  91.      * @return \DateTime
  92.      */
  93.     private function updateYearlyDate(\DateTime $date): \DateTime
  94.     {
  95.         return date_create_from_format('d-m H:i', $date->format("d-m H:i"));
  96.     }
  97.     /**
  98.      * Puts the weekly date to the present if necessary
  99.      * @param \DateTime $date
  100.      * @return \DateTime
  101.      */
  102.     private function updateWeeklyDate(\DateTime $date): \DateTime
  103.     {
  104.         return date_create_from_format('l H:i', $date->format('l H:i'));
  105.     }
  106.     /**
  107.      * Gets the start of a restriction for a weekly value
  108.      * no error management is done in here be careful
  109.      * @param \DateTime $time
  110.      * @return \DateTime|false
  111.      */
  112.     private function getWeeklyStart(\DateTime $time)
  113.     {
  114.         list($hour) = explode(' - ', $this->getValue());
  115.         return $this->innerWeeklyDate($time, $hour);
  116.     }
  117.     /**
  118.      * Gets the limit for the given hour
  119.      * @param \DateTime $time
  120.      * @param string $hour
  121.      * @return \DateTime|false
  122.      */
  123.     private function innerWeeklyDate(\DateTime $time, string $hour)
  124.     {
  125.         $weekday = $this->getField();
  126.         $_time = clone $time;
  127.         if ($weekday === "everyday") {
  128.             $weekday = $_time->format('l');
  129.         }
  130.         $weekday = ucfirst($weekday);
  131.         if ($weekday != $_time->format('l')) {
  132.             $_time->add(date_interval_create_from_date_string("next $weekday"));
  133.         }
  134.         $hour = date_create_from_format('H:i', $hour);
  135.         return $_time->setTime(intval($hour->format('H')), intval($hour->format('i')), 0, 0);
  136.     }
  137.     /**
  138.      * Gets the start of a restriction for a weekly value
  139.      * no error management is done in here be careful
  140.      * @param \DateTime $time
  141.      * @return \DateTime|false
  142.      */
  143.     private function getWeeklyEnd(\DateTime $time)
  144.     {
  145.         list($hour) = array_reverse(explode(' - ', $this->getValue()));
  146.         return $this->innerWeeklyDate($time, $hour);
  147.     }
  148.     /**
  149.      * @param \DateTime $time
  150.      * @return \DateTime
  151.      */
  152.     public function getStart(\DateTime $time): \DateTime
  153.     {
  154.         $date = (new \DateTime());
  155.         switch ($this->code) {
  156.             case self::ANNUALLY:
  157.                 $date = $time;
  158.                 break;
  159.             case self::WEEKLY:
  160.                 $date = $this->getWeeklyStart($time);
  161.                 break;
  162.             case self::PUNCTUAL:
  163.             default:
  164.                 break;
  165.         }
  166.         return $date;
  167.     }
  168.     /**
  169.      * @param \DateTime $time
  170.      * @return \DateTime
  171.      */
  172.     public function getEnd(\DateTime $time): \DateTime
  173.     {
  174.         $date = (new \DateTime());
  175.         switch ($this->code) {
  176.             case self::ANNUALLY:
  177.                 $date = $this->updateYearlyDate($date);
  178.                 break;
  179.             case self::WEEKLY:
  180.                 $date = $this->getWeeklyEnd($time);
  181.                 break;
  182.             case self::PUNCTUAL:
  183.             default:
  184.                 break;
  185.         }
  186.         return $date;
  187.     }
  188.     public function getId(): ?int
  189.     {
  190.         return $this->id;
  191.     }
  192.     public function getCode(): ?string
  193.     {
  194.         return $this->code;
  195.     }
  196.     public function setCode(string $code): self
  197.     {
  198.         $this->code = $code;
  199.         return $this;
  200.     }
  201.     public function getField(): ?string
  202.     {
  203.         return $this->field;
  204.     }
  205.     public function setField(string $field): self
  206.     {
  207.         $this->field = $field;
  208.         return $this;
  209.     }
  210.     public function getValue(): ?string
  211.     {
  212.         return $this->value;
  213.     }
  214.     public function setValue(?string $value): self
  215.     {
  216.         $this->value = $value;
  217.         return $this;
  218.     }
  219.     public function getCreatedAt(): ?\DateTimeInterface
  220.     {
  221.         return $this->createdAt;
  222.     }
  223.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  224.     {
  225.         $this->createdAt = $createdAt;
  226.         return $this;
  227.     }
  228.     public function getUpdatedAt(): ?\DateTimeInterface
  229.     {
  230.         return $this->updatedAt;
  231.     }
  232.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  233.     {
  234.         $this->updatedAt = $updatedAt;
  235.         return $this;
  236.     }
  237.     public function getTvCompany(): ?TvCompany
  238.     {
  239.         return $this->tvCompany;
  240.     }
  241.     public function setTvCompany(?TvCompany $tvCompany): self
  242.     {
  243.         $this->tvCompany = $tvCompany;
  244.         return $this;
  245.     }
  246.     public function getCompany(): ?Company
  247.     {
  248.         return $this->company;
  249.     }
  250.     public function setCompany(?Company $company): self
  251.     {
  252.         $this->company = $company;
  253.         return $this;
  254.     }
  255.     /**
  256.      * Retrieves if the restriction is applied or not
  257.      *
  258.      * @param \DateTime $time
  259.      * @return bool
  260.      */
  261.     public function isRestricted(\DateTime $time): bool
  262.     {
  263.         return ($time > $this->getStart($time) && $time < $this->getEnd($time));
  264.     }
  265.     /**
  266.      * @Assert\IsTrue(message="The first hour must be inferior to the second one")
  267.      */
  268.     public function isValueValid(): bool
  269.     {
  270.         $result = false;
  271.         list($start, $end) = explode(' - ', $this->value);
  272.         $start = str_replace(':', '', $start);
  273.         $end = str_replace(':', '', $end);
  274.         if (intval($start) < intval($end)) {
  275.             $result = true;
  276.         }
  277.         return $result;
  278.     }
  279. }