Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
16 / 16
30.00% covered (danger)
30.00%
6 / 20
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
API
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
16 / 16
30.00% covered (danger)
30.00%
6 / 20
100.00% covered (success)
100.00%
4 / 4
52.50
100.00% covered (success)
100.00%
1 / 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
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
4
 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
1<?php
2namespace Dbmi\Webservice\Conversion;
3
4use Fdsn\DataStructure\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    protected string $from;
13    protected string $to;
14    protected string $idQuake;
15
16    protected array $reply;
17
18    private int $curlErrNum = 0;
19    private string $curlErrInfo = '';
20
21    abstract public function quake();
22
23    public function curlErrorNumber(){ return $this->curlErrNum; }
24    public function curlErrorInfo(){ return $this->curlErrInfo; }
25
26    /**
27     * Try to find the seismic event in DBMI database and its internal ID
28     *
29     * @return int  number of elements found (or -1 on error)
30     */
31    public function fetch():int{
32        $curlSession = curl_init();
33        curl_setopt_array($curlSession, array(
34            CURLOPT_URL         => sprintf(self::conversionUrlTemplate, $this->from, $this->to, $this->idQuake),
35            CURLOPT_HEADER         => false,
36            CURLOPT_CUSTOMREQUEST    => 'GET',
37            CURLOPT_RETURNTRANSFER    => true,
38            CURLOPT_FAILONERROR     => true,
39            //CURLOPT_VERBOSE        => true
40            )
41        );
42        $downloadedData = curl_exec($curlSession);
43        
44        $this->curlErrNum = curl_errno($curlSession);
45        $this->curlErrInfo = curl_error($curlSession);
46        curl_close($curlSession);
47
48        if(CURLE_OK != $this->curlErrNum) {
49            // @codeCoverageIgnoreStart
50            error_log(sprintf("[%s] [CurlErr: %d] %s", __METHOD__, $this->curlErrNum, $this->curlErrInfo));
51            return -1;
52            // @codeCoverageIgnoreEnd
53        }
54
55        if( empty($downloadedData) ){
56            error_log(sprintf("[%s] Empty response", __METHOD__));
57            return 0;
58        }
59        
60        $this->reply = json_decode($downloadedData, true);
61        if( ! $this->replyIsSet() ){
62            // @codeCoverageIgnoreStart
63            error_log(sprintf("[%s] Invalid reply '%s'", __METHOD__, $downloadedData));
64            return 0;
65            // @codeCoverageIgnoreEnd
66        }
67
68        return 1;
69    }
70
71    protected function replyIsSet():bool{
72        return ( isset($this->reply)
73            && is_array($this->reply) 
74            && array_key_exists('output', $this->reply)
75            && array_key_exists('id', $this->reply['output'])
76            && ! empty($this->reply['output']['id']));
77    }
78
79}
80
81?>