GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Calculator::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
cc 3
eloc 6
nc 2
nop 1
crap 3.0416
1
<?php
2
3
namespace GeoTimeZone;
4
5
use DateTime;
6
use DateTimeZone;
7
use ErrorException;
8
use GeoTimeZone\Quadrant\Tree;
9
10
class Calculator
11
{
12
    protected $quadrantTree;
13
    
14
    /**
15
     * TimeZone constructor.
16
     * @param $dataDirectory
17
     * @throws ErrorException
18
     */
19 25
    public function __construct($dataDirectory = null)
20
    {
21 25
        if (isset($dataDirectory) && is_dir($dataDirectory)) {
22 25
            $this->quadrantTree = new Tree($dataDirectory);
23 25
            $this->quadrantTree->initializeDataTree();
24
        }else{
25
            throw new ErrorException('Invalid data tree directory: ' . $dataDirectory);
26
        }
27 25
    }
28
    
29
    /**
30
     * Adjust the latitude value
31
     * @param $latitude
32
     * @return float|int
33
     * @throws ErrorException
34
     */
35 23
    protected function adjustLatitude($latitude)
36
    {
37 23
        $newLatitude = $latitude;
38 23
        if (null == $latitude || abs($latitude) > Tree::MAX_ABS_LATITUDE) {
39 2
            throw new ErrorException('Invalid latitude: ' . $latitude);
40
        }
41 21
        if (abs($latitude) == Tree::MAX_ABS_LATITUDE) {
42 4
            $newLatitude = ($latitude <=> 0) * Tree::ABS_LATITUDE_LIMIT;
43
        }
44 21
        return $newLatitude;
45
    }
46
    
47
    /**
48
     * Adjust longitude value
49
     * @param $longitude
50
     * @return float|int
51
     * @throws ErrorException
52
     */
53 21
    protected function adjustLongitude($longitude)
54
    {
55 21
        $newLongitude = $longitude;
56 21
        if (null == $longitude || abs($longitude) > Tree::MAX_ABS_LONGITUDE) {
57 2
            throw new ErrorException('Invalid longitude: ' . $longitude);
58
        }
59 19
        if (abs($longitude) == Tree::MAX_ABS_LONGITUDE) {
60 4
            $newLongitude = ($longitude <=> 0) * Tree::ABS_LONGITUDE_LIMIT;
61
        }
62 19
        return $newLongitude;
63
    }
64
    
65
    /**
66
     * Get timezone name from a particular location (latitude, longitude)
67
     * @param $latitude
68
     * @param $longitude
69
     * @return string
70
     * @throws ErrorException
71
     */
72 21
    public function getTimeZoneName($latitude, $longitude)
73
    {
74 21
        $timeZone = Tree::NONE_TIMEZONE;
75
        try {
76 21
            $latitude = $this->adjustLatitude($latitude);
77 19
            $longitude = $this->adjustLongitude($longitude);
78 17
            $timeZone = $this->quadrantTree->lookForTimezone($latitude, $longitude);
79 11
        }catch (ErrorException $error){
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
80 11
            throw $error;
81
        }
82 10
        return $timeZone;
83
    }
84
    
85
    /**
86
     * Get the local date belonging to a particular latitude, longitude and timestamp
87
     * @param $latitude
88
     * @param $longitude
89
     * @param $timestamp
90
     * @return DateTime
91
     * @throws ErrorException
92
     */
93 7
    public function getLocalDate($latitude, $longitude, $timestamp)
94
    {
95 7
        $date = new DateTime();
96
        try {
97 7
            $timeZone = $this->getTimeZoneName($latitude, $longitude);
98 6
            $date->setTimestamp($timestamp);
99 6
            if ($timeZone != Tree::NONE_TIMEZONE) {
100 6
                $date->setTimezone(new DateTimeZone($timeZone));
101
            }
102 1
        }catch (ErrorException $error){
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
103 1
            throw $error;
104
        }
105 6
        return $date;
106
    }
107
    
108
    /**
109
     * Get timestamp from latitude, longitude and localTimestamp
110
     * @param $latitude
111
     * @param $longitude
112
     * @param $localTimestamp
113
     * @return mixed
114
     * @throws ErrorException
115
     */
116 3
    public function getCorrectTimestamp($latitude, $longitude, $localTimestamp)
117
    {
118 3
        $timestamp = $localTimestamp;
119
        try {
120 3
            $timeZoneName = $this->getTimeZoneName($latitude, $longitude);
121 2
            if ($timeZoneName != Tree::NONE_TIMEZONE) {
122 2
                $date = new DateTime();
123 2
                $date->setTimestamp($localTimestamp);
124 2
                if ($timeZoneName != null) {
125 2
                    $date->setTimezone(new DateTimeZone($timeZoneName));
126
                }
127 2
                $timestamp = $date->getOffset() != false ? $localTimestamp - $date->getOffset() : $localTimestamp;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $date->getOffset() of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
128
            }
129 1
        }catch(ErrorException $error){
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
130 1
            throw $error;
131
        }
132 2
        return $timestamp;
133
    }
134
}
135
136