src/Entity/Restriction.php line 24

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