DetectCommand::execute()   C
last analyzed

Complexity

Conditions 12
Paths 62

Size

Total Lines 85

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 85
ccs 0
cts 70
cp 0
rs 5.9006
c 0
b 0
f 0
cc 12
nc 62
nop 2
crap 156

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Command;
23
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Question\ChoiceQuestion;
28
use GuzzleHttp\Event\ProgressEvent;
29
use GuzzleHttp\Exception\ClientException;
30
use Owncloud\Updater\Utils\Fetcher;
31
use Owncloud\Updater\Utils\ConfigReader;
32
use \Owncloud\Updater\Controller\DownloadController;
33
34
/**
35
 * Class DetectCommand
36
 *
37
 * @package Owncloud\Updater\Command
38
 */
39
class DetectCommand extends Command {
40
41
	/**
42
	 * @var Fetcher $fetcher
43
	 */
44
	protected $fetcher;
45
46
	/**
47
	 * @var ConfigReader $configReader
48
	 */
49
	protected $configReader;
50
51
	/**
52
	 *
53
	 */
54
	protected $output;
55
56
	/**
57
	 * Constructor
58
	 *
59
	 * @param Fetcher $fetcher
60
	 * @param ConfigReader $configReader
61
	 */
62
	public function __construct(Fetcher $fetcher, ConfigReader $configReader){
63
		parent::__construct();
64
		$this->fetcher = $fetcher;
65
		$this->configReader = $configReader;
66
	}
67
68
	protected function configure(){
69
		$this
70
				->setName('upgrade:detect')
71
				->setDescription('Detect
72
- 1. currently existing code, 
73
- 2. version in config.php, 
74
- 3. online available verison.
75
(ASK) what to do? (download, upgrade, abort, …)')
76
				->addOption(
77
						'exit-if-none', null, InputOption::VALUE_NONE, 'exit with non-zero status code if new version is not found'
78
				)
79
				->addOption(
80
						'only-check', null, InputOption::VALUE_NONE, 'Only check if update is available'
81
				)
82
		;
83
		;
84
	}
85
86
	/**
87
	 * @param InputInterface $input
88
	 * @param OutputInterface $output
89
	 * @return int
90
	 */
91
	protected function execute(InputInterface $input, OutputInterface $output){
92
		$registry = $this->container['utils.registry'];
93
		$registry->set('feed', false);
94
95
		$fsHelper = $this->container['utils.filesystemhelper'];
96
		$downloadController = new DownloadController($this->fetcher, $registry, $fsHelper);
97
		try {
98
			$currentVersion = $this->configReader->getByPath('system.version');
99
			if (!strlen($currentVersion)){
100
				throw new \UnexpectedValueException('Could not detect installed version.');
101
			}
102
103
			$this->getApplication()->getLogger()->info('ownCloud ' . $currentVersion . ' found');
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getLogger() does only exist in the following sub-classes of Symfony\Component\Console\Application: Owncloud\Updater\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
104
			$output->writeln('Current version is ' . $currentVersion);
105
106
			$feedData = $downloadController->checkFeed();
107
			if (!$feedData['success']){
108
				// Network errors, etc
109
				$output->writeln("Can't fetch feed.");
110
				$output->writeln($feedData['exception']->getMessage());
111
				$this->getApplication()->logException($feedData['exception']);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method logException() does only exist in the following sub-classes of Symfony\Component\Console\Application: Owncloud\Updater\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
112
				// Return a number to stop the queue
113
				return $input->getOption('exit-if-none') ? 4 : null;
114
			}
115
116
			/** @var \Owncloud\Updater\Utils\Feed $feed */
117
			$feed = $feedData['data']['feed'];
118
			if (!$feed->isValid()){
119
				// Feed is empty. Means there are no updates
120
				$output->writeln('No updates found online.');
121
				return $input->getOption('exit-if-none') ? 4 : null;
122
			}
123
124
			$registry->set('feed', $feed);
125
			$output->writeln(
126
				sprintf(
127
					'Online version is %s [%s]',
128
					$feed->getVersion(),
129
					$this->fetcher->getUpdateChannel()
130
				)
131
			);
132
133
			if ($input->getOption('only-check')){
134
				return 0;
135
			}
136
137
			$action = $this->ask($input, $output);
138
			if ($action === 'abort'){
139
				$output->writeln('Exiting on user command.');
140
				return 128;
141
			}
142
143
			$this->output = $output;
144
			$packageData = $downloadController->downloadOwncloud([$this, 'progress']);
145
			//Empty line, in order not to overwrite the progress message
146
			$this->output->writeln('');
147
			if (!$packageData['success']){
148
				$registry->set('feed', null);
149
				throw $packageData['exception'];
150
			}
151
	
152
			if ($action === 'download'){
153
				$output->writeln('Downloading has been completed. Exiting.');
154
				return 64;
155
			}
156
		} catch (ClientException $e){
157
			$this->getApplication()->getLogger()->error($e->getMessage());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getLogger() does only exist in the following sub-classes of Symfony\Component\Console\Application: Owncloud\Updater\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
158
			$output->writeln('<error>Network error</error>');
159
			$output->writeln(
160
					sprintf(
161
							'<error>Error %d: %s while fetching an URL %s</error>',
162
							$e->getCode(),
163
							$e->getResponse()->getReasonPhrase(),
164
							$e->getResponse()->getEffectiveUrl()
165
							)
166
			);
167
			return 2;
168
		} catch (\Exception $e){
169
			$this->getApplication()->getLogger()->error($e->getMessage());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getLogger() does only exist in the following sub-classes of Symfony\Component\Console\Application: Owncloud\Updater\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
170
			$output->writeln('<error>'.$e->getMessage().'</error>');
171
			return 2;
172
		}
173
174
		return 0;
175
	}
176
177
	/**
178
	 * Ask what to do
179
	 * @param InputInterface $input
180
	 * @param OutputInterface $output
181
	 * @return string
182
	 */
183
	public function ask(InputInterface $input, OutputInterface $output){
184
		$helper = $this->getHelper('question');
185
		$question = new ChoiceQuestion(
186
			'What would you do next?',
187
			['download', 'upgrade', 'abort'],
188
			'1'
189
		);
190
		$action = $helper->ask($input, $output, $question);
191
192
		return $action;
193
	}
194
195
	/**
196
	 * Callback to output download progress
197
	 * @param ProgressEvent $e
198
	 */
199
	public function progress(ProgressEvent $e){
200
		if ($e->downloadSize){
201
			$percent = intval(100 * $e->downloaded / $e->downloadSize );
202
			$percentString = $percent . '%';
203
			$this->output->write( 'Downloaded ' . $percentString . ' (' . $e->downloaded . ' of ' . $e->downloadSize . ")\r");
204
		}
205
	}
206
}
207