Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
82.05% covered (warning)
82.05%
32 / 39
72.22% covered (warning)
72.22%
13 / 18
31.25% covered (danger)
31.25%
5 / 16
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SingleInstance
82.05% covered (warning)
82.05%
32 / 39
72.22% covered (warning)
72.22%
13 / 18
31.25% covered (danger)
31.25%
5 / 16
33.33% covered (danger)
33.33%
2 / 6
50.32
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
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
 curlErrorNumber
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
 curlErrorInfo
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
 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
87.88% covered (warning)
87.88%
29 / 33
81.82% covered (warning)
81.82%
9 / 11
20.00% covered (danger)
20.00%
2 / 10
0.00% covered (danger)
0.00%
0 / 1
17.80
 filterEmptyDataLine
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Dbmi\Webservice\Analysis;
3
4use ArrayIterator;
5use IteratorAggregate;
6use Traversable;
7
8use Dbmi\Webservice\Contributor\Struct as DS_Contributor;
9use Dbmi\Webservice\Quake\IdStruct as DS_QuakeId;
10
11use Fdsn\DataStructure\LatLon as DS_LatLon;
12
13/**
14 * Get analysis
15 */
16class SingleInstance implements IteratorAggregate{
17    private const templateUrlDataByContributor = 'https://emidius.mi.ingv.it/services/macroseismic/query?format=textmacro&includemdps=true&eventid=%s&contributor=%s';
18    private const templateUrlDataByContributorAndMdpId = 'https://emidius.mi.ingv.it/services/macroseismic/query?format=textmacro&includemdps=true&eventid=%s&contributor=%s&mdpsetid=%s';
19    
20    private DS_Contributor $contributor;
21    private DS_QuakeId $quakeId;
22
23    private array $localities = array();
24
25    private int $curlErrNum = 0;
26    private string $curlErrInfo = '';
27
28    public function __construct(DS_Contributor $contributor, DS_QuakeId $quakeId){
29        $this->contributor = $contributor;
30        $this->quakeId = $quakeId;
31    }
32
33    public function curlErrorNumber(){ return $this->curlErrNum; }
34    public function curlErrorInfo(){ return $this->curlErrInfo; }
35
36    /**
37     * get all localities intensity
38     * 
39     * @return array locality intensity
40     */
41    public function getIterator():Traversable{ return new ArrayIterator($this->localities); }
42
43    /**
44     * find contributor based on quakeId
45     *
46     * @return int number of studies found
47     */
48    public function fetch():int{
49        $url = ( ! is_null ($this->contributor->mdpSetId()) ) 
50            ? sprintf(self::templateUrlDataByContributorAndMdpId, $this->quakeId->id(), $this->contributor->name(), $this->contributor->mdpSetId() )
51            : sprintf(self::templateUrlDataByContributor, $this->quakeId->id(), $this->contributor->name() );
52
53        $curlSession = curl_init();
54        curl_setopt_array($curlSession, array(
55            CURLOPT_URL         => $url,
56            CURLOPT_HEADER         => false,
57            CURLOPT_CUSTOMREQUEST    => 'GET',
58            CURLOPT_RETURNTRANSFER    => true,
59            CURLOPT_FAILONERROR     => true
60            )
61        );
62        $downloadedData = curl_exec($curlSession);
63
64        $this->curlErrNum = curl_errno($curlSession);
65        $this->curlErrInfo = curl_error($curlSession);
66        curl_close($curlSession);
67
68        if(CURLE_OK != $this->curlErrNum){
69            error_log(sprintf("[%s] [CurlErr: %d] %s", __METHOD__, $this->curlErrNum, $this->curlErrInfo));
70            return -1;
71        }
72
73        $stringsData = preg_split("/\n/", $downloadedData);
74        $stringsArray = array_filter($stringsData, array($this, 'filterEmptyDataLine'));
75
76        if(0 == count($stringsArray)){
77            error_log(sprintf("[%s] No data\n'%s'", __METHOD__, $stringsData));
78            return 0;
79        }
80        
81        foreach($stringsArray as $data){    
82            $dataArray = str_getcsv($data, '|');
83            
84            $this->localities[] = new Locality(
85                $dataArray[8],
86                $dataArray[9],
87                new DS_LatLon($dataArray[10], $dataArray[11]),
88                new Intensity($dataArray[12])
89            );
90        }
91
92        return count($this->localities);
93    }
94
95    /**
96     * filter empty lines or comment lines (starting with #)
97     */
98    private function filterEmptyDataLine($data){
99        return !(preg_match('/^$/', $data) || preg_match("/^#/", $data));
100    }
101
102}
103
104?>
105