Passed
Push — main ( b13b1c...30676d )
by Emlyn
12:24
created

GitHubReleases   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 64
ccs 19
cts 19
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getApiUrl() 0 6 1
A getContent() 0 27 2
A setContent() 0 4 1
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
/**
11
 * Allows change log files to be generated from the GitHub release api.
12
 *
13
 * This class has the following config options.
14
 * - repo: "<user>/<repo>" The full username and repo name to use.
15
 * - token: GitHub API token, can be generated under account -> applications.
16
 * - log_title: Optional name of the change log.
17
 *
18
 * The token must have the "repo" or "public_repo" permission depending on the visibility of the repo.
19
 */
20
class GitHubReleases extends AbstractGitHubIO
21
{
22
23
	protected $configDefaults = [
24
		'log_title' => 'GitHub Releases',
25
	];
26
27
	/**
28
	 * Returns the content of the change log to be parsed.
29
	 * The returned data should be an array of strings, one entry for each file line.
30
	 *
31
	 * @return array
32
	 */
33 1
	public function getContent()
34
	{
35 1
		$api = $this->getApi();
36 1
		$response = $api->get($this->getApiUrl());
37 1
		$content = $api->decode($response);
38
39 1
		$links = [];
40
41 1
		$title = $this->getConfig('log_title');
42 1
		$log = "# $title\n\n";
43
44 1
		foreach($content as $release)
45
		{
46
			// published_at for release date
47 1
			$date = substr($release->published_at, 0, 10);
48
49
			// tag_name for the release title
50 1
			$log .= "## [{$release->tag_name}] - $date\n";
51
52
			// body for the release changes
53 1
			$log .= $release->body."\n";
54
55
			// html_url for links added at the end
56 1
			$links[] = "[{$release->tag_name}]: {$release->html_url}";
57
		}
58
59 1
		return explode("\n", $log . "\n" . implode("\n", $links));
60
	}
61
62
	/**
63
	 * Writes out the given content,
64
	 *
65
	 * @param string $content
66
	 */
67 1
	public function setContent($content)
68
	{
69
		// TODO: Implement setContent() method.
70 1
		throw new \Exception('This has yet to be implemented.');
71
	}
72
73
	/**
74
	 * Gets a URL for the GitHub api for our file.
75
	 *
76
	 * @return string
77
	 */
78 1
	protected function getApiUrl()
79
	{
80 1
		$repo = $this->getConfig('repo');
81
82 1
		$url = "/repos/$repo/releases";
83 1
		return $url;
84
	}
85
86
}
87