Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
96.67% covered (success)
96.67%
29 / 30
94.74% covered (success)
94.74%
18 / 19
30.43% covered (danger)
30.43%
7 / 23
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
API
96.67% covered (success)
96.67%
29 / 30
94.74% covered (success)
94.74%
18 / 19
30.43% covered (danger)
30.43%
7 / 23
80.00% covered (warning)
80.00%
4 / 5
69.89
0.00% covered (danger)
0.00%
0 / 1
 quake
n/a
0 / 0
n/a
0 / 0
n/a
0 / 0
n/a
0 / 0
0
 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.45% covered (success)
95.45%
21 / 22
85.71% covered (warning)
85.71%
6 / 7
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
8.12
 replyIsSet
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
9 / 9
12.50% covered (danger)
12.50%
2 / 16
100.00% covered (success)
100.00%
1 / 1
21.75
 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\Conversion;
3
4use Fdsn\Webservices\Event\Structs\LatLon as DS_LatLon;
5
6/**
7 * Library to convert idQuake between systems
8 */
9abstract class API{
10    public const conversionUrlTemplate = 'https://emidius.mi.ingv.it/services/rosetta/?type=event&input=%s&output=%s&format=json&parameters=yes&id=%s';
11
12    private const curlCloceLatestPhpVersion = 7;
13    
14    protected string $from;
15    protected string $to;
16    protected string $idQuake;
17
18    protected array $reply;
19
20    private int $curlErrNum = 0;
21    private string $curlErrInfo = '';
22
23    abstract public function quake();
24
25    public function curlErrorNumber(){ return $this->curlErrNum; }
26    public function curlErrorInfo(){ return $this->curlErrInfo; }
27
28    /**
29     * Try to find the seismic event in DBMI database and its internal ID
30     *
31     * @return int  number of elements found (or -1 on error)
32     */
33    public function fetch():int{
34        $curlSession = curl_init();
35        curl_setopt_array($curlSession, array(
36            CURLOPT_URL         => sprintf(self::conversionUrlTemplate, $this->from, $this->to, $this->idQuake),
37            CURLOPT_HEADER         => false,
38            CURLOPT_CUSTOMREQUEST    => 'GET',
39            CURLOPT_RETURNTRANSFER    => true,
40            CURLOPT_FAILONERROR     => true,
41            //CURLOPT_VERBOSE        => 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        if( empty($downloadedData) ){
59            error_log(sprintf("[%s] Empty response", __METHOD__));
60            return 0;
61        }
62        
63        $this->reply = json_decode($downloadedData, true);
64        if( ! $this->replyIsSet() ){
65            // @codeCoverageIgnoreStart
66            error_log(sprintf("[%s] Invalid reply '%s'", __METHOD__, $downloadedData));
67            return 0;
68            // @codeCoverageIgnoreEnd
69        }
70
71        return 1;
72    }
73
74    protected function replyIsSet():bool{
75        return ( isset($this->reply)
76            && is_array($this->reply) 
77            && array_key_exists('output', $this->reply)
78            && array_key_exists('id', $this->reply['output'])
79            && ! empty($this->reply['output']['id']));
80    }
81
82    /**
83     * Check if curl_close is still supported
84     * @return bool true is supported, false otherwise
85     */
86    private function isCurlCloseSupported():bool{ return PHP_MAJOR_VERSION <= self::curlCloceLatestPhpVersion; }
87
88}
89
90?>