<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Twig;
use App\Service\SiteConfigService;
use Pimcore\Model\DataObject\AlpineCrossingStation;
use Pimcore\Model\DataObject\Fieldcollection\Data\WeatherForecast;
use Pimcore\Model\DataObject\SiteConfig;
use Pimcore\Model\DataObject\Snow;
use Pimcore\Model\DataObject\Warning;
use Pimcore\Model\DataObject\Weather;
use Pimcore\Model\DataObject\Wind;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class LiveInfoHelperExtension extends AbstractExtension
{
private SiteConfig $siteConfig;
public function __construct(SiteConfigService $siteConfigService)
{
$this->siteConfig = $siteConfigService->getSiteConfig();
}
public function getFunctions(): array
{
return [
new TwigFunction('getEarliestStartTime', [ $this, 'getEarliestStartTime' ], [ 'is_safe' => [ 'html' ] ]),
new TwigFunction('getLatestEndTime', [ $this, 'getLatestEndTime' ], [ 'is_safe' => [ 'html' ] ]),
new TwigFunction('getAvalancheObject', [ $this, 'getAvalancheObject' ], [ 'is_safe' => [ 'html' ] ]),
new TwigFunction('getSnowObject', [ $this, 'getSnowObject' ], [ 'is_safe' => [ 'html' ] ]),
new TwigFunction('getTemperature', [ $this, 'getTemperature' ], [ 'is_safe' => [ 'html' ] ]),
new TwigFunction('getWindDirectionFromDegree', [ $this, 'getWindDirectionFromDegree' ], [ 'is_safe' => [ 'html' ] ]),
new TwigFunction('checkIfWindWarning', [ $this, 'checkIfWindWarning'], [ 'is_safe' => [ 'html' ] ]),
];
}
public function getEarliestStartTime(AlpineCrossingStation $station): string
{
$earliestStartTime = false;
if ($station->getFacilityToCervinia() && $station->getFacilityToCervinia()->getStart()) {
$earliestStartTime = $station->getFacilityToCervinia()->getStart();
}
if ($station->getFacilityToZermatt() && $station->getFacilityToZermatt()->getStart() && (!$earliestStartTime || $station->getFacilityToZermatt()->getStart() < $earliestStartTime)) {
$earliestStartTime = $station->getFacilityToZermatt()->getStart();
}
return $earliestStartTime;
}
public function getLatestEndTime(AlpineCrossingStation $station): string
{
$latestEndTime = false;
if ($station->getFacilityToCervinia() && $station->getFacilityToCervinia()->getEnd()) {
$latestEndTime = $station->getFacilityToCervinia()->getEnd();
}
if ($station->getFacilityToZermatt() && $station->getFacilityToZermatt()->getEnd() && $station->getFacilityToZermatt()->getEnd() > $latestEndTime) {
$latestEndTime = $station->getFacilityToZermatt()->getEnd();
}
return $latestEndTime;
}
public function getAvalancheObject(): bool|Warning
{
$warnings = new Warning\Listing();
$warnings->setOrderKey('avalancheLevel');
$warnings->setOrder('DESC');
$warnings->setLimit(1);
return $warnings->current();
}
public function getSnowObject(): Snow
{
return $this->siteConfig->getSnowObject();
}
public function getTemperature(): string
{
$weather = $this->siteConfig->getTemperatureObject();
if ($weather instanceof Wind && $weather->getTemperature()) {
return $weather->getTemperature();
}
if ($weather instanceof Weather) {
$forecasts = $weather->getForecasts();
$forecastToday = $forecasts->getItems()[0];
if ($forecastToday instanceof WeatherForecast && $forecastToday->getTempMin() && $forecastToday->getTempMax()) {
return (string)(($forecastToday->getTempMin() + $forecastToday->getTempMax()) / 2);
}
}
return '';
}
public function getWindDirectionFromDegree(int $degree): string
{
$cardinalDirections =[
'N' => [ 348.75, 360 ],
'N2' => [ 0, 11.25 ],
'NNE' => [ 11.25, 33.75 ],
'NE' => [ 33.75, 56.25 ],
'ENE' => [ 56.25, 78.75 ],
'E' => [ 78.75, 101.25 ],
'ESE' => [ 101.25, 123.75 ],
'SE' => [ 123.75, 146.25 ],
'SSE' => [ 146.25, 168.75 ],
'S' => [ 168.75, 191.25 ],
'SSW' => [ 191.25, 213.75 ],
'SW' => [ 213.75, 236.25 ],
'WSW' => [ 236.25, 258.75 ],
'W' => [ 258.75, 281.25 ],
'WNW' => [ 281.25, 303.75 ],
'NW' => [ 303.75, 326.25 ],
'NNW' => [ 326.25, 348.75 ],
];
foreach ($cardinalDirections as $direction => $angles) {
if ($degree >= $angles[0] && $degree < $angles[1]) {
return str_replace('2', '', $direction);
}
}
return '';
}
/**
* @param array<mixed> $windObjects
*
* @return float
*/
public function checkIfWindWarning(array $windObjects): float
{
$windForce = 0;
foreach ($windObjects as $windItem) {
if ($windItem->getWindForce() > 50) {
$windForce = $windItem->getWindForce() > $windForce ? $windItem->getWindForce() : $windForce;
}
}
return $windForce;
}
}