src/Entity/TemperatureNotification.php line 36

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license Silk Software House Sp. Z O.O.
  4.  */
  5. declare(strict_types=1);
  6. namespace App\Entity;
  7. use App\Entity\Interfaces\ChannelInterface;
  8. use App\Entity\Traits\IdTrait;
  9. use App\Entity\Traits\TimePreUpdateTrait;
  10. use App\Entity\Traits\TimeTrait;
  11. use App\Validator\Constraints as AppAssert;
  12. use DateTime;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. /**
  16.  * A one-shot temperature alarm for one configured measurement channel.
  17.  *
  18.  * @ORM\Entity(repositoryClass="App\Repository\TemperatureNotificationRepository")
  19.  * @ORM\Table(
  20.  *     name="temperature_notifications",
  21.  *     indexes={
  22.  *         @ORM\Index(name="IDX_TEMPERATURE_NOTIFICATION_USER", columns={"user_id"}),
  23.  *         @ORM\Index(name="IDX_TEMPERATURE_NOTIFICATION_CHANNEL", columns={"channel_setting_id"}),
  24.  *         @ORM\Index(name="IDX_TEMPERATURE_NOTIFICATION_PENDING", columns={"channel_setting_id", "id"}, options={"where": "(triggered_at IS NULL)"})
  25.  *     },
  26.  *     uniqueConstraints={
  27.  *         @ORM\UniqueConstraint(name="UNIQ_TEMPERATURE_NOTIFICATION_PENDING", columns={"user_id", "channel_setting_id", "temperature_trigger", "direction", "email"}, options={"where": "(triggered_at IS NULL)"})
  28.  *     }
  29.  * )
  30.  * @ORM\HasLifecycleCallbacks()
  31.  */
  32. class TemperatureNotification implements ChannelInterface
  33. {
  34.     use IdTrait;
  35.     use TimeTrait;
  36.     use TimePreUpdateTrait;
  37.     public const DIRECTION_ABOVE 'above';
  38.     public const DIRECTION_BELOW 'below';
  39.     public const DIRECTIONS = [
  40.         self::DIRECTION_ABOVE,
  41.         self::DIRECTION_BELOW,
  42.     ];
  43.     public const MINIMUM_TEMPERATURE = -273.15;
  44.     public const MAXIMUM_TEMPERATURE 1416833.0;
  45.     /**
  46.      * @ORM\ManyToOne(targetEntity="App\Entity\User", cascade={"persist"})
  47.      * @ORM\JoinColumn(name="user_id", nullable=false)
  48.      */
  49.     private User $user;
  50.     /**
  51.      * @AppAssert\ChannelNotBlank()
  52.      *
  53.      * @ORM\ManyToOne(targetEntity="App\Entity\ChannelSetting", cascade={"persist"})
  54.      * @ORM\JoinColumn(name="channel_setting_id", nullable=false)
  55.      */
  56.     private ChannelSetting $channelSetting;
  57.     /**
  58.      * @Assert\NotNull()
  59.      * @Assert\Range(min="-273.15", max="1416833")
  60.      *
  61.      * @ORM\Column(name="temperature_trigger", type="float", nullable=false, options={"comment": "Temperature in degrees Celsius that triggers this notification."})
  62.      */
  63.     private float $temperatureTrigger;
  64.     /**
  65.      * @Assert\Choice(choices={"above", "below"})
  66.      *
  67.      * @ORM\Column(name="direction", type="string", length=16, nullable=false, options={"default": "above", "comment": "Alarm direction: above or below."})
  68.      */
  69.     private string $direction self::DIRECTION_ABOVE;
  70.     /**
  71.      * @ORM\Column(name="triggered_at", type="datetime", nullable=true, options={"comment": "When this notification was triggered."})
  72.      */
  73.     private ?DateTime $triggeredAt null;
  74.     /**
  75.      * @Assert\Email()
  76.      *
  77.      * @ORM\Column(name="email", type="string", length=255, nullable=true)
  78.      */
  79.     private ?string $email null;
  80.     public function getUser(): ?User
  81.     {
  82.         return $this->user ?? null;
  83.     }
  84.     public function setUser(User $user): self
  85.     {
  86.         $this->user $user;
  87.         return $this;
  88.     }
  89.     public function getChannelSetting(): ?ChannelSetting
  90.     {
  91.         return $this->channelSetting ?? null;
  92.     }
  93.     public function setChannelSetting(ChannelSetting $channelSetting): self
  94.     {
  95.         $this->channelSetting $channelSetting;
  96.         return $this;
  97.     }
  98.     public function getTemperatureTrigger(): ?float
  99.     {
  100.         return $this->temperatureTrigger ?? null;
  101.     }
  102.     public function setTemperatureTrigger(float $temperatureTrigger): self
  103.     {
  104.         $this->temperatureTrigger $temperatureTrigger;
  105.         return $this;
  106.     }
  107.     public function getDirection(): string
  108.     {
  109.         return $this->direction;
  110.     }
  111.     public function setDirection(string $direction): self
  112.     {
  113.         $this->direction $direction;
  114.         return $this;
  115.     }
  116.     public function getTriggeredAt(): ?DateTime
  117.     {
  118.         return $this->triggeredAt;
  119.     }
  120.     public function setTriggeredAt(?DateTime $triggeredAt): self
  121.     {
  122.         $this->triggeredAt $triggeredAt;
  123.         return $this;
  124.     }
  125.     public function getIsTriggered(): bool
  126.     {
  127.         return null !== $this->triggeredAt;
  128.     }
  129.     public function isTriggeredBy(float $temperature): bool
  130.     {
  131.         if (!isset($this->temperatureTrigger)) {
  132.             return false;
  133.         }
  134.         if (self::DIRECTION_ABOVE === $this->direction) {
  135.             return $temperature >= $this->temperatureTrigger;
  136.         }
  137.         return self::DIRECTION_BELOW === $this->direction
  138.             && $temperature <= $this->temperatureTrigger;
  139.     }
  140.     public function getEmail(): ?string
  141.     {
  142.         return $this->email;
  143.     }
  144.     public function setEmail(?string $email): self
  145.     {
  146.         $this->email $email;
  147.         return $this;
  148.     }
  149.     public function getMeasurement(): ?Measurement
  150.     {
  151.         if (!isset($this->channelSetting) || !$this->channelSetting->getMeasurementSetting()) {
  152.             return null;
  153.         }
  154.         return $this->channelSetting->getMeasurementSetting()->getMeasurement();
  155.     }
  156.     public function hasChannel(): bool
  157.     {
  158.         return isset($this->channelSetting);
  159.     }
  160. }