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

CommitMessageBuilder::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
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
use ConsoleHelpers\SVNBuddy\Repository\WorkingCopyConflictTracker;
15
16
class CommitMessageBuilder
17
{
18
19
	/**
20
	 * Working copy conflict tracker.
21
	 *
22
	 * @var WorkingCopyConflictTracker
23
	 */
24
	protected $workingCopyConflictTracker;
25
26
	/**
27
	 * Creates commit message builder instance.
28
	 *
29
	 * @param WorkingCopyConflictTracker $working_copy_conflict_tracker Working copy conflict tracker.
30
	 */
31 6
	public function __construct(WorkingCopyConflictTracker $working_copy_conflict_tracker)
32
	{
33 6
		$this->workingCopyConflictTracker = $working_copy_conflict_tracker;
34 6
	}
35
36
	/**
37
	 * Builds a commit message.
38
	 *
39
	 * @param string                $wc_path        Working copy path.
40
	 * @param AbstractMergeTemplate $merge_template Merge template.
41
	 * @param string|null           $changelist     Changelist.
42
	 *
43
	 * @return string
44
	 */
45 5
	public function build($wc_path, AbstractMergeTemplate $merge_template, $changelist = null)
46
	{
47 5
		$commit_message_parts = array();
48
49 5
		if ( strlen($changelist) ) {
50 2
			$commit_message_parts[] = trim($changelist);
51 2
		}
52
53 5
		$commit_message_parts[] = $merge_template->apply($wc_path);
54 5
		$commit_message_parts[] = $this->getFragmentForRecentConflicts($wc_path);
55
56 5
		return implode(PHP_EOL, array_filter($commit_message_parts));
57
	}
58
59
	/**
60
	 * Returns commit message fragment for recent conflicts.
61
	 *
62
	 * @param string $wc_path Working copy path.
63
	 *
64
	 * @return string
65
	 */
66 5
	protected function getFragmentForRecentConflicts($wc_path)
67
	{
68 5
		$recorded_conflicts = $this->workingCopyConflictTracker->getRecordedConflicts($wc_path);
69
70 5
		if ( !$recorded_conflicts ) {
71 3
			return '';
72
		}
73
74
		// Ensure empty line before.
75 2
		$ret = PHP_EOL . 'Conflicts:';
76
77 2
		foreach ( $recorded_conflicts as $conflict_path ) {
78 2
			$ret .= PHP_EOL . ' * ' . $conflict_path;
79 2
		}
80
81 2
		return $ret;
82
	}
83
84
}
85