Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
25 / 26
90.91% covered (success)
90.91%
10 / 11
77.78% covered (warning)
77.78%
7 / 9
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Quake
96.15% covered (success)
96.15%
25 / 26
90.91% covered (success)
90.91%
10 / 11
77.78% covered (warning)
77.78%
7 / 9
83.33% covered (warning)
83.33%
5 / 6
9.89
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 details
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 curlErrorNumber
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 curlErrorInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fetch
95.24% covered (success)
95.24%
20 / 21
83.33% covered (warning)
83.33%
5 / 6
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
6.00
 isCurlCloseSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Dbmi\Webservice\Quake;
3
4/**
5 * Get quake details
6 */
7class Quake{
8    private const templateUrl = 'https://emidius.mi.ingv.it/services/macroseismic/query?eventid=%s&format=text';
9    private const validResponseLines = 2;
10
11    private const curlCloceLatestPhpVersion = 7;
12
13    private QuakeId $quakeId;
14    private array $details = array();
15
16    private int $curlErrNum = 0;
17    private string $curlErrInfo = '';
18
19
20    public function __construct(QuakeId $quakeId){
21        $this->quakeId = $quakeId;
22    }
23
24    public function details():array{ return $this->details; } 
25
26    public function curlErrorNumber():int{ return $this->curlErrNum; }
27    public function curlErrorInfo():string{ return $this->curlErrInfo; }
28    
29    /**
30     * Get quake epicentral details
31     *
32     * @return int     found lines
33     */
34    public function fetch():int{
35        $curlSession = curl_init();
36        curl_setopt_array($curlSession, array(
37            CURLOPT_URL         => sprintf(self::templateUrl, $this->quakeId->id()),
38            CURLOPT_HEADER         => false,
39            CURLOPT_CUSTOMREQUEST    => 'GET',
40            CURLOPT_RETURNTRANSFER    => true,
41            CURLOPT_FAILONERROR     => true
42            )
43        );
44        $downloadedData = curl_exec($curlSession);
45
46        $this->curlErrNum = curl_errno($curlSession);
47        $this->curlErrInfo = curl_error($curlSession);
48        if ( $this->isCurlCloseSupported() )
49            curl_close($curlSession);
50        
51        if(CURLE_OK != $this->curlErrNum){
52            // @codeCoverageIgnoreStart
53            error_log(sprintf("[%s] [CurlErr: %d] %s", __METHOD__, $this->curlErrNum, $this->curlErrInfo));
54            return -1;
55            // @codeCoverageIgnoreEnd
56        }
57
58        $rows = preg_split("/\n/", trim($downloadedData));
59
60        if( count($rows) != self::validResponseLines ){ //header+data
61            error_log(sprintf("[%s] Invalid epicenter response, see below\n'%s'", __METHOD__, $downloadedData));
62            return -2;
63        }    
64
65        $this->details = preg_split("/\|/", $rows[1]);
66
67        return 1;
68    }
69
70    /**
71     * Check if curl_close is still supported
72     * @return bool true is supported, false otherwise
73     */
74    private function isCurlCloseSupported():bool{ return PHP_MAJOR_VERSION <= self::curlCloceLatestPhpVersion; }
75}
76
77?>
78