1 | <?php |
||
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){ |
||
67 | |||
68 | protected function configure(){ |
||
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'); |
||
|
|||
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']); |
||
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()); |
||
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()); |
||
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){ |
||
194 | |||
195 | /** |
||
196 | * Callback to output download progress |
||
197 | * @param ProgressEvent $e |
||
198 | */ |
||
199 | public function progress(ProgressEvent $e){ |
||
206 | } |
||
207 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: