|
4 | 4 |
|
5 | 5 | use Github\Api\AbstractApi; |
6 | 6 | use Github\Exception\MissingArgumentException; |
| 7 | +use Github\Exception\InvalidArgumentException; |
| 8 | +use Github\Exception\ErrorException; |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * @link http://developer.github.com/v3/repos/contents/ |
@@ -81,24 +83,29 @@ public function archive($username, $repository, $format, $reference = null) |
81 | 83 | * @param string $path path to file |
82 | 84 | * @param string $reference reference to a branch or commit |
83 | 85 | * |
84 | | - * @return string content of file |
| 86 | + * @return string|null content of file, or null in case of base64_decode failure |
| 87 | + * |
| 88 | + * @throws InvalidArgumentException If $path is not a file or if its encoding is different from base64 |
| 89 | + * @throws ErrorException If $path doesn't include a 'content' index |
85 | 90 | */ |
86 | 91 | public function download($username, $repository, $path, $reference = null) |
87 | 92 | { |
88 | 93 | $file = $this->show($username, $repository, $path, $reference); |
89 | 94 |
|
90 | 95 | if (!isset($file['type']) || 'file' !== $file['type']) { |
91 | | - throw new \Exception(sprintf('Path "%s" is not a file.', $path)); |
| 96 | + throw new InvalidArgumentException(sprintf('Path "%s" is not a file.', $path)); |
92 | 97 | } |
93 | 98 |
|
94 | 99 | if (!isset($file['content'])) { |
95 | | - throw new \Exception(sprintf('Unable to access "content" for file "%s" (possible keys: "%s").', $path, implode(', ', array_keys($file)))); |
| 100 | + throw new ErrorException(sprintf('Unable to access "content" for file "%s" (possible keys: "%s").', $path, implode(', ', array_keys($file)))); |
96 | 101 | } |
97 | 102 |
|
98 | | - if (!isset($file['encoding']) || 'base64' !== $file['encoding']) { |
99 | | - throw new \Exception(sprintf('Encoding of file "%s" is not supported.', $path)); |
| 103 | + if (!isset($file['encoding'])) { |
| 104 | + throw new InvalidArgumentException(sprintf('Can\t decode contents of file "%s" as no encoding is defined.', $path)); |
| 105 | + } elseif ('base64' !== $file['encoding']) { |
| 106 | + throw new InvalidArgumentException(sprintf('Encoding "%s" of file "%s" is not supported.', $file['encoding'], $path)); |
100 | 107 | } |
101 | 108 |
|
102 | | - return base64_decode($file['content']); |
| 109 | + return base64_decode($file['content']) ?: null; |
103 | 110 | } |
104 | 111 | } |
0 commit comments