Failed Conditions
Branch master (acad7e)
by Alexander
18:14 queued 08:15
created

CommandConfig::getSetting()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 3
crap 3
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\Config;
12
13
14
use ConsoleHelpers\ConsoleKit\Config\ConfigEditor;
15
use ConsoleHelpers\SVNBuddy\Command\AbstractCommand;
16
use ConsoleHelpers\SVNBuddy\Command\IConfigAwareCommand;
17
use ConsoleHelpers\SVNBuddy\Repository\WorkingCopyResolver;
18
19
class CommandConfig
20
{
21
22
	/**
23
	 * Config editor.
24
	 *
25
	 * @var ConfigEditor
26
	 */
27
	protected $configEditor;
28
29
	/**
30
	 * Working copy resolver.
31
	 *
32
	 * @var WorkingCopyResolver
33
	 */
34
	protected $workingCopyResolver;
35
36
	/**
37
	 * Creates command configurator instance.
38
	 *
39
	 * @param ConfigEditor        $config_editor         Config editor.
40
	 * @param WorkingCopyResolver $working_copy_resolver Working copy resolver.
41
	 */
42 9
	public function __construct(ConfigEditor $config_editor, WorkingCopyResolver $working_copy_resolver)
43
	{
44 9
		$this->configEditor = $config_editor;
45 9
		$this->workingCopyResolver = $working_copy_resolver;
46 9
	}
47
48
	/**
49
	 * Returns command setting value.
50
	 *
51
	 * @param string          $name     Name.
52
	 * @param AbstractCommand $command  Command to get settings from.
53
	 * @param string          $raw_path Raw path.
54
	 *
55
	 * @return mixed
56
	 */
57 6
	public function getSettingValue($name, AbstractCommand $command, $raw_path)
58
	{
59 6
		return $this->getSetting($name, $command, $raw_path)->getValue();
60
	}
61
62
	/**
63
	 * Sets command setting value.
64
	 *
65
	 * @param string          $name     Name.
66
	 * @param AbstractCommand $command  Command to get settings from.
67
	 * @param string          $raw_path Raw path.
68
	 * @param mixed           $value    Value.
69
	 *
70
	 * @return void
71
	 */
72 4
	public function setSettingValue($name, AbstractCommand $command, $raw_path, $value)
73
	{
74 4
		$this->getSetting($name, $command, $raw_path)->setValue($value);
75 2
	}
76
77
	/**
78
	 * Validates command setting usage.
79
	 *
80
	 * @param string          $name     Name.
81
	 * @param AbstractCommand $command  Command to get settings from.
82
	 * @param string          $raw_path Raw path.
83
	 *
84
	 * @return AbstractConfigSetting
85
	 * @throws \LogicException When command don't have any config settings to provide.
86
	 */
87 8
	protected function getSetting($name, AbstractCommand $command, $raw_path)
88
	{
89 8
		if ( !($command instanceof IConfigAwareCommand) ) {
90 2
			throw new \LogicException('The "' . $command->getName() . '" command does not have any settings.');
91
		}
92
93 6
		$config_setting = $this->findSetting($name, $command->getConfigSettings(), $command->getName());
94
95 4
		if ( $config_setting->isWithinScope(AbstractConfigSetting::SCOPE_WORKING_COPY) ) {
96 2
			$config_setting->setWorkingCopyUrl(
97 2
				$this->workingCopyResolver->getWorkingCopyUrl($raw_path)
98 2
			);
99 2
		}
100
101 4
		$config_setting->setEditor($this->configEditor);
102
103 4
		return $config_setting;
104
	}
105
106
	/**
107
	 * Searches for a config setting with a given name.
108
	 *
109
	 * @param string                  $name            Config setting name.
110
	 * @param AbstractConfigSetting[] $config_settings Config settings.
111
	 * @param string                  $command_name    Command name.
112
	 *
113
	 * @return AbstractConfigSetting
114
	 * @throws \LogicException When config setting is not found.
115
	 */
116 6
	protected function findSetting($name, array $config_settings, $command_name)
117
	{
118 6
		foreach ( $config_settings as $config_setting ) {
119 6
			if ( $config_setting->getName() === $name ) {
120 4
				return $config_setting;
121
			}
122 2
		}
123
124 2
		throw new \LogicException(
125 2
			'The "' . $command_name . '" command doesn\'t have "' . $name . '" config setting.'
126 2
		);
127
	}
128
129
}
130