Conditions | 32 |
Paths | 784 |
Total Lines | 186 |
Code Lines | 103 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
35 | public function execute(array &$param_pool = null) |
||
36 | { |
||
37 | $result = new XMLElement($this->dsParamROOTELEMENT); |
||
38 | |||
39 | $this->dsParamURL = $this->parseParamURL($this->dsParamURL); |
||
40 | |||
41 | if (isset($this->dsParamXPATH)) { |
||
42 | $this->dsParamXPATH = $this->__processParametersInString($this->dsParamXPATH, $this->_env); |
||
43 | } |
||
44 | |||
45 | $stylesheet = new XMLElement('xsl:stylesheet'); |
||
46 | $stylesheet->setAttributeArray(array('version' => '1.0', 'xmlns:xsl' => 'http://www.w3.org/1999/XSL/Transform')); |
||
47 | |||
48 | $output = new XMLElement('xsl:output'); |
||
49 | $output->setAttributeArray(array('method' => 'xml', 'version' => '1.0', 'encoding' => 'utf-8', 'indent' => 'yes', 'omit-xml-declaration' => 'yes')); |
||
50 | $stylesheet->appendChild($output); |
||
51 | |||
52 | $template = new XMLElement('xsl:template'); |
||
53 | $template->setAttribute('match', '/'); |
||
54 | |||
55 | $instruction = new XMLElement('xsl:copy-of'); |
||
56 | |||
57 | // Namespaces |
||
58 | if (isset($this->dsParamFILTERS) && is_array($this->dsParamFILTERS)) { |
||
59 | foreach ($this->dsParamFILTERS as $name => $uri) { |
||
60 | $instruction->setAttribute('xmlns' . ($name ? ":{$name}" : null), $uri); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | // XPath |
||
65 | $instruction->setAttribute('select', $this->dsParamXPATH); |
||
66 | |||
67 | $template->appendChild($instruction); |
||
68 | $stylesheet->appendChild($template); |
||
69 | |||
70 | $stylesheet->setIncludeHeader(true); |
||
71 | |||
72 | $xsl = $stylesheet->generate(true); |
||
73 | |||
74 | $cache_id = md5($this->dsParamURL . serialize($this->dsParamFILTERS) . $this->dsParamXPATH); |
||
75 | |||
76 | $cache = new Cacheable(Symphony::Database()); |
||
77 | |||
78 | $cachedData = $cache->read($cache_id); |
||
79 | $writeToCache = false; |
||
80 | $valid = true; |
||
81 | $creation = DateTimeObj::get('c'); |
||
82 | $timeout = (isset($this->dsParamTIMEOUT)) ? (int)max(1, $this->dsParamTIMEOUT) : 6; |
||
83 | |||
84 | // Execute if the cache doesn't exist, or if it is old. |
||
85 | if ( |
||
86 | (!is_array($cachedData) || empty($cachedData)) // There's no cache. |
||
87 | || (time() - $cachedData['creation']) > ($this->dsParamCACHE * 60) // The cache is old. |
||
88 | ) { |
||
89 | if (Mutex::acquire($cache_id, $timeout, TMP)) { |
||
90 | $ch = new Gateway; |
||
91 | $ch->init($this->dsParamURL); |
||
92 | $ch->setopt('TIMEOUT', $timeout); |
||
93 | $ch->setopt('HTTPHEADER', array('Accept: text/xml, */*')); |
||
94 | |||
95 | $data = $ch->exec(); |
||
96 | $info = $ch->getInfoLast(); |
||
97 | |||
98 | Mutex::release($cache_id, TMP); |
||
99 | |||
100 | $data = trim($data); |
||
101 | $writeToCache = true; |
||
102 | |||
103 | // Handle any response that is not a 200, or the content type does not include XML, plain or text |
||
104 | if ((int)$info['http_code'] !== 200 || !preg_match('/(xml|plain|text)/i', $info['content_type'])) { |
||
105 | $writeToCache = false; |
||
106 | |||
107 | $result->setAttribute('valid', 'false'); |
||
108 | |||
109 | // 28 is CURLE_OPERATION_TIMEOUTED |
||
110 | if ($info['curl_error'] == 28) { |
||
111 | $result->appendChild( |
||
112 | new XMLElement('error', |
||
113 | sprintf('Request timed out. %d second limit reached.', $timeout) |
||
114 | ) |
||
115 | ); |
||
116 | } else { |
||
117 | $result->appendChild( |
||
118 | new XMLElement('error', |
||
119 | sprintf('Status code %d was returned. Content-type: %s', $info['http_code'], $info['content_type']) |
||
120 | ) |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | return $result; |
||
125 | |||
126 | // Handle where there is `$data` |
||
127 | } elseif (strlen($data) > 0) { |
||
128 | // If the XML doesn't validate.. |
||
129 | if (!General::validateXML($data, $errors, false, new XsltProcess)) { |
||
130 | $writeToCache = false; |
||
131 | } |
||
132 | |||
133 | // If the `$data` is invalid, return a result explaining why |
||
134 | if ($writeToCache === false) { |
||
135 | $element = new XMLElement('errors'); |
||
136 | |||
137 | $result->setAttribute('valid', 'false'); |
||
138 | |||
139 | $result->appendChild(new XMLElement('error', __('Data returned is invalid.'))); |
||
140 | |||
141 | foreach ($errors as $e) { |
||
142 | if (strlen(trim($e['message'])) == 0) { |
||
143 | continue; |
||
144 | } |
||
145 | |||
146 | $element->appendChild(new XMLElement('item', General::sanitize($e['message']))); |
||
147 | } |
||
148 | |||
149 | $result->appendChild($element); |
||
150 | |||
151 | return $result; |
||
152 | } |
||
153 | |||
154 | // If `$data` is empty, set the `force_empty_result` to true. |
||
155 | } elseif (strlen($data) == 0) { |
||
156 | $this->_force_empty_result = true; |
||
157 | } |
||
158 | |||
159 | // Failed to acquire a lock |
||
160 | } else { |
||
161 | $result->appendChild( |
||
162 | new XMLElement('error', __('The %s class failed to acquire a lock, check that %s exists and is writable.', array( |
||
163 | '<code>Mutex</code>', |
||
164 | '<code>' . TMP . '</code>' |
||
165 | ))) |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | // The cache is good, use it! |
||
170 | } else { |
||
171 | $data = trim($cachedData['data']); |
||
172 | $creation = DateTimeObj::get('c', $cachedData['creation']); |
||
173 | } |
||
174 | |||
175 | // If `$writeToCache` is set to false, invalidate the old cache if it existed. |
||
176 | if (is_array($cachedData) && !empty($cachedData) && $writeToCache === false) { |
||
177 | $data = trim($cachedData['data']); |
||
178 | $valid = false; |
||
179 | $creation = DateTimeObj::get('c', $cachedData['creation']); |
||
180 | |||
181 | if (empty($data)) { |
||
182 | $this->_force_empty_result = true; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | // If `force_empty_result` is false and `$result` is an instance of |
||
187 | // XMLElement, build the `$result`. |
||
188 | if (!$this->_force_empty_result && is_object($result)) { |
||
189 | $proc = new XsltProcess; |
||
190 | $ret = $proc->process($data, $xsl); |
||
191 | |||
192 | if ($proc->isErrors()) { |
||
193 | $result->setAttribute('valid', 'false'); |
||
194 | $error = new XMLElement('error', __('Transformed XML is invalid.')); |
||
195 | $result->appendChild($error); |
||
196 | $element = new XMLElement('errors'); |
||
197 | |||
198 | foreach ($proc->getError() as $e) { |
||
199 | if (strlen(trim($e['message'])) == 0) { |
||
200 | continue; |
||
201 | } |
||
202 | |||
203 | $element->appendChild(new XMLElement('item', General::sanitize($e['message']))); |
||
204 | } |
||
205 | |||
206 | $result->appendChild($element); |
||
207 | } elseif (strlen(trim($ret)) == 0) { |
||
208 | $this->_force_empty_result = true; |
||
209 | } else { |
||
210 | if ($writeToCache) { |
||
211 | $cache->write($cache_id, $data, $this->dsParamCACHE); |
||
212 | } |
||
213 | |||
214 | $result->setValue(PHP_EOL . str_repeat("\t", 2) . preg_replace('/([\r\n]+)/', "$1\t", $ret)); |
||
215 | $result->setAttribute('status', ($valid === true ? 'fresh' : 'stale')); |
||
216 | $result->setAttribute('creation', $creation); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | return $result; |
||
221 | } |
||
223 |