Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
32 / 36
83.33% covered (warning)
83.33%
15 / 18
53.85% covered (warning)
53.85%
7 / 13
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Intensity
88.89% covered (warning)
88.89%
32 / 36
83.33% covered (warning)
83.33%
15 / 18
53.85% covered (warning)
53.85%
7 / 13
40.00% covered (danger)
40.00%
2 / 5
22.90
0.00% covered (danger)
0.00%
0 / 1
 __construct
90.00% covered (success)
90.00%
9 / 10
88.89% covered (warning)
88.89%
8 / 9
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
8.12
 getIterator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fetch
n/a
0 / 0
n/a
0 / 0
n/a
0 / 0
n/a
0 / 0
0
 fetchFromLocalPath
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
 fetchFromUrl
91.30% covered (success)
91.30%
21 / 23
83.33% covered (warning)
83.33%
5 / 6
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
4.12
 isValidType
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 Hsit\Webservice\Event;
3
4use ArrayIterator;
5use IteratorAggregate;
6use Traversable;
7
8use Fdsn\Webservices\Event\Structs\Id as DS_Id;
9use Fdsn\Webservices\Event\Structs\LatLon as DS_LatLon;
10
11/**
12 * Fetch ASCII intensity data from HSIT portal
13 * 
14 * @param Fdsn\Webservices\Event\Structs\Id $fdsnQuakeId Id quake to scan for data
15 * @param string $macroseismicStudyName macroseismic scale study (now only mcs|ems are supported)
16 * @param bool $fetchMunicipality TRUE fetch municipality data, FALSE fetch locality data. Default is TRUE
17 * @param string $localFileFullPath path to fetch local file  (if exists AND is readable)
18 */
19abstract class Intensity implements IteratorAggregate {
20    protected const templateUrl = 'https://e.hsit.it/%d/%d_%s%s.%s';
21
22    protected DS_Id $idQuake;
23    protected string $macroseismicStudyName;
24    protected bool $fetchMunicipality = true;
25    protected ?string $localFileFullPath = null;
26
27    protected int $curlErrNum;
28    protected string $curlErrInfo;
29
30    protected array $places = array();
31
32    public function __construct(DS_Id $fdsnQuakeId, string $macroseismicStudyName, ?bool $fetchMunicipality = true, ?string $localFileFullPath = null){
33        if( ! $this->isValidType(strtolower($macroseismicStudyName)) )
34            throw new \RuntimeException(sprintf("[%s] Type %s not valid", __METHOD__, $macroseismicStudyName));
35
36        $this->idQuake = $fdsnQuakeId;
37        $this->macroseismicStudyName = strtolower($macroseismicStudyName);
38        $this->fetchMunicipality = $fetchMunicipality;
39
40        if( is_null($localFileFullPath) )
41            return;
42
43        if ( ! file_exists($localFileFullPath) || ! is_readable($localFileFullPath) )
44            throw new \RuntimeException(sprintf("[%s] file %s not exists or is not readable", __METHOD__, $localFileFullPath));
45
46        $this->localFileFullPath = $localFileFullPath;
47    }
48
49    /**
50     * get all places intensity
51     * 
52     * @return array locality intensity (array of associative arrays[lat, lon, intensity, reports, placeName] )
53     */
54    public function getIterator():Traversable{ return new ArrayIterator($this->places); }
55    
56    /**
57     * fetch data from url  (remote or local - if $localFileFullPath is set in __construct)
58     * 
59     * @return int number of data found
60     */
61    abstract public function fetch():int;
62
63    /**
64     * fetch data from local path - if $localFileFullPath is set in __construct)
65     * 
66     * @return array data from file, splitted in array of lines
67     */
68    protected function fetchFromLocalPath():string { return file_get_contents( $this->localFileFullPath); }
69
70    /**
71     * fetch data from remote url path - if $localFileFullPath is set NOT in __construct)
72     * 
73     * @return array data from file, splitted in array of lines
74     */
75    protected function fetchFromUrl():string{
76        $callingClass = get_called_class();
77
78        $curlSession = curl_init();
79        curl_setopt_array($curlSession, array(
80            CURLOPT_URL         => sprintf(self::templateUrl, 
81                            $this->idQuake->value(), 
82                            $this->idQuake->value(), 
83                            $this->macroseismicStudyName,
84                            $this->fetchMunicipality ? '_municipality' : '_locality',
85                            $callingClass::fileExtension),
86            CURLOPT_HEADER         => false,
87            CURLOPT_CUSTOMREQUEST    => 'GET',
88            CURLOPT_RETURNTRANSFER    => true,
89            CURLOPT_FAILONERROR     => true
90            )
91        );
92        $downloadedData = curl_exec($curlSession);
93        $this->curlErrNum = curl_errno($curlSession);
94        $this->curlErrInfo = curl_error($curlSession);
95        curl_close($curlSession);
96
97        if(CURLE_OK != $this->curlErrNum){
98            error_log(sprintf("[%s] [CurlErr: %d] %s", __METHOD__, $this->curlErrNum, $this->curlErrInfo));
99            return -1;
100        }
101
102        return $downloadedData;
103    }
104
105    /**
106     * check if macroseismic scale study is supported
107     * 
108     * @param string $macroseismicStudyName name of the macroseismic scale study
109     * 
110     * @return bool TRUE if is supported, FALSE otherwise
111     */
112    private function isValidType(string $macroseismicStudyName):bool{ return preg_match("(mcs|ems)", $macroseismicStudyName); }
113
114}
115?>