Lines 100.00% 9 / 9
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SunPosition 100.00% 9 / 9 100.00% 4 / 4 7 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 isHigherThan 100.00% 1 / 1 100.00% 1 / 1 1
 isWithinAzimuth 100.00% 6 / 6 100.00% 1 / 1 4
 normalizeAzimuth 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts\Sun;
6
7final readonly class SunPosition
8{
9    private const float FULL_TURN_DEGREES = 360.0;
10
11    public function __construct(
12        public float $azimuthDegrees,
13        public float $elevationDegrees,
14    ) {}
15
16    public function isHigherThan(float $elevationDegrees): bool
17    {
18        return $this->elevationDegrees > $elevationDegrees;
19    }
20
21    public function isWithinAzimuth(float $fromDegrees, float $toDegrees): bool
22    {
23        $azimuth = self::normalizeAzimuth($this->azimuthDegrees);
24        $from = self::normalizeAzimuth($fromDegrees);
25        $to = self::normalizeAzimuth($toDegrees);
26
27        if ($from <= $to) {
28            return $azimuth >= $from && $azimuth <= $to;
29        }
30
31        return $azimuth >= $from || $azimuth <= $to;
32    }
33
34    private static function normalizeAzimuth(float $degrees): float
35    {
36        return $degrees - self::FULL_TURN_DEGREES * floor($degrees / self::FULL_TURN_DEGREES);
37    }
38}