Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  5. * @license For full copyright and license information view LICENSE file distributed with this source code.
  6. */
  7. namespace eZ\Bundle\EzPublishCoreBundle\Command;
  8.  
  9. use eZ\Publish\API\Repository\Values\Content\Content;
  10. use RuntimeException;
  11. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  12. use Symfony\Component\Console\Helper\ProgressBar;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17.  
  18. class CountReverseRelationsCommand extends ContainerAwareCommand
  19. {
  20. /** @var \eZ\Publish\API\Repository\ContentService */
  21. private $contentService;
  22.  
  23. /** @var \eZ\Publish\API\Repository\UserService */
  24. private $userService;
  25.  
  26. /** @var \Symfony\Component\Console\Style\SymfonyStyle */
  27. private $io;
  28.  
  29. /** @var \eZ\Publish\API\Repository\PermissionResolver */
  30. private $permissionResolver;
  31.  
  32. protected function configure()
  33. {
  34. $this->setName('support-toolkit:count-reverse-relations')
  35. ->setDescription('Counts reverse relations.')
  36. ->addOption(
  37. 'contentId',
  38. 'c',
  39. InputOption::VALUE_REQUIRED,
  40. 'Content ID'
  41. )
  42. ->addOption(
  43. 'userId',
  44. 'u',
  45. InputOption::VALUE_OPTIONAL,
  46. 'User ID',
  47. 14
  48. )
  49. ->setHelp(
  50. <<<EOT
  51. The command <info>%command.name%</info> allows you to count content's reverse relations.
  52. Arguments can be combined.
  53. Required arguments:
  54. - Content ID
  55. <comment>support-toolkit:create-folders --contentId=10</comment> or <comment>support-toolkit:create-folders -c 10</comment>
  56. Optional arguments:
  57. - User Id:
  58. <comment>support-toolkit:create-folders --userId=14</comment> or <comment>support-toolkit:create-folders -u 14</comment>
  59. EOT
  60. );
  61. }
  62.  
  63. protected function initialize(InputInterface $input, OutputInterface $output)
  64. {
  65. $this->io = new SymfonyStyle($input, $output);
  66. $this->io->title('Count reverse relation');
  67.  
  68. /** @var \eZ\Publish\API\Repository\Repository $repository */
  69. $repository = $this->getContainer()->get('ezpublish.api.repository');
  70. $this->contentService = $repository->getContentService();
  71. $this->userService = $repository->getUserService();
  72. $this->permissionResolver = $repository->getPermissionResolver();
  73. }
  74.  
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function interact(InputInterface $input, OutputInterface $output)
  79. {
  80. foreach ($input->getOptions() as $option => $value) {
  81. if (null === $value && $option !== 'siteaccess') {
  82. $input->setOption($option, $this->io->ask(sprintf('%s', ucfirst($option))));
  83. }
  84. }
  85. }
  86.  
  87. protected function execute(InputInterface $input, OutputInterface $output)
  88. {
  89. $contentId = $input->getOption('contentId');
  90. $userId = $input->getOption('userId');
  91.  
  92. try {
  93. $user = $this->userService->loadUser($userId);
  94. $this->permissionResolver->setCurrentUserReference($user);
  95.  
  96. $content = $this->contentService->loadContent($contentId);
  97.  
  98. $count = $this->contentService->countReverseRelations($content->contentInfo);
  99.  
  100. $output->writeln(sprintf('<info>Content with ID: %d has %d reverse relations</info>',
  101. $content->id, $count
  102. ));
  103. } catch (\Exception $e) {
  104. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement