src/Twig/LiveInfoHelperExtension.php line 86

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Twig;
  7. use App\Service\SiteConfigService;
  8. use Pimcore\Model\DataObject\AlpineCrossingStation;
  9. use Pimcore\Model\DataObject\Fieldcollection\Data\WeatherForecast;
  10. use Pimcore\Model\DataObject\SiteConfig;
  11. use Pimcore\Model\DataObject\Snow;
  12. use Pimcore\Model\DataObject\Warning;
  13. use Pimcore\Model\DataObject\Weather;
  14. use Pimcore\Model\DataObject\Wind;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFunction;
  17. class LiveInfoHelperExtension extends AbstractExtension
  18. {
  19.     private SiteConfig $siteConfig;
  20.     public function __construct(SiteConfigService $siteConfigService)
  21.     {
  22.         $this->siteConfig $siteConfigService->getSiteConfig();
  23.     }
  24.     public function getFunctions(): array
  25.     {
  26.         return [
  27.             new TwigFunction('getEarliestStartTime', [ $this'getEarliestStartTime' ], [ 'is_safe' => [ 'html' ] ]),
  28.             new TwigFunction('getLatestEndTime', [ $this'getLatestEndTime' ], [ 'is_safe' => [ 'html' ] ]),
  29.             new TwigFunction('getAvalancheObject', [ $this'getAvalancheObject' ], [ 'is_safe' => [ 'html' ] ]),
  30.             new TwigFunction('getSnowObject', [ $this'getSnowObject' ], [ 'is_safe' => [ 'html' ] ]),
  31.             new TwigFunction('getTemperature', [ $this'getTemperature' ], [ 'is_safe' => [ 'html' ] ]),
  32.             new TwigFunction('getWindDirectionFromDegree', [ $this'getWindDirectionFromDegree' ], [ 'is_safe' => [ 'html' ] ]),
  33.             new TwigFunction('checkIfWindWarning', [ $this'checkIfWindWarning'], [ 'is_safe' => [ 'html' ] ]),
  34.         ];
  35.     }
  36.     public function getEarliestStartTime(AlpineCrossingStation $station): string
  37.     {
  38.         $earliestStartTime false;
  39.         if ($station->getFacilityToCervinia() && $station->getFacilityToCervinia()->getStart()) {
  40.             $earliestStartTime $station->getFacilityToCervinia()->getStart();
  41.         }
  42.         if ($station->getFacilityToZermatt() && $station->getFacilityToZermatt()->getStart() && (!$earliestStartTime || $station->getFacilityToZermatt()->getStart() < $earliestStartTime)) {
  43.             $earliestStartTime $station->getFacilityToZermatt()->getStart();
  44.         }
  45.         return $earliestStartTime;
  46.     }
  47.     public function getLatestEndTime(AlpineCrossingStation $station): string
  48.     {
  49.         $latestEndTime false;
  50.         if ($station->getFacilityToCervinia() && $station->getFacilityToCervinia()->getEnd()) {
  51.             $latestEndTime $station->getFacilityToCervinia()->getEnd();
  52.         }
  53.         if ($station->getFacilityToZermatt() && $station->getFacilityToZermatt()->getEnd() && $station->getFacilityToZermatt()->getEnd() > $latestEndTime) {
  54.             $latestEndTime $station->getFacilityToZermatt()->getEnd();
  55.         }
  56.         return $latestEndTime;
  57.     }
  58.     public function getAvalancheObject(): bool|Warning
  59.     {
  60.         $warnings = new Warning\Listing();
  61.         $warnings->setOrderKey('avalancheLevel');
  62.         $warnings->setOrder('DESC');
  63.         $warnings->setLimit(1);
  64.         return $warnings->current();
  65.     }
  66.     public function getSnowObject(): Snow
  67.     {
  68.         return $this->siteConfig->getSnowObject();
  69.     }
  70.     public function getTemperature(): string
  71.     {
  72.         $weather $this->siteConfig->getTemperatureObject();
  73.         if ($weather instanceof Wind && $weather->getTemperature()) {
  74.             return $weather->getTemperature();
  75.         }
  76.         if ($weather instanceof Weather) {
  77.             $forecasts $weather->getForecasts();
  78.             $forecastToday $forecasts->getItems()[0];
  79.             if ($forecastToday instanceof WeatherForecast && $forecastToday->getTempMin() && $forecastToday->getTempMax()) {
  80.                 return (string)(($forecastToday->getTempMin() + $forecastToday->getTempMax()) / 2);
  81.             }
  82.         }
  83.         return '';
  84.     }
  85.     public function getWindDirectionFromDegree(int $degree): string
  86.     {
  87.         $cardinalDirections =[
  88.             'N' => [ 348.75360 ],
  89.             'N2' => [ 011.25 ],
  90.             'NNE' => [ 11.2533.75 ],
  91.             'NE' => [ 33.7556.25 ],
  92.             'ENE' => [ 56.2578.75 ],
  93.             'E' => [ 78.75101.25 ],
  94.             'ESE' => [ 101.25123.75 ],
  95.             'SE' => [ 123.75146.25 ],
  96.             'SSE' => [ 146.25168.75 ],
  97.             'S' => [ 168.75191.25 ],
  98.             'SSW' => [ 191.25213.75 ],
  99.             'SW' => [ 213.75236.25 ],
  100.             'WSW' => [ 236.25258.75 ],
  101.             'W' => [ 258.75281.25 ],
  102.             'WNW' => [ 281.25303.75 ],
  103.             'NW' => [ 303.75326.25 ],
  104.             'NNW' => [ 326.25348.75 ],
  105.         ];
  106.         foreach ($cardinalDirections as $direction => $angles) {
  107.             if ($degree >= $angles[0] && $degree $angles[1]) {
  108.                 return str_replace('2'''$direction);
  109.             }
  110.         }
  111.         return '';
  112.     }
  113.     /**
  114.      * @param array<mixed> $windObjects
  115.      *
  116.      * @return float
  117.      */
  118.     public function checkIfWindWarning(array $windObjects): float
  119.     {
  120.         $windForce 0;
  121.         foreach ($windObjects as $windItem) {
  122.             if ($windItem->getWindForce() > 50) {
  123.                 $windForce $windItem->getWindForce() > $windForce $windItem->getWindForce() : $windForce;
  124.             }
  125.         }
  126.         return $windForce;
  127.     }
  128. }