| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Part of the Joomla Framework Github Package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license    GNU General Public License version 2 or later; see LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Joomla\Github\Package; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Joomla\Github\AbstractPackage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Joomla\Http\Exception\UnexpectedResponseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * GitHub API Gitignore class for the Joomla Framework. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * The .gitignore Templates API lists and fetches templates from the GitHub .gitignore repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @documentation  http://developer.github.com/v3/gitignore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @documentation  https://github.com/github/gitignore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @since  1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class Gitignore extends AbstractPackage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * Listing available templates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * List all templates available to pass as an option when creating a repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * @return  object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @since   1.0 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 	public function getList() | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 		// Build the request path. | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 		$path = '/gitignore/templates'; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 		return $this->processResponse( | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 			$this->client->get($this->fetchUrl($path)) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * Get a single template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @param   string   $name  The name of the template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param   boolean  $raw   Raw output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @return  mixed|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @since   1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * @throws  UnexpectedResponseException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	public function get($name, $raw = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		// Build the request path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$path = '/gitignore/templates/' . $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$headers = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		if ($raw) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$headers['Accept'] = 'application/vnd.github.raw+json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$response = $this->client->get($this->fetchUrl($path), $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		// Validate the response code. | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 71 |  | View Code Duplication | 		if ($response->code != 200) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			// Decode the error response and throw an exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			$error = json_decode($response->body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			$message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			throw new UnexpectedResponseException($response, $message, $response->code); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		return ($raw) ? $response->body : json_decode($response->body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.