Completed
Push — master ( f2828f...7fcd15 )
by Alexander
02:39
created

GroupByRevisionMergeTemplate::apply()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 19
cts 19
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 4
nop 1
crap 4
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Repository\CommitMessage;
12
13
14
class GroupByRevisionMergeTemplate extends AbstractMergeTemplate
15
{
16
17
	/**
18
	 * Returns merge template name.
19
	 *
20
	 * @return string
21
	 */
22 2
	public function getName()
23
	{
24 2
		return 'group_by_revision';
25
	}
26
27
	/**
28
	 * Applies merge template to a working copy.
29
	 *
30
	 * @param string $wc_path Working copy path.
31
	 *
32
	 * @return string
33
	 */
34 2
	public function apply($wc_path)
35
	{
36 2
		$merged_revisions = $this->repositoryConnector->getFreshMergedRevisions($wc_path);
37
38 2
		if ( !$merged_revisions ) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $merged_revisions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39 1
			return '';
40
		}
41
42 1
		$ret = '';
43 1
		$wc_url = $this->repositoryConnector->getWorkingCopyUrl($wc_path);
44 1
		$repository_url = $this->repositoryConnector->getRootUrl($wc_url);
45
46 1
		foreach ( $merged_revisions as $path => $revisions ) {
47 1
			$merged_messages = array();
48 1
			$revision_log = $this->revisionLogFactory->getRevisionLog($repository_url . $path);
49 1
			$ret .= PHP_EOL . $this->getCommitMessageHeading($wc_url, $path) . PHP_EOL;
50
51 1
			$revisions_data = $revision_log->getRevisionsData('summary', $revisions);
52
53 1
			foreach ( $revisions as $revision ) {
54 1
				$merged_messages[] = ' * r' . $revision . ': ' . $revisions_data[$revision]['msg'];
55 1
			}
56
57 1
			$merged_messages = array_unique(array_map('trim', $merged_messages));
58 1
			$ret .= implode(PHP_EOL, $merged_messages) . PHP_EOL;
59 1
		}
60
61 1
		return trim($ret);
62
	}
63
64
	/**
65
	 * Builds commit message heading.
66
	 *
67
	 * @param string $wc_url Working copy url.
68
	 * @param string $path   Source path for merge operation.
69
	 *
70
	 * @return string
71
	 */
72 1
	protected function getCommitMessageHeading($wc_url, $path)
73
	{
74 1
		return 'Merging from ' . ucfirst(basename($path)) . ' to ' . ucfirst(basename($wc_url));
75
	}
76
77
}
78