Issues (22)

src/IO/GitHub.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * @category Library
4
 * @license MIT http://opensource.org/licenses/MIT
5
 * @link https://github.com/emlynwest/changelog
6
 */
7
8
namespace ChangeLog\IO;
9
10
use stdClass;
11
12
/**
13
 * Allows change log files to be read from GitHub and committed back.
14
 *
15
 * This class has the following config options
16
 * - repo: "<user>/<repo>" The full username and repo name to use.
17
 * - file: Path to the file from the root of the repo
18
 * - token: GitHub API token, can be generated under account -> applications
19
 * - commit_message: Optional commit message, used when setContent() is called.
20
 *
21
 * The token must have the "repo" or "public_repo" permission depending on the visibility of the repo.
22
 */
23
class GitHub extends AbstractGitHubIO
24
{
25
26
	protected $configDefaults = [
27
		'commit_message' => 'Updates change log.',
28
		'line_separator' => "\n",
29
	];
30
31
	/**
32
	 * {@inheritdoc}
33
	 */
34 2
	public function getContent()
35
	{
36 2
		$content = $this->requestFile();
37
38 1
		return explode(
39 1
			$this->getConfig('line_separator'),
0 ignored issues
show
It seems like $this->getConfig('line_separator') can also be of type array; however, parameter $separator of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
			/** @scrutinizer ignore-type */ $this->getConfig('line_separator'),
Loading history...
40 1
			base64_decode($content->content)
41 1
		);
42
	}
43
44
	/**
45
	 * {@inheritdoc}
46
	 */
47 1
	public function setContent($content)
48
	{
49
		// Request the file so we can get its sha
50 1
		$fileInfo = $this->requestFile();
51
52 1
		$encodedContent = base64_encode($content);
53 1
		$data = [
54 1
			'message' => $this->getConfig('commit_message'),
55 1
			'content' => $encodedContent,
56 1
			'sha' => $fileInfo->sha,
57 1
		];
58
59 1
		$api = $this->getApi();
60 1
		$response = $api->put($this->getApiUrl(), $data);
61
		// Parse the response so an exception is thrown if anything goes funky
62 1
		$api->decode($response);
63
	}
64
65
	/**
66
	 * Gets a URL for the GitHub api for our file.
67
	 *
68
	 * @return string
69
	 */
70 2
	protected function getApiUrl()
71
	{
72 2
		$repo = $this->getConfig('repo');
73 2
		$file = $this->getConfig('file');
74
75 2
		$url = "/repos/$repo/contents/$file";
76 2
		return $url;
77
	}
78
79
	/**
80
	 * @return stdClass
81
	 */
82 3
	protected function requestFile()
83
	{
84 3
		$api = $this->getApi();
85 2
		$response = $api->get($this->getApiUrl());
86 2
		$content = $api->decode($response);
87 2
		return $content;
88
	}
89
90
}
91