MergeSourceDetectorAggregator::add()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
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\MergeSourceDetector;
12
13
14
class MergeSourceDetectorAggregator extends AbstractMergeSourceDetector
15
{
16
17
	/**
18
	 * Merge sources.
19
	 *
20
	 * @var AbstractMergeSourceDetector[]
21
	 */
22
	private $_detectors = array();
23
24
	/**
25
	 * Adds merge source detector.
26
	 *
27
	 * @param AbstractMergeSourceDetector $merge_source_detector Merge source detector.
28
	 *
29
	 * @return void
30
	 * @throws \InvalidArgumentException When another detector with same weight was added.
31
	 */
32 5
	public function add(AbstractMergeSourceDetector $merge_source_detector)
33
	{
34 5
		$weight = $merge_source_detector->getWeight();
35
36 5
		if ( array_key_exists($weight, $this->_detectors) ) {
37 1
			throw new \InvalidArgumentException('Another detector with same weight is already added.');
38
		}
39
40 5
		$this->_detectors[$weight] = $merge_source_detector;
41 5
		krsort($this->_detectors, SORT_NUMERIC);
42
	}
43
44
	/**
45
	 * Detects merge source from repository url.
46
	 *
47
	 * @param string $repository_url Repository url.
48
	 *
49
	 * @return null|string
50
	 */
51 2
	public function detect($repository_url)
52
	{
53 2
		foreach ( $this->_detectors as $detector ) {
54 2
			$result = $detector->detect($repository_url);
55
56 2
			if ( isset($result) ) {
57 1
				return $result;
58
			}
59
		}
60
61 1
		return null;
62
	}
63
64
}
65