Conditions | 15 |
Paths | 1254 |
Total Lines | 120 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
116 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
117 | { |
||
118 | // We don't load repo services or config resolver before execute() to avoid loading before SiteAccess is set. |
||
119 | $keep = $input->getOption('keep'); |
||
120 | if ($keep === 'config_default') { |
||
121 | $config = $this->repositoryConfigurationProvider->getRepositoryConfig(); |
||
122 | $keep = $config['options']['default_version_archive_limit']; |
||
123 | } |
||
124 | |||
125 | if (($keep = (int) $keep) < 0) { |
||
126 | throw new InvalidArgumentException( |
||
127 | 'keep', |
||
128 | 'Keep value cannot be negative.' |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | $userService = $this->repository->getUserService(); |
||
133 | $contentService = $this->repository->getContentService(); |
||
134 | $permissionResolver = $this->repository->getPermissionResolver(); |
||
135 | |||
136 | $permissionResolver->setCurrentUserReference( |
||
137 | $userService->loadUserByLogin($input->getOption('user')) |
||
|
|||
138 | ); |
||
139 | |||
140 | $status = $input->getOption('status'); |
||
141 | |||
142 | $excludedContentTypes = (string) $input->getOption('excluded-content-types'); |
||
143 | if ($excludedContentTypes === '') { |
||
144 | $excludedContentTypes = self::DEFAULT_EXCLUDED_CONTENT_TYPES; |
||
145 | } |
||
146 | $excludedContentTypeIdentifiers = explode(',', $excludedContentTypes); |
||
147 | $contentIds = $this->getObjectsIds($keep, $status, $excludedContentTypeIdentifiers); |
||
148 | $contentIdsCount = count($contentIds); |
||
149 | |||
150 | if ($contentIdsCount === 0) { |
||
151 | $output->writeln('<info>There is no content matching the given Criteria.</info>'); |
||
152 | |||
153 | return 0; |
||
154 | } |
||
155 | |||
156 | $output->writeln(sprintf( |
||
157 | '<info>Found %d Content IDs matching the given Criteria.</info>', |
||
158 | $contentIdsCount |
||
159 | )); |
||
160 | |||
161 | $displayProgressBar = !($output->isVerbose() || $output->isVeryVerbose() || $output->isDebug()); |
||
162 | |||
163 | if ($displayProgressBar) { |
||
164 | $progressBar = new ProgressBar($output, $contentIdsCount); |
||
165 | $progressBar->setFormat( |
||
166 | '%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%' . PHP_EOL |
||
167 | ); |
||
168 | $progressBar->start(); |
||
169 | } |
||
170 | |||
171 | $removedVersionsCounter = 0; |
||
172 | |||
173 | $removeAll = $status === self::VERSION_ALL; |
||
174 | |||
175 | foreach ($contentIds as $contentId) { |
||
176 | try { |
||
177 | $contentInfo = $contentService->loadContentInfo((int) $contentId); |
||
178 | $versions = $contentService->loadVersions( |
||
179 | $contentInfo, |
||
180 | $removeAll ? null : $this->mapStatusToVersionInfoStatus($status) |
||
181 | ); |
||
182 | $versionsCount = count($versions); |
||
183 | |||
184 | $output->writeln(sprintf( |
||
185 | '<info>Content %d has %d version(s)</info>', |
||
186 | (int) $contentId, |
||
187 | $versionsCount |
||
188 | ), OutputInterface::VERBOSITY_VERBOSE); |
||
189 | |||
190 | if ($removeAll) { |
||
191 | $versions = array_filter($versions, static function (VersionInfo $version) { |
||
192 | return $version->status !== VersionInfo::STATUS_PUBLISHED; |
||
193 | }); |
||
194 | } |
||
195 | |||
196 | if ($keep > 0) { |
||
197 | $versions = array_slice($versions, 0, -$keep); |
||
198 | } |
||
199 | |||
200 | $output->writeln(sprintf( |
||
201 | 'Found %d content (%d) version(s) to remove.', |
||
202 | count($versions), |
||
203 | (int) $contentId |
||
204 | ), OutputInterface::VERBOSITY_VERBOSE); |
||
205 | |||
206 | /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $version */ |
||
207 | foreach ($versions as $version) { |
||
208 | $contentService->deleteVersion($version); |
||
209 | ++$removedVersionsCounter; |
||
210 | $output->writeln(sprintf( |
||
211 | 'Content (%d) version (%d) has been deleted.', |
||
212 | $contentInfo->id, |
||
213 | $version->id |
||
214 | ), OutputInterface::VERBOSITY_VERBOSE); |
||
215 | } |
||
216 | |||
217 | if ($displayProgressBar) { |
||
218 | $progressBar->advance(1); |
||
219 | } |
||
220 | } catch (Exception $e) { |
||
221 | $output->writeln(sprintf( |
||
222 | '<error>%s</error>', |
||
223 | $e->getMessage() |
||
224 | )); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | $output->writeln(sprintf( |
||
229 | '<info>Removed %d unwanted contents version(s) from %d Content item(s).</info>', |
||
230 | $removedVersionsCounter, |
||
231 | $contentIdsCount |
||
232 | )); |
||
233 | |||
234 | return 0; |
||
235 | } |
||
236 | |||
303 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.