Failed Conditions
Push — master ( 7014aa...7fc00d )
by Alexander
04:08
created

AbstractCommand::checkForAppUpdates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 2
nc 1
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\Command;
12
13
14
use ConsoleHelpers\ConsoleKit\Command\AbstractCommand as BaseCommand;
15
use ConsoleHelpers\SVNBuddy\Config\CommandConfig;
16
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector;
17
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLog;
18
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLogFactory;
19
use ConsoleHelpers\SVNBuddy\Repository\WorkingCopyResolver;
20
use ConsoleHelpers\SVNBuddy\Updater\UpdateManager;
21
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
22
use Symfony\Component\Console\Input\ArgvInput;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
/**
27
 * Base command class.
28
 */
29
abstract class AbstractCommand extends BaseCommand
30
{
31
32
	/**
33
	 * Raw path.
34
	 *
35
	 * @var string
36
	 */
37
	private $_rawPath;
38
39
	/**
40
	 * Whatever "path" argument accepts repository urls.
41
	 *
42
	 * @var boolean
43
	 */
44
	protected $pathAcceptsUrl = false;
45
46
	/**
47
	 * Repository connector
48
	 *
49
	 * @var Connector
50
	 */
51
	protected $repositoryConnector;
52
53
	/**
54
	 * Working directory.
55
	 *
56
	 * @var string
57
	 */
58
	protected $workingDirectory = null;
59
60
	/**
61
	 * Working copy resolver.
62
	 *
63
	 * @var WorkingCopyResolver
64
	 */
65
	private $_workingCopyResolver = null;
66
67
	/**
68
	 * Revision log factory.
69
	 *
70
	 * @var RevisionLogFactory
71
	 */
72
	private $_revisionLogFactory;
73
74
	/**
75
	 * Command config.
76
	 *
77
	 * @var CommandConfig
78
	 */
79
	private $_commandConfig;
80
81
	/**
82
	 * Update manager.
83
	 *
84
	 * @var UpdateManager
85
	 */
86
	private $_updateManager;
87
88
	/**
89
	 * {@inheritdoc}
90
	 *
91
	 * @throws \RuntimeException When url was given instead of path.
92
	 */
93
	protected function initialize(InputInterface $input, OutputInterface $output)
94
	{
95
		$this->_rawPath = null;
96
		$output->getFormatter()->setStyle('debug', new OutputFormatterStyle('white', 'magenta'));
97
98
		parent::initialize($input, $output);
99
100
		// Only apply check for commands, that accept working copy path.
101
		if ( $input->hasArgument('path') ) {
102
			if ( !$this->pathAcceptsUrl && $this->repositoryConnector->isUrl($this->getRawPath()) ) {
103
				throw new \RuntimeException('The "path" argument must be a working copy path and not URL.');
104
			}
105
		}
106
107
		if ( $this->checkForAppUpdates($input) ) {
108
			$this->_showAppUpdateBanner($output);
109
		}
110
	}
111
112
	/**
113
	 * Allow showing update banner.
114
	 *
115
	 * @param InputInterface $input Input.
116
	 *
117
	 * @return boolean
118
	 */
119
	protected function checkForAppUpdates(InputInterface $input)
120
	{
121
		// Show update banner only for outer command invoked by user and not sub-commands.
122
		return $input instanceof ArgvInput;
123
	}
124
125
	/**
126
	 * Shows application update banner.
127
	 *
128
	 * @param OutputInterface $output Output.
129
	 *
130
	 * @return void
131
	 */
132
	private function _showAppUpdateBanner(OutputInterface $output)
1 ignored issue
show
Coding Style introduced by
_showAppUpdateBanner uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
133
	{
134
		$new_version = $this->_updateManager->getNewVersion();
135
136
		if ( !strlen($new_version) ) {
137
			return;
138
		}
139
140
		$message = sprintf(
141
			'  Update available. Run "%s self-update" to upgrade.  ',
142
			$_SERVER['argv'][0]
143
		);
144
		$line_length = mb_strlen($message);
145
146
		$output->writeln(array(
147
			'<fg=white;bg=blue>' . str_repeat(' ', $line_length) . '</>',
148
			'<fg=white;bg=blue>' . $message . '</>',
149
			'<fg=white;bg=blue>' . str_repeat(' ', $line_length) . '</>',
150
			'',
151
		));
152
	}
153
154
	/**
155
	 * Prepare dependencies.
156
	 *
157
	 * @return void
158
	 */
159
	protected function prepareDependencies()
160
	{
161
		parent::prepareDependencies();
162
163
		$container = $this->getContainer();
164
165
		$this->_workingCopyResolver = $container['working_copy_resolver'];
166
		$this->repositoryConnector = $container['repository_connector'];
167
		$this->_revisionLogFactory = $container['revision_log_factory'];
168
		$this->workingDirectory = $container['working_directory'];
169
		$this->_commandConfig = $container['command_config'];
170
		$this->_updateManager = $container['update_manager'];
171
	}
172
173
	/**
174
	 * Returns command setting value.
175
	 *
176
	 * @param string $name Name.
177
	 *
178
	 * @return mixed
179
	 */
180
	protected function getSetting($name)
181
	{
182
		return $this->_commandConfig->getSettingValue($name, $this, $this->getRawPath());
183
	}
184
185
	/**
186
	 * Sets command setting value.
187
	 *
188
	 * @param string $name  Name.
189
	 * @param mixed  $value Value.
190
	 *
191
	 * @return void
192
	 */
193
	protected function setSetting($name, $value)
194
	{
195
		$this->_commandConfig->setSettingValue($name, $this, $this->getRawPath(), $value);
196
	}
197
198
	/**
199
	 * Returns revision log.
200
	 *
201
	 * @param string $repository_url Repository url.
202
	 *
203
	 * @return RevisionLog
204
	 */
205
	protected function getRevisionLog($repository_url)
206
	{
207
		return $this->_revisionLogFactory->getRevisionLog($repository_url, $this->io);
208
	}
209
210
	/**
211
	 * Transforms string into list.
212
	 *
213
	 * @param string $string    String.
214
	 * @param string $separator Separator.
215
	 *
216
	 * @return array
217
	 */
218
	protected function getList($string, $separator = ',')
219
	{
220
		return array_filter(array_map('trim', explode($separator, $string)));
221
	}
222
223
	/**
224
	 * Returns URL to the working copy.
225
	 *
226
	 * @return string
227
	 */
228
	protected function getWorkingCopyUrl()
229
	{
230
		return $this->_workingCopyResolver->getWorkingCopyUrl($this->getRawPath());
231
	}
232
233
	/**
234
	 * Return working copy path.
235
	 *
236
	 * @return string
237
	 */
238
	protected function getWorkingCopyPath()
239
	{
240
		return $this->_workingCopyResolver->getWorkingCopyPath($this->getRawPath());
241
	}
242
243
	/**
244
	 * Returns all refs.
245
	 *
246
	 * @return array
247
	 */
248
	protected function getAllRefs()
249
	{
250
		$wc_url = $this->getWorkingCopyUrl();
251
		$revision_log = $this->getRevisionLog($wc_url);
252
253
		return $revision_log->find('refs', 'all_refs');
254
	}
255
256
	/**
257
	 * Returns working copy path as used specified it.
258
	 *
259
	 * @return string
260
	 */
261
	protected function getRawPath()
262
	{
263
		if ( !isset($this->_rawPath) ) {
264
			// FIXME: During auto-complete working copy at CWD is used regardless of given path.
265
			if ( !isset($this->io) ) {
266
				$this->_rawPath = '.';
267
			}
268
			else {
269
				$this->_rawPath = $this->io->getArgument('path');
270
			}
271
		}
272
273
		return $this->_rawPath;
274
	}
275
276
}
277