Completed
Branch master (c790c5)
by Tom
02:05 queued 25s
created

IndexParser::parseAlert()   D

Complexity

Conditions 23
Paths 43

Size

Total Lines 93
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 81
CRAP Score 23

Importance

Changes 0
Metric Value
cc 23
eloc 82
nc 43
nop 1
dl 0
loc 93
ccs 81
cts 81
cp 1
crap 23
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace NoaaCapAlerts\Parser;
4
5
class IndexParser
6
{
7
    protected $xmlParser;
8
9 3
    function __construct(XmlParser $xmlParser = null)
10
    {
11 3
        if ($xmlParser === null) {
12 2
            $this->xmlParser = new XmlParser();
13
        } else {
14 1
            $this->xmlParser = $xmlParser;
15
        }
16 3
    }
17
18 3
    public function parse(string $xml): array
19
    {
20
        // parse XML into an array of alerts
21 3
        $rawDataArray = $this->xmlParser->getArrayFromXml($xml);
22 2
        $alertDataArray = $rawDataArray[0]['children'];
23
24
        // Process each alert ("ENTRY")
25 2
        $resultArray = array();
26
27 2
        foreach ($alertDataArray as $alert) {
28 2
            if (!$this->isAlert($alert)) {
29 2
                continue;
30
            }
31
32 2
            $parsedAlert = $this->parseAlert($alert);
33
34 2
            $resultArray[] = $parsedAlert;
35
        }
36
37 2
        return $resultArray;
38
    }
39
40 2
    protected function isAlert(array $alert): bool
41
    {
42 2
        return isset($alert['name']) && $alert['name'] == 'ENTRY';
43
    }
44
45 2
    protected function parseAlert(array $alert): array
46
    {
47 2
        $parsedAlert = $this->getDefaultAlertValues();
48
49
        // Loop through attributes and set values
50 2
        foreach ($alert['children'] as $element) {
51 2
            $elementName = $element['name'];
52 2
            $elementAttrs = $element['attrs'];
53 2
            if (isset($element['tagData'])) {
54 2
                $elementData = $element['tagData'];
55
            } else {
56 2
                $elementData = '';
57
            }
58
59
            switch ($elementName) {
60 2
                case 'ID':
61 2
                    $parsedAlert['idString'] = $elementData;
62 2
                    break;
63 2
                case 'UPDATED':
64 2
                    $parsedAlert['updatedDateTime'] = new \DateTime($elementData);
65 2
                    $parsedAlert['updatedTime'] = $parsedAlert['updatedDateTime']->format('Y-m-d H:i:s');
66 2
                    break;
67 2
                case 'PUBLISHED':
68 2
                    $parsedAlert['publishedDateTime'] = new \DateTime($elementData);
69 2
                    $parsedAlert['publishedTime'] = $parsedAlert['publishedDateTime']->format('Y-m-d H:i:s');
70 2
                    break;
71 2
                case 'AUTHOR':
72 2
                    $parsedAlert['authorName'] = $element['children'][0]['tagData'];
73 2
                    break;
74 2
                case 'TITLE':
75 2
                    $parsedAlert['title'] = $elementData;
76 2
                    break;
77 2
                case 'LINK':
78 2
                    $parsedAlert['link'] = $elementAttrs['HREF'];
79 2
                    break;
80 2
                case 'SUMMARY':
81 2
                    $parsedAlert['summary'] = $elementData;
82 2
                    break;
83 2
                case 'CAP:EVENT':
84 2
                    $parsedAlert['capEvent'] = $elementData;
85 2
                    break;
86 2
                case 'CAP:EFFECTIVE':
87 2
                    $effectiveDateTime = new \DateTime($elementData);
88 2
                    $parsedAlert['capEffectiveTime'] = $effectiveDateTime->format('Y-m-d H:i:s');
89 2
                    $parsedAlert['capEffectiveDateTime'] = $effectiveDateTime;
90 2
                    break;
91 2
                case 'CAP:EXPIRES':
92 2
                    $expiresDateTime = new \DateTime($elementData);
93 2
                    $parsedAlert['capExpiresTime'] = $expiresDateTime->format('Y-m-d H:i:s');
94 2
                    $parsedAlert['capExpiresDateTime'] = $expiresDateTime;
95 2
                    break;
96 2
                case 'CAP:STATUS':
97 2
                    $parsedAlert['capStatus'] = $elementData;
98 2
                    break;
99 2
                case 'CAP:MSGTYPE':
100 2
                    $parsedAlert['capMsgType'] = $elementData;
101 2
                    break;
102 2
                case 'CAP:CATEGORY':
103 2
                    $parsedAlert['capCategory'] = $elementData;
104 2
                    break;
105 2
                case 'CAP:URGENCY':
106 2
                    $parsedAlert['capUrgencyExpected'] = $elementData;
107 2
                    break;
108 2
                case 'CAP:SEVERITY':
109 2
                    $parsedAlert['capSeverity'] = $elementData;
110 2
                    break;
111 2
                case 'CAP:CERTAINTY':
112 2
                    $parsedAlert['capCertainty'] = $elementData;
113 2
                    break;
114 2
                case 'CAP:AREADESC':
115 2
                    $parsedAlert['capAreaDesc'] = $elementData;
116 2
                    break;
117 2
                case 'CAP:POLYGON':
118 2
                    $capPolygonString = $elementData;
119 2
                    $parsedAlert['capPolygon'] = explode(' ', $capPolygonString);
120 2
                    break;
121 2
                case 'CAP:GEOCODE':
122 2
                    $geoArray = $this->parseGeoArray($element);
123 2
                    $cleanGeoArray = $this->formatGeoArray($geoArray);
124 2
                    $parsedAlert['capGeoString'] = implode(', ', $geoArray);
125 2
                    $parsedAlert['capGeo'] = $cleanGeoArray;
126 2
                    break;
127 2
                case 'CAP:PARAMETER':
128 2
                    $vtec = $this->parseVtec($element);
129 2
                    $parsedAlert['vtec'] = $vtec;
130 2
                    break;
131
            }
132
133 2
            $parsedAlert['idKey'] = $this->generateIdKey($parsedAlert['idString']);
134
135
        }
136
137 2
        return $parsedAlert;
138
    }
139
140 2
    protected function formatGeoArray(array $geoArray): array
141
    {
142
        // organize array by format type
143
        $locationFormatTypes = array(
144 2
            'FIPS6',
145
            'UGC',
146
        );
147
148 2
        $currentLocationKey = 'null';
149 2
        $geoLocArray = array();
150
151 2
        foreach ($geoArray as $geoLoc) {
152 2
            if (in_array($geoLoc, $locationFormatTypes)) {
153 2
                $currentLocationKey = $geoLoc;
154 2
                $geoLocArray[$geoLoc] = array();
155
            } else {
156 2
                $geoLocArray[$currentLocationKey] = explode(' ', $geoLoc);
157
            }
158
        }
159
160 2
        return $geoLocArray;
161
    }
162
163 2
    protected function generateIdKey(string $idString): string
164
    {
165
        // idString contains important data in it as well.
166
        // Use it to generate a unique key for the alert.
167
        //
168
        // Example:
169
        //    alerts.weather.gov/cap/wwacapget.php?x=AK12539092A414.WinterWeatherAdvisory.125390A09AB0AK.AFGWSWNSB.a59f94b5da45867f6f45272a36df61cc
170
        //
171
        // The pieces of idString appears to be
172
        // 0. State abrev + some strange timestamp format
173
        // 1. Type
174
        // 2. Another timestamp with state abrev.
175
        // 3. ??
176
        // 4. Hash of some data (32 bit)
177
        //
178
        // Since 0,1,2, and 3 aren't unique on their own, but it looks like 4 is, I'll plan on using 0 and 4 just to be sure.
179
180 2
        $idParts = explode('=', $idString);
181 2
        $idSplit = explode('.', $idParts[1]);
182 2
        $idKey = $idSplit[0] . '.' . $idSplit[4];
183
184 2
        return $idKey;
185
    }
186
187
    /**
188
     * @return array
189
     */
190 2
    protected function getDefaultAlertValues(): array
191
    {
192
        $parsedAlert = array(
193 2
            'idString' => '',
194
            'idKey' => '',
195
            'updatedDateTime' => null,
196
            'publishedDateTime' => null,
197
            'updatedTime' => '',
198
            'publishedTime' => '',
199
            'authorName' => '',
200
            'title' => '',
201
            'link' => '',
202
            'summary' => '',
203
            'capEvent' => '',
204
            'capEffectiveTime' => '',
205
            'capExpiresTime' => '',
206
            'capEffectiveDateTime' => null,
207
            'capExpiresDateTime' => null,
208
            'capStatus' => '',
209
            'capMsgType' => '',
210
            'capCategory' => '',
211
            'capUrgencyExpected' => '',
212
            'capSeverity' => '',
213
            'capCertainty' => '',
214
            'capAreaDesc' => '',
215
            'capPolygon' => '',
216
            'capGeo' => array(),
217
            'capGeoString' => '',
218
            'vtec' => '',
219
        );
220 2
        return $parsedAlert;
221
    }
222
223 2
    protected function parseGeoArray(array $element): array
224
    {
225 2
        $geoArray = array();
226
227 2
        foreach ($element['children'] as $geo) {
228 2
            if (isset($geo['tagData'])) {
229 2
                $geoArray[] = $geo['tagData'];
230
            }
231
        }
232
233 2
        return $geoArray;
234
    }
235
236 2
    protected function parseVtec(array $element): string
237
    {
238 2
        if (isset($element['children'][1]['tagData'])) {
239 2
            return $element['children'][1]['tagData'];
240
        }
241
242 1
        return "";
243
    }
244
245
}
246