Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @copyright Copyright (C) eZ Systems AS. All rights reserved.
- * @license For full copyright and license information view LICENSE file distributed with this source code.
- */
- namespace eZ\Bundle\EzPublishCoreBundle\Command;
- use eZ\Publish\API\Repository\Values\Content\Content;
- use RuntimeException;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Helper\ProgressBar;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Style\SymfonyStyle;
- class CountReverseRelationsCommand extends ContainerAwareCommand
- {
- /** @var \eZ\Publish\API\Repository\ContentService */
- private $contentService;
- /** @var \eZ\Publish\API\Repository\UserService */
- private $userService;
- /** @var \Symfony\Component\Console\Style\SymfonyStyle */
- private $io;
- /** @var \eZ\Publish\API\Repository\PermissionResolver */
- private $permissionResolver;
- protected function configure()
- {
- $this->setName('support-toolkit:count-reverse-relations')
- ->setDescription('Counts reverse relations.')
- ->addOption(
- 'contentId',
- 'c',
- InputOption::VALUE_REQUIRED,
- 'Content ID'
- )
- ->addOption(
- 'userId',
- 'u',
- InputOption::VALUE_OPTIONAL,
- 'User ID',
- 14
- )
- ->setHelp(
- <<<EOT
- The command <info>%command.name%</info> allows you to count content's reverse relations.
- Arguments can be combined.
- Required arguments:
- - Content ID
- <comment>support-toolkit:create-folders --contentId=10</comment> or <comment>support-toolkit:create-folders -c 10</comment>
- Optional arguments:
- - User Id:
- <comment>support-toolkit:create-folders --userId=14</comment> or <comment>support-toolkit:create-folders -u 14</comment>
- EOT
- );
- }
- protected function initialize(InputInterface $input, OutputInterface $output)
- {
- $this->io = new SymfonyStyle($input, $output);
- $this->io->title('Count reverse relation');
- /** @var \eZ\Publish\API\Repository\Repository $repository */
- $repository = $this->getContainer()->get('ezpublish.api.repository');
- $this->contentService = $repository->getContentService();
- $this->userService = $repository->getUserService();
- $this->permissionResolver = $repository->getPermissionResolver();
- }
- /**
- * {@inheritdoc}
- */
- protected function interact(InputInterface $input, OutputInterface $output)
- {
- foreach ($input->getOptions() as $option => $value) {
- if (null === $value && $option !== 'siteaccess') {
- $input->setOption($option, $this->io->ask(sprintf('%s', ucfirst($option))));
- }
- }
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $contentId = $input->getOption('contentId');
- $userId = $input->getOption('userId');
- try {
- $user = $this->userService->loadUser($userId);
- $this->permissionResolver->setCurrentUserReference($user);
- $content = $this->contentService->loadContent($contentId);
- $count = $this->contentService->countReverseRelations($content->contentInfo);
- $output->writeln(sprintf('<info>Content with ID: %d has %d reverse relations</info>',
- $content->id, $count
- ));
- } catch (\Exception $e) {
- $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement