src/Entity/Channel.php line 26

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\Traits\IdTrait;
  8. use App\Entity\Traits\TimePreUpdateTrait;
  9. use App\Entity\Traits\TimeTrait;
  10. use DateTime;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Exception;
  15. /**
  16.  * Class Channel.
  17.  *
  18.  * @ORM\Table("channels")
  19.  * @ORM\Entity(repositoryClass="App\Repository\ChannelRepository")
  20.  * @ORM\HasLifecycleCallbacks()
  21.  */
  22. class Channel
  23. {
  24.     use IdTrait;
  25.     use TimeTrait;
  26.     use TimePreUpdateTrait;
  27.     /**
  28.      * Recipe.
  29.      *
  30.      * @var Recipe|null
  31.      *
  32.      * @ORM\ManyToOne(targetEntity="App\Entity\Recipe", cascade={"persist"})
  33.      * @ORM\JoinColumn(name="recipe_id", nullable=true)
  34.      */
  35.     private ?Recipe $recipe null;
  36.     /**
  37.      * Measurement.
  38.      *
  39.      * @var Measurement|null
  40.      *
  41.      * @ORM\ManyToOne(targetEntity="App\Entity\Measurement", inversedBy="channels", cascade={"persist"})
  42.      * @ORM\JoinColumn(name="measurement_id", nullable=false)
  43.      */
  44.     private ?Measurement $measurement null;
  45.     /**
  46.      * Number.
  47.      *
  48.      * @var int
  49.      *
  50.      * @ORM\Column(name="channel_number", type="integer", nullable=false, options={"comment": "Channel number."})
  51.      */
  52.     private int $number 0;
  53.     /**
  54.      * @ORM\OneToMany(targetEntity="App\Entity\Probe", mappedBy="channel")
  55.      * @ORM\OrderBy({"time" = "ASC"})
  56.      */
  57.     private $probes;
  58.     /**
  59.      * @ORM\Column(type="string", length=255, nullable=true)
  60.      */
  61.     private ?string $name null;
  62.     /**
  63.      * @var Collection<int, ChannelSetting>
  64.      *
  65.      * @ORM\OneToMany(targetEntity="App\Entity\ChannelSetting", mappedBy="channel")
  66.      * @ORM\OrderBy({"priority": "ASC"})
  67.      */
  68.     private Collection $channelSettings;
  69.     /**
  70.      * Channel constructor.
  71.      */
  72.     public function __construct()
  73.     {
  74.         $this->probes = new ArrayCollection();
  75.         $this->channelSettings = new ArrayCollection();
  76.     }
  77.     /**
  78.      * Get recipe.
  79.      *
  80.      * @return Recipe|null
  81.      */
  82.     public function getRecipe(): ?Recipe
  83.     {
  84.         return $this->recipe;
  85.     }
  86.     /**
  87.      * Set recipe.
  88.      *
  89.      * @param Recipe|null $recipe
  90.      *
  91.      * @return Channel
  92.      */
  93.     public function setRecipe(?Recipe $recipe): self
  94.     {
  95.         $this->recipe $recipe;
  96.         return $this;
  97.     }
  98.     /**
  99.      * Get measurement.
  100.      *
  101.      * @return Measurement
  102.      */
  103.     public function getMeasurement(): Measurement
  104.     {
  105.         if (!isset($this->measurement)) {
  106.             throw new \LogicException('The measurement is not set, not expected.');
  107.         }
  108.         return $this->measurement;
  109.     }
  110.     /**
  111.      * Set measurement.
  112.      *
  113.      * @param Measurement $measurement
  114.      *
  115.      * @return Channel
  116.      */
  117.     public function setMeasurement(Measurement $measurement): self
  118.     {
  119.         $this->measurement $measurement;
  120.         return $this;
  121.     }
  122.     /**
  123.      * Get number.
  124.      *
  125.      * @return int
  126.      */
  127.     public function getNumber(): int
  128.     {
  129.         return $this->number ?? 1;
  130.     }
  131.     /**
  132.      * Set number.
  133.      *
  134.      * @param int $number
  135.      *
  136.      * @return Channel
  137.      */
  138.     public function setNumber(int $number): self
  139.     {
  140.         $this->number $number;
  141.         return $this;
  142.     }
  143.     /**
  144.      * @return Collection
  145.      */
  146.     public function getProbes(): Collection
  147.     {
  148.         return $this->probes;
  149.     }
  150.     /**
  151.      * @param Probe $probe
  152.      *
  153.      * @return Channel
  154.      */
  155.     public function addProbe(Probe $probe): self
  156.     {
  157.         $this->probes[] = $probe;
  158.         return $this;
  159.     }
  160.     /**
  161.      * Sort probes.
  162.      *
  163.      * @return Channel
  164.      * @throws Exception
  165.      */
  166.     public function sortProbes(): self
  167.     {
  168.         $iterator $this->probes->getIterator();
  169.         $iterator->uasort(function ($first$second) {
  170.             return $first->getTime() > $second->getTime() ? : -1;
  171.         });
  172.         $this->probes = new ArrayCollection(iterator_to_array($iterator));
  173.         return $this;
  174.     }
  175.     /**
  176.      * Filter probes older than.
  177.      *
  178.      * @param DateTime|null $startingTimePoint
  179.      *
  180.      * @return Channel
  181.      */
  182.     public function filterProbesOlderThan(DateTime $startingTimePoint null): self
  183.     {
  184.         if (!$startingTimePoint) {
  185.             return $this;
  186.         }
  187.         foreach ($this->probes as $probe) {
  188.             if ($probe->getTime() < $startingTimePoint) {
  189.                 $this->probes->removeElement($probe);
  190.                 continue;
  191.             }
  192.             return $this;
  193.         }
  194.         return $this;
  195.     }
  196.     /**
  197.      * Update visibilities.
  198.      *
  199.      * @param array $hidden
  200.      *
  201.      * @return Channel
  202.      */
  203.     public function updateVisibilities(array $hidden): self
  204.     {
  205.         /** @var Probe $probe */
  206.         foreach ($this->probes as $probe) {
  207.             $probe->setIsActive(!in_array($probe->getTime()->format('YmdHis'), $hidden));
  208.         }
  209.         return $this;
  210.     }
  211.     /**
  212.      * @return string|null
  213.      */
  214.     public function getName(): ?string
  215.     {
  216.         return $this->name;
  217.     }
  218.     /**
  219.      * @param string|null $name
  220.      *
  221.      * @return $this
  222.      */
  223.     public function setName(?string $name): self
  224.     {
  225.         $this->name $name;
  226.         return $this;
  227.     }
  228.     /**
  229.      * To string.
  230.      *
  231.      * @return string
  232.      */
  233.     public function __toString(): string
  234.     {
  235.         return $this->getNameWithFallback();
  236.     }
  237.     /**
  238.      * Get an extended name.
  239.      *
  240.      * @return string
  241.      */
  242.     public function getNameWithFallback(): string
  243.     {
  244.         return $this->name ?? '{{Channel}} ' $this->number;
  245.     }
  246.     /**
  247.      * With measurement.
  248.      *
  249.      * This method is heavy.
  250.      *
  251.      * @param bool $withMeasurement
  252.      *
  253.      * @return string
  254.      */
  255.     public function getFullName(bool $withMeasurement true): string
  256.     {
  257.         $serial $this->getSerialNumberString();
  258.         return $serial.': '.($this->getNameWithFallback()).($withMeasurement ? (' '.($this->measurement->getNameWithRelativeNumberPerDevice())):'');
  259.     }
  260.     /**
  261.      * Get a sequence name.
  262.      *
  263.      * @param bool $withMeasurement
  264.      *
  265.      * @return string
  266.      */
  267.     public function getSequenceName(bool $withMeasurement true): string
  268.     {
  269.         return ($this->name ?? '{{Sequence}} '.($this->number)).': '.($this->measurement->getDevice()).($withMeasurement ? (' '.($this->measurement->getChannelDate())):'');
  270.     }
  271.     /**
  272.      * Get the sequence name short.
  273.      *
  274.      * @return string
  275.      */
  276.     public function getSequenceShortName(): string
  277.     {
  278.         return $this->getSequenceName(false);
  279.     }
  280.     /**
  281.      * Get channel settings.
  282.      *
  283.      * @return Collection
  284.      */
  285.     public function getChannelSettings(): Collection
  286.     {
  287.         return $this->channelSettings;
  288.     }
  289.     /**
  290.      * @return string
  291.      */
  292.     private function getSerialNumberString(): string
  293.     {
  294.         if (!$this->measurement) {
  295.             return '';
  296.         }
  297.         return $this->measurement->getDevice()->getSerialNumber();
  298.     }
  299. }