Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
24 / 24 |
|
100.00% |
8 / 8 |
|
100.00% |
6 / 6 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| SingleInstance | |
100.00% |
24 / 24 |
|
100.00% |
8 / 8 |
|
100.00% |
6 / 6 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| details | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| curlErrorNumber | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| curlErrorInfo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fetch | |
100.00% |
20 / 20 |
|
100.00% |
4 / 4 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Dbmi\Webservice\Quake; |
| 3 | |
| 4 | use Dbmi\Webservice\Quake\IdStruct as DS_QuakeId; |
| 5 | |
| 6 | /** |
| 7 | * Get quake details |
| 8 | */ |
| 9 | class SingleInstance{ |
| 10 | private const templateUrl = 'https://emidius.mi.ingv.it/services/macroseismic/query?eventid=%s&format=text'; |
| 11 | private const validResponseLines = 2; |
| 12 | |
| 13 | private DS_QuakeId $quakeId; |
| 14 | private array $details = array(); |
| 15 | |
| 16 | private int $curlErrNum = 0; |
| 17 | private string $curlErrInfo = ''; |
| 18 | |
| 19 | |
| 20 | public function __construct(DS_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 | curl_close($curlSession); |
| 49 | |
| 50 | if(CURLE_OK != $this->curlErrNum){ |
| 51 | // @codeCoverageIgnoreStart |
| 52 | error_log(sprintf("[%s] [CurlErr: %d] %s", __METHOD__, $this->curlErrNum, $this->curlErrInfo)); |
| 53 | return -1; |
| 54 | // @codeCoverageIgnoreEnd |
| 55 | } |
| 56 | |
| 57 | $rows = preg_split("/\n/", trim($downloadedData)); |
| 58 | |
| 59 | if( count($rows) != self::validResponseLines ){ //header+data |
| 60 | error_log(sprintf("[%s] Invalid epicenter response, see below\n'%s'", __METHOD__, $downloadedData)); |
| 61 | return -2; |
| 62 | } |
| 63 | |
| 64 | $this->details = preg_split("/\|/", $rows[1]); |
| 65 | |
| 66 | return 1; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | ?> |
| 71 |