| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace FlexyProject\GitHub\Receiver\Repositories; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use FlexyProject\GitHub\AbstractApi; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * The Contents API class provides access to repository's contents. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @link    https://developer.github.com/v3/repos/contents/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package FlexyProject\GitHub\Receiver\Repositories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Contents extends AbstractRepositories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * Get the README | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @link https://developer.github.com/v3/repos/contents/#get-the-readme | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     public function getReadme(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/readme', | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo())); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * This method returns the contents of a file or directory in a repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @link https://developer.github.com/v3/repos/contents/#get-contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $ref | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function getContents(string $path = '', string $ref = AbstractApi::BRANCH_MASTER): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/contents/:path?:args', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             http_build_query(['ref' => $ref]))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Create a file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @link https://developer.github.com/v3/repos/contents/#create-a-file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $branch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function createFile(string $path, string $message, string $content, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                                string $branch = AbstractApi::BRANCH_MASTER): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/contents/:path?:args', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             http_build_query(['message' => $message, 'content' => $content, 'branch' => $branch])), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             Request::METHOD_PUT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * Update a file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @link https://developer.github.com/v3/repos/contents/#update-a-file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param string $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param string $sha | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $branch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 79 |  | View Code Duplication |     public function updateFile(string $path, string $message, string $content, string $sha, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                                string $branch = AbstractApi::BRANCH_MASTER): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/contents/:path?:args', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             http_build_query(['message' => $message, 'content' => $content, 'sha' => $sha, 'branch' => $branch])), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             Request::METHOD_PUT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Delete a file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @link https://developer.github.com/v3/repos/contents/#delete-a-file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param string $sha | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param string $branch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function deleteFile(string $path, string $message, string $sha, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                                string $branch = AbstractApi::BRANCH_MASTER): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/contents/:path', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $path), Request::METHOD_DELETE, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 'message' => $message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 'sha'     => $sha, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 'branch'  => $branch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Get archive link | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @link https://developer.github.com/v3/repos/contents/#get-archive-link | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param string $archiveFormat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param string $ref | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function getArchiveLink(string $archiveFormat = AbstractApi::ARCHIVE_TARBALL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                                    string $ref = AbstractApi::BRANCH_MASTER): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/:archive_format/:ref', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $archiveFormat, $ref)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } | 
            
                        
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.