|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sherlockode\SyliusAdvancedContentPlugin\Command; |
| 4 | + |
| 5 | +use Sherlockode\AdvancedContentBundle\Manager\ConfigurationManager; |
| 6 | +use Sherlockode\SyliusAdvancedContentPlugin\Scope\ScopeInitializer; |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 11 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 12 | + |
| 13 | +class ScopeInitCommand extends Command |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var ConfigurationManager |
| 17 | + */ |
| 18 | + private $configurationManager; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var TranslatorInterface |
| 22 | + */ |
| 23 | + private $translator; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var ScopeInitializer |
| 27 | + */ |
| 28 | + private $scopeInitializer; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param ConfigurationManager $configurationManager |
| 32 | + * @param TranslatorInterface $translator |
| 33 | + * @param ScopeInitializer $scopeInitializer |
| 34 | + * @param $name |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + ConfigurationManager $configurationManager, |
| 38 | + TranslatorInterface $translator, |
| 39 | + ScopeInitializer $scopeInitializer, |
| 40 | + $name = null |
| 41 | + ) { |
| 42 | + parent::__construct($name); |
| 43 | + $this->configurationManager = $configurationManager; |
| 44 | + $this->translator = $translator; |
| 45 | + $this->scopeInitializer = $scopeInitializer; |
| 46 | + } |
| 47 | + |
| 48 | + protected function configure() |
| 49 | + { |
| 50 | + $this |
| 51 | + ->setName('sherlockode:sylius-acb:init-scope') |
| 52 | + ->setDescription('Initialize sylius scopes for ACB') |
| 53 | + ; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param InputInterface $input |
| 58 | + * @param OutputInterface $output |
| 59 | + * |
| 60 | + * @return int|void |
| 61 | + */ |
| 62 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 63 | + { |
| 64 | + $io = new SymfonyStyle($input, $output); |
| 65 | + |
| 66 | + if (!$this->configurationManager->isScopesEnabled()) { |
| 67 | + $io->info($this->translator->trans('sherlockode_sylius_acb.scopes.disabled')); |
| 68 | + |
| 69 | + if (defined(sprintf('%s::SUCCESS', get_class($this)))) { |
| 70 | + return self::SUCCESS; |
| 71 | + } |
| 72 | + return; |
| 73 | + } |
| 74 | + if (!$this->scopeInitializer->hasMissingScopes()) { |
| 75 | + $io->info($this->translator->trans('sherlockode_sylius_acb.scopes.up_to_date')); |
| 76 | + |
| 77 | + if (defined(sprintf('%s::SUCCESS', get_class($this)))) { |
| 78 | + return self::SUCCESS; |
| 79 | + } |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + $this->scopeInitializer->init(); |
| 85 | + $io->success($this->translator->trans('sherlockode_sylius_acb.scopes.init_success')); |
| 86 | + if (defined(sprintf('%s::SUCCESS', get_class($this)))) { |
| 87 | + return self::SUCCESS; |
| 88 | + } |
| 89 | + return; |
| 90 | + } catch (\Exception $e) { |
| 91 | + $io->error($e->getMessage()); |
| 92 | + |
| 93 | + if (defined(sprintf('%s::FAILURE', get_class($this)))) { |
| 94 | + return self::FAILURE; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments