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\DataStructure\Id as DS_Id;
9use Fdsn\DataStructure\LatLon as DS_LatLon;
10
11/**
12 * Fetch ASCII intensity data from HSIT portal
13 * 
14 * @param Fdsn\DataStructure\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 array $places = array();
28
29    public function __construct(DS_Id $fdsnQuakeId, string $macroseismicStudyName, ?bool $fetchMunicipality = true, ?string $localFileFullPath = null){
30        if( ! $this->isValidType(strtolower($macroseismicStudyName)) )
31            throw new \RuntimeException(sprintf("[%s] Type %s not valid", __METHOD__, $macroseismicStudyName));
32
33        $this->idQuake = $fdsnQuakeId;
34        $this->macroseismicStudyName = strtolower($macroseismicStudyName);
35        $this->fetchMunicipality = $fetchMunicipality;
36
37        if( is_null($localFileFullPath) )
38            return;
39
40        if ( ! file_exists($localFileFullPath) || ! is_readable($localFileFullPath) )
41            throw new \RuntimeException(sprintf("[%s] file %s not exists or is not readable", __METHOD__, $localFileFullPath));
42
43        $this->localFileFullPath = $localFileFullPath;
44    }
45
46    /**
47     * get all places intensity
48     * 
49     * @return array locality intensity (array of associative arrays[lat, lon, intensity, reports, placeName] )
50     */
51    public function getIterator():Traversable{ return new ArrayIterator($this->places); }
52    
53    /**
54     * fetch data from url  (remote or local - if $localFileFullPath is set in __construct)
55     * 
56     * @return int number of data found
57     */
58    abstract public function fetch():int;
59
60    /**
61     * fetch data from local path - if $localFileFullPath is set in __construct)
62     * 
63     * @return array data from file, splitted in array of lines
64     */
65    protected function fetchFromLocalPath():string { return file_get_contents( $this->localFileFullPath); }
66
67    /**
68     * fetch data from remote url path - if $localFileFullPath is set NOT in __construct)
69     * 
70     * @return array data from file, splitted in array of lines
71     */
72    protected function fetchFromUrl():string{
73        $callingClass = get_called_class();
74
75        $curlSession = curl_init();
76        curl_setopt_array($curlSession, array(
77            CURLOPT_URL         => sprintf(self::templateUrl, 
78                            $this->idQuake->value(), 
79                            $this->idQuake->value(), 
80                            $this->macroseismicStudyName,
81                            $this->fetchMunicipality ? '' : '_locality',
82                            $callingClass::fileExtension),
83            CURLOPT_HEADER         => false,
84            CURLOPT_CUSTOMREQUEST    => 'GET',
85            CURLOPT_RETURNTRANSFER    => true,
86            CURLOPT_FAILONERROR     => true
87            )
88        );
89        $downloadedData = curl_exec($curlSession);
90        $this->curlErrNum = curl_errno($curlSession);
91        $this->curlErrInfo = curl_error($curlSession);
92        curl_close($curlSession);
93
94        if(CURLE_OK != $this->curlErrNum){
95            error_log(sprintf("[%s] [CurlErr: %d] %s", __METHOD__, $this->curlErrNum, $this->curlErrInfo));
96            return -1;
97        }
98
99        return $downloadedData;
100    }
101
102    /**
103     * check if macroseismic scale study is supported
104     * 
105     * @param string $macroseismicStudyName name of the macroseismic scale study
106     * 
107     * @return bool TRUE if is supported, FALSE otherwise
108     */
109    private function isValidType(string $macroseismicStudyName):bool{ return preg_match("(mcs|ems)", $macroseismicStudyName); }
110
111}
112?>