Lines 100.00% 17 / 17
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SunEvent 100.00% 17 / 17 100.00% 4 / 4 17 100.00% 1 / 1
 findElevationCrossing 100.00% 14 / 14 100.00% 1 / 1 14
 describe 100.00% 1 / 1 100.00% 1 / 1 1
 createRising 100.00% 1 / 1 100.00% 1 / 1 1
 createSetting 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts\Sun;
6
7enum SunEvent: string
8{
9    public const float SUN_RADIUS_DEGREES = 32.0 / 120.0;
10    private const float CIVIL_TWILIGHT_DEGREES = -6.0;
11    private const float NAUTICAL_TWILIGHT_DEGREES = -12.0;
12    private const float ASTRONOMICAL_TWILIGHT_DEGREES = -18.0;
13    private const float GOLDEN_HOUR_UPPER_DEGREES = 6.0;
14    private const float BLUE_HOUR_UPPER_DEGREES = -4.0;
15
16    case AstronomicalDawn = 'astronomical_dawn';
17    case NauticalDawn = 'nautical_dawn';
18    case CivilDawn = 'civil_dawn';
19    case BlueHourMorningStart = 'blue_hour_morning_start';
20    case BlueHourMorningEnd = 'blue_hour_morning_end';
21    case Sunrise = 'sunrise';
22    case GoldenHourMorningEnd = 'golden_hour_morning_end';
23    case SolarNoon = 'solar_noon';
24    case GoldenHourEveningStart = 'golden_hour_evening_start';
25    case Sunset = 'sunset';
26    case BlueHourEveningStart = 'blue_hour_evening_start';
27    case BlueHourEveningEnd = 'blue_hour_evening_end';
28    case CivilDusk = 'civil_dusk';
29    case NauticalDusk = 'nautical_dusk';
30    case AstronomicalDusk = 'astronomical_dusk';
31
32    public function findElevationCrossing(): ?SunElevationCrossing
33    {
34        return match ($this) {
35            self::AstronomicalDawn => self::createRising(self::ASTRONOMICAL_TWILIGHT_DEGREES),
36            self::NauticalDawn => self::createRising(self::NAUTICAL_TWILIGHT_DEGREES),
37            self::CivilDawn, self::BlueHourMorningStart => self::createRising(self::CIVIL_TWILIGHT_DEGREES),
38            self::BlueHourMorningEnd => self::createRising(self::BLUE_HOUR_UPPER_DEGREES),
39            self::Sunrise => self::createRising(-self::SUN_RADIUS_DEGREES),
40            self::GoldenHourMorningEnd => self::createRising(self::GOLDEN_HOUR_UPPER_DEGREES),
41            self::SolarNoon => null,
42            self::GoldenHourEveningStart => self::createSetting(self::GOLDEN_HOUR_UPPER_DEGREES),
43            self::Sunset => self::createSetting(-self::SUN_RADIUS_DEGREES),
44            self::BlueHourEveningStart => self::createSetting(self::BLUE_HOUR_UPPER_DEGREES),
45            self::BlueHourEveningEnd, self::CivilDusk => self::createSetting(self::CIVIL_TWILIGHT_DEGREES),
46            self::NauticalDusk => self::createSetting(self::NAUTICAL_TWILIGHT_DEGREES),
47            self::AstronomicalDusk => self::createSetting(self::ASTRONOMICAL_TWILIGHT_DEGREES),
48        };
49    }
50
51    public function describe(): string
52    {
53        return str_replace('_', ' ', $this->value);
54    }
55
56    private static function createRising(float $elevationDegrees): SunElevationCrossing
57    {
58        return new SunElevationCrossing($elevationDegrees, SunDirection::Rising);
59    }
60
61    private static function createSetting(float $elevationDegrees): SunElevationCrossing
62    {
63        return new SunElevationCrossing($elevationDegrees, SunDirection::Setting);
64    }
65}