Completed
Push — master ( 8c59ef...24dea2 )
by Alexander
03:02
created

AbstractGroupByMergeTemplate::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 2
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
abstract class AbstractGroupByMergeTemplate extends AbstractMergeTemplate
15
{
16
17
	/**
18
	 * Applies merge template to a working copy.
19
	 *
20
	 * @param string $wc_path Working copy path.
21
	 *
22
	 * @return string
23
	 */
24 4
	public function apply($wc_path)
25
	{
26 4
		$merged_revisions = $this->repositoryConnector->getFreshMergedRevisions($wc_path);
27
28 4
		if ( !$merged_revisions ) {
29 2
			return '';
30
		}
31
32 2
		$wc_url = $this->repositoryConnector->getWorkingCopyUrl($wc_path);
33 2
		$relative_path = $this->repositoryConnector->getRelativePath($wc_url);
34 2
		$repository_url = $this->repositoryConnector->getRootUrl($wc_url);
35
36 2
		return $this->doGenerate($merged_revisions, $relative_path, $repository_url);
37
	}
38
39
	/**
40
	 * Builds commit message.
41
	 *
42
	 * @param array  $merged_revisions Merged revisions.
43
	 * @param string $relative_path    Relative path.
44
	 * @param string $repository_url   Repository url.
45
	 *
46
	 * @return string
47
	 */
48 2
	protected function doGenerate(array $merged_revisions, $relative_path, $repository_url)
49
	{
50 2
		$ret = '';
51
52 2
		foreach ( $merged_revisions as $path => $revisions ) {
53 2
			$ret .= PHP_EOL . $this->generateGroupHeading($path, $relative_path);
54 2
			$ret .= PHP_EOL . $this->generateGroupBody($revisions, $repository_url . $path);
55 2
			$ret .= PHP_EOL;
56
		}
57
58 2
		return trim($ret);
59
	}
60
61
	/**
62
	 * Builds group heading.
63
	 *
64
	 * @param string $source_path Source path for merge operation.
65
	 * @param string $target_path Target path for merge operation.
66
	 *
67
	 * @return string
68
	 */
69 2
	protected function generateGroupHeading($source_path, $target_path)
70
	{
71 2
		$from_path = basename($source_path);
72 2
		$source_project = $this->repositoryConnector->getProjectUrl($source_path);
73
74 2
		$to_path = basename($target_path);
75 2
		$target_project = $this->repositoryConnector->getProjectUrl($target_path);
76
77 2
		if ( $source_project !== $target_project ) {
78 2
			$from_project_parts = explode('/', $source_project);
79 2
			$from_path .= ' (' . end($from_project_parts) . ')';
80
		}
81
82 2
		return 'Merging from ' . ucfirst($from_path) . ' to ' . ucfirst($to_path);
83
	}
84
85
	/**
86
	 * Builds group body.
87
	 *
88
	 * @param array  $revisions  Revisions.
89
	 * @param string $source_url Source URL.
90
	 *
91
	 * @return string
92
	 */
93
	abstract protected function generateGroupBody(array $revisions, $source_url);
94
95
}
96