| Total Complexity | 78 |
| Total Lines | 493 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Zend_Gdata_YouTube_VideoQuery often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Zend_Gdata_YouTube_VideoQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class Zend_Gdata_YouTube_VideoQuery extends Zend_Gdata_Query |
||
| 46 | { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Create Gdata_YouTube_VideoQuery object |
||
| 50 | */ |
||
| 51 | public function __construct($url = null) |
||
| 52 | { |
||
| 53 | parent::__construct($url); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Sets the type of feed this query should be used to search |
||
| 58 | * |
||
| 59 | * @param string $feedType The type of feed |
||
| 60 | * @param string $videoId The ID of the video associated with this query |
||
| 61 | * @param string $entry The ID of the entry associated with this query |
||
| 62 | */ |
||
| 63 | public function setFeedType($feedType, $videoId = null, $entry = null) |
||
| 64 | { |
||
| 65 | switch ($feedType) { |
||
| 66 | case 'top rated': |
||
| 67 | $this->_url = Zend_Gdata_YouTube::STANDARD_TOP_RATED_URI; |
||
| 68 | break; |
||
| 69 | case 'most viewed': |
||
| 70 | $this->_url = Zend_Gdata_YouTube::STANDARD_MOST_VIEWED_URI; |
||
| 71 | break; |
||
| 72 | case 'recently featured': |
||
| 73 | $this->_url = Zend_Gdata_YouTube::STANDARD_RECENTLY_FEATURED_URI; |
||
| 74 | break; |
||
| 75 | case 'mobile': |
||
| 76 | $this->_url = Zend_Gdata_YouTube::STANDARD_WATCH_ON_MOBILE_URI; |
||
| 77 | break; |
||
| 78 | case 'related': |
||
| 79 | if ($videoId === null) { |
||
| 80 | require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
||
| 81 | throw new Zend_Gdata_App_InvalidArgumentException( |
||
| 82 | 'Video ID must be set for feed of type: ' . $feedType); |
||
| 83 | } else { |
||
| 84 | $this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' . $videoId . |
||
| 85 | '/related'; |
||
| 86 | } |
||
| 87 | break; |
||
| 88 | case 'responses': |
||
| 89 | if ($videoId === null) { |
||
| 90 | require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
||
| 91 | throw new Zend_Gdata_App_Exception( |
||
| 92 | 'Video ID must be set for feed of type: ' . $feedType); |
||
| 93 | } else { |
||
| 94 | $this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' . $videoId . |
||
| 95 | '/responses'; |
||
| 96 | } |
||
| 97 | break; |
||
| 98 | case 'comments': |
||
| 99 | if ($videoId === null) { |
||
| 100 | require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
||
| 101 | throw new Zend_Gdata_App_Exception( |
||
| 102 | 'Video ID must be set for feed of type: ' . $feedType); |
||
| 103 | } else { |
||
| 104 | $this->_url = Zend_Gdata_YouTube::VIDEO_URI . '/' . |
||
| 105 | $videoId . '/comments'; |
||
| 106 | if ($entry !== null) { |
||
| 107 | $this->_url .= '/' . $entry; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | break; |
||
| 111 | default: |
||
| 112 | require_once 'Zend/Gdata/App/Exception.php'; |
||
| 113 | throw new Zend_Gdata_App_Exception('Unknown feed type'); |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Sets the location parameter for the query |
||
| 120 | * |
||
| 121 | * @param string $value |
||
| 122 | * @throws Zend_Gdata_App_InvalidArgumentException |
||
| 123 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface |
||
| 124 | */ |
||
| 125 | public function setLocation($value) |
||
| 126 | { |
||
| 127 | switch($value) { |
||
| 128 | case null: |
||
| 129 | unset($this->_params['location']); |
||
|
|
|||
| 130 | default: |
||
| 131 | $parameters = explode(',', $value); |
||
| 132 | if (count($parameters) != 2) { |
||
| 133 | require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
||
| 134 | throw new Zend_Gdata_App_InvalidArgumentException( |
||
| 135 | 'You must provide 2 coordinates to the location ' . |
||
| 136 | 'URL parameter'); |
||
| 137 | } |
||
| 138 | |||
| 139 | foreach($parameters as $param) { |
||
| 140 | $temp = trim($param); |
||
| 141 | // strip off the optional exclamation mark for numeric check |
||
| 142 | if (substr($temp, -1) == '!') { |
||
| 143 | $temp = substr($temp, 0, -1); |
||
| 144 | } |
||
| 145 | if (!is_numeric($temp)) { |
||
| 146 | require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
||
| 147 | throw new Zend_Gdata_App_InvalidArgumentException( |
||
| 148 | 'Value provided to location parameter must' . |
||
| 149 | ' be in the form of two coordinates'); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | $this->_params['location'] = $value; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the value of the location parameter |
||
| 158 | * |
||
| 159 | * @return string|null Return the location if it exists, null otherwise. |
||
| 160 | */ |
||
| 161 | public function getLocation() |
||
| 162 | { |
||
| 163 | if (array_key_exists('location', $this->_params)) { |
||
| 164 | return $this->_params['location']; |
||
| 165 | } else { |
||
| 166 | return null; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * Sets the location-radius parameter for the query |
||
| 173 | * |
||
| 174 | * @param string $value |
||
| 175 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface |
||
| 176 | */ |
||
| 177 | public function setLocationRadius($value) |
||
| 178 | { |
||
| 179 | switch($value) { |
||
| 180 | case null: |
||
| 181 | unset($this->_params['location-radius']); |
||
| 182 | default: |
||
| 183 | $this->_params['location-radius'] = $value; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get the value of the location-radius parameter |
||
| 189 | * |
||
| 190 | * @return string|null Return the location-radius if it exists, |
||
| 191 | * null otherwise. |
||
| 192 | */ |
||
| 193 | public function getLocationRadius() |
||
| 194 | { |
||
| 195 | if (array_key_exists('location-radius', $this->_params)) { |
||
| 196 | return $this->_params['location-radius']; |
||
| 197 | } else { |
||
| 198 | return null; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Sets the time period over which this query should apply |
||
| 204 | * |
||
| 205 | * @param string $value |
||
| 206 | * @throws Zend_Gdata_App_InvalidArgumentException |
||
| 207 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface |
||
| 208 | */ |
||
| 209 | public function setTime($value = null) |
||
| 210 | { |
||
| 211 | switch ($value) { |
||
| 212 | case 'today': |
||
| 213 | $this->_params['time'] = 'today'; |
||
| 214 | break; |
||
| 215 | case 'this_week': |
||
| 216 | $this->_params['time'] = 'this_week'; |
||
| 217 | break; |
||
| 218 | case 'this_month': |
||
| 219 | $this->_params['time'] = 'this_month'; |
||
| 220 | break; |
||
| 221 | case 'all_time': |
||
| 222 | $this->_params['time'] = 'all_time'; |
||
| 223 | break; |
||
| 224 | case null: |
||
| 225 | unset($this->_params['time']); |
||
| 226 | default: |
||
| 227 | require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
||
| 228 | throw new Zend_Gdata_App_InvalidArgumentException( |
||
| 229 | 'Unknown time value'); |
||
| 230 | break; |
||
| 231 | } |
||
| 232 | return $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Sets the value of the uploader parameter |
||
| 237 | * |
||
| 238 | * @param string $value The value of the uploader parameter. Currently this |
||
| 239 | * can only be set to the value of 'partner'. |
||
| 240 | * @throws Zend_Gdata_App_InvalidArgumentException |
||
| 241 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface |
||
| 242 | */ |
||
| 243 | public function setUploader($value = null) |
||
| 244 | { |
||
| 245 | switch ($value) { |
||
| 246 | case 'partner': |
||
| 247 | $this->_params['uploader'] = 'partner'; |
||
| 248 | break; |
||
| 249 | case null: |
||
| 250 | unset($this->_params['uploader']); |
||
| 251 | break; |
||
| 252 | default: |
||
| 253 | require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
||
| 254 | throw new Zend_Gdata_App_InvalidArgumentException( |
||
| 255 | 'Unknown value for uploader'); |
||
| 256 | } |
||
| 257 | return $this; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Sets the formatted video query (vq) URL param value |
||
| 262 | * |
||
| 263 | * @param string $value |
||
| 264 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface |
||
| 265 | */ |
||
| 266 | public function setVideoQuery($value = null) |
||
| 267 | { |
||
| 268 | if ($value != null) { |
||
| 269 | $this->_params['vq'] = $value; |
||
| 270 | } else { |
||
| 271 | unset($this->_params['vq']); |
||
| 272 | } |
||
| 273 | return $this; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sets the param to return videos of a specific format |
||
| 278 | * |
||
| 279 | * @param string $value |
||
| 280 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface |
||
| 281 | */ |
||
| 282 | public function setFormat($value = null) |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Sets whether or not to include racy videos in the search results |
||
| 294 | * |
||
| 295 | * @param string $value |
||
| 296 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface |
||
| 297 | */ |
||
| 298 | public function setRacy($value = null) |
||
| 299 | { |
||
| 300 | switch ($value) { |
||
| 301 | case 'include': |
||
| 302 | $this->_params['racy'] = $value; |
||
| 303 | break; |
||
| 304 | case 'exclude': |
||
| 305 | $this->_params['racy'] = $value; |
||
| 306 | break; |
||
| 307 | case null: |
||
| 308 | unset($this->_params['racy']); |
||
| 309 | break; |
||
| 310 | } |
||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Whether or not to include racy videos in the search results |
||
| 316 | * |
||
| 317 | * @return string|null The value of racy if it exists, null otherwise. |
||
| 318 | */ |
||
| 319 | public function getRacy() |
||
| 320 | { |
||
| 321 | if (array_key_exists('racy', $this->_params)) { |
||
| 322 | return $this->_params['racy']; |
||
| 323 | } else { |
||
| 324 | return null; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Set the safeSearch parameter |
||
| 330 | * |
||
| 331 | * @param string $value The value of the parameter, currently only 'none', |
||
| 332 | * 'moderate' or 'strict' are allowed values. |
||
| 333 | * @throws Zend_Gdata_App_InvalidArgumentException |
||
| 334 | * @return Zend_Gdata_YouTube_VideoQuery Provides a fluent interface |
||
| 335 | */ |
||
| 336 | public function setSafeSearch($value) |
||
| 337 | { |
||
| 338 | switch ($value) { |
||
| 339 | case 'none': |
||
| 340 | $this->_params['safeSearch'] = 'none'; |
||
| 341 | break; |
||
| 342 | case 'moderate': |
||
| 343 | $this->_params['safeSearch'] = 'moderate'; |
||
| 344 | break; |
||
| 345 | case 'strict': |
||
| 346 | $this->_params['safeSearch'] = 'strict'; |
||
| 347 | break; |
||
| 348 | case null: |
||
| 349 | unset($this->_params['safeSearch']); |
||
| 350 | default: |
||
| 351 | require_once 'Zend/Gdata/App/InvalidArgumentException.php'; |
||
| 352 | throw new Zend_Gdata_App_InvalidArgumentException( |
||
| 353 | 'The safeSearch parameter only supports the values '. |
||
| 354 | '\'none\', \'moderate\' or \'strict\'.'); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Return the value of the safeSearch parameter |
||
| 360 | * |
||
| 361 | * @return string|null The value of the safeSearch parameter if it has been |
||
| 362 | * set, null otherwise. |
||
| 363 | */ |
||
| 364 | public function getSafeSearch() |
||
| 365 | { |
||
| 366 | if (array_key_exists('safeSearch', $this->_params)) { |
||
| 367 | return $this->_params['safeSearch']; |
||
| 368 | } |
||
| 369 | return $this; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set the value of the orderby parameter |
||
| 374 | * |
||
| 375 | * @param string $value |
||
| 376 | * @return Zend_Gdata_YouTube_Query Provides a fluent interface |
||
| 377 | */ |
||
| 378 | public function setOrderBy($value) |
||
| 379 | { |
||
| 380 | if ($value != null) { |
||
| 381 | $this->_params['orderby'] = $value; |
||
| 382 | } else { |
||
| 383 | unset($this->_params['orderby']); |
||
| 384 | } |
||
| 385 | return $this; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Return the value of the format parameter |
||
| 390 | * |
||
| 391 | * @return string|null The value of format if it exists, null otherwise. |
||
| 392 | */ |
||
| 393 | public function getFormat() |
||
| 394 | { |
||
| 395 | if (array_key_exists('format', $this->_params)) { |
||
| 396 | return $this->_params['format']; |
||
| 397 | } else { |
||
| 398 | return null; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Return the value of the video query that has been set |
||
| 404 | * |
||
| 405 | * @return string|null The value of the video query if it exists, |
||
| 406 | * null otherwise. |
||
| 407 | */ |
||
| 408 | public function getVideoQuery() |
||
| 409 | { |
||
| 410 | if (array_key_exists('vq', $this->_params)) { |
||
| 411 | return $this->_params['vq']; |
||
| 412 | } else { |
||
| 413 | return null; |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Return the value of the time parameter |
||
| 419 | * |
||
| 420 | * @return string|null The time parameter if it exists, null otherwise. |
||
| 421 | */ |
||
| 422 | public function getTime() |
||
| 423 | { |
||
| 424 | if (array_key_exists('time', $this->_params)) { |
||
| 425 | return $this->_params['time']; |
||
| 426 | } else { |
||
| 427 | return null; |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Return the value of the orderby parameter if it exists |
||
| 433 | * |
||
| 434 | * @return string|null The value of orderby if it exists, null otherwise. |
||
| 435 | */ |
||
| 436 | public function getOrderBy() |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Generate the query string from the URL parameters, optionally modifying |
||
| 447 | * them based on protocol version. |
||
| 448 | * |
||
| 449 | * @param integer $majorProtocolVersion The major protocol version |
||
| 450 | * @param integer $minorProtocolVersion The minor protocol version |
||
| 451 | * @throws Zend_Gdata_App_VersionException |
||
| 452 | * @return string querystring |
||
| 453 | */ |
||
| 454 | public function getQueryString($majorProtocolVersion = null, |
||
| 513 | } |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Returns the generated full query URL, optionally modifying it based on |
||
| 518 | * the protocol version. |
||
| 519 | * |
||
| 520 | * @param integer $majorProtocolVersion The major protocol version |
||
| 521 | * @param integer $minorProtocolVersion The minor protocol version |
||
| 522 | * @return string The URL |
||
| 523 | */ |
||
| 524 | public function getQueryUrl($majorProtocolVersion = null, |
||
| 538 | } |
||
| 539 | |||
| 540 | } |
||
| 541 |