diff --git a/app/code/Magento/Store/Console/Command/StoreCreateCommand.php b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php new file mode 100644 index 0000000000000..5ff661756bc39 --- /dev/null +++ b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php @@ -0,0 +1,164 @@ +storeFactory = $storeFactory; + $this->createStore = $createStore; + $this->getDefaultStoreGroup = $getDefaultStoreGroup; + $this->filterManager = $filterManager; + parent::__construct($name); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this->setName('store:create') + ->setDescription('Create new store view (see list: bin/magento store:list)') + ->addArgument(self::INPUT_ARGUMENT_NAME, InputArgument::REQUIRED, 'Put the store view name you want to create') + ->addArgument(self::INPUT_ARGUMENT_CODE, InputArgument::REQUIRED, 'Put the store view code') + ->addArgument( + self::INPUT_ARGUMENT_IS_ACTIVE, + InputArgument::OPTIONAL, + 'Status (enable/disable)', + true + ) + ->addArgument( + self::INPUT_ARGUMENT_SORT_ORDER, + InputArgument::OPTIONAL, + 'Sort Order', + 0 + ) + ; + + $this->addOption( + self::INPUT_OPTION_GROUP, + 'g', + InputOption::VALUE_OPTIONAL, + 'Group ID (php bin/magento store:list).' + ); + + parent::configure(); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + try { + $defaultGroupId = $input->getOption(self::INPUT_OPTION_GROUP); + if ($defaultGroupId === null) { + /** @var \Magento\Store\Api\Data\GroupInterface $defaultGroup */ + $defaultGroup = $this->getDefaultStoreGroup->execute(); + $defaultGroupId = $defaultGroup->getId(); + } + $defaultGroupId = (int) $defaultGroupId; + $data = [ + self::INPUT_OPTION_GROUP => (int) $input->getOption( + self::INPUT_OPTION_GROUP + ), + self::INPUT_ARGUMENT_NAME => (string) $input->getArgument( + self::INPUT_ARGUMENT_NAME + ), + self::INPUT_ARGUMENT_CODE => (string) $input->getArgument( + self::INPUT_ARGUMENT_CODE + ), + self::INPUT_ARGUMENT_IS_ACTIVE => (bool) $input->getArgument( + self::INPUT_ARGUMENT_IS_ACTIVE + ), + self::INPUT_ARGUMENT_SORT_ORDER => (int) $input->getArgument( + self::INPUT_ARGUMENT_SORT_ORDER + ) + ]; + $data[self::INPUT_ARGUMENT_NAME] = $this->filterManager->removeTags( + $data[self::INPUT_ARGUMENT_NAME] + ); + $data[self::INPUT_ARGUMENT_CODE] = $this->filterManager->removeTags( + $data[self::INPUT_ARGUMENT_CODE] + ); + + /** @var \Magento\Store\Model\Storev $storeModel */ + $storeModel = $this->storeFactory->create(['data' => $data]); + + $this->createStore->create($storeModel); + + $io->success('You created the store view.'); + + return \Magento\Framework\Console\Cli::RETURN_SUCCESS; + } catch (\Exception $e) { + $io->error($e->getMessage()); + if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { + $io->comment($e->getTraceAsString()); + } + + return \Magento\Framework\Console\Cli::RETURN_FAILURE; + } + } +} diff --git a/app/code/Magento/Store/Model/CreateStore.php b/app/code/Magento/Store/Model/CreateStore.php new file mode 100644 index 0000000000000..f17f32715d817 --- /dev/null +++ b/app/code/Magento/Store/Model/CreateStore.php @@ -0,0 +1,49 @@ +groupFactory = $groupFactory; + } + + /** + * + * @param \Magento\Store\Api\Data\StoreInterface $storeModel + * @return \Magento\Store\Api\Data\StoreInterface + */ + public function execute(StoreInterface $storeModel): StoreInterface + { + /** @var \Magento\Store\Model\Group $groupModel */ + $groupModel = $this->groupFactory->create() + ->load($storeModel->getGroupId()); + + $storeModel->setWebsiteId($groupModel->getWebsiteId()); + $storeModel->save(); + + return $storeModel; + } +} diff --git a/app/code/Magento/Store/Model/GetDefaultStoreGroup.php b/app/code/Magento/Store/Model/GetDefaultStoreGroup.php new file mode 100644 index 0000000000000..a432f41019a89 --- /dev/null +++ b/app/code/Magento/Store/Model/GetDefaultStoreGroup.php @@ -0,0 +1,74 @@ +websiteFactory = $websiteFactory; + $this->groupFactory = $groupFactory; + } + + /** + * Get default group + * + * @return GroupInterface + */ + public function execute() : GroupInterface + { + $groups = $this->getAllStoreGroups(); + + return current($groups); + } + + + /** + * Retrieve list of store groups + * + * @return array + */ + private function getAllStoreGroups() : array + { + $websites = $this->websiteFactory->create()->getCollection(); + $allgroups = $this->groupFactory->create()->getCollection(); + $groups = []; + foreach ($websites as $website) { + foreach ($allgroups as $group) { + if ($group->getWebsiteId() == $website->getId()) { + $groups[$group->getWebsiteId() . '-' . $group->getId()] = $group; + } + } + } + + return $groups; + } +} diff --git a/app/code/Magento/Store/etc/di.xml b/app/code/Magento/Store/etc/di.xml index 984a16eb34965..04c6a6767b9f4 100644 --- a/app/code/Magento/Store/etc/di.xml +++ b/app/code/Magento/Store/etc/di.xml @@ -388,6 +388,7 @@ Magento\Store\Console\Command\StoreListCommand + Magento\Store\Console\Command\StoreCreateCommand Magento\Store\Console\Command\WebsiteListCommand diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php new file mode 100644 index 0000000000000..306e6036bdc75 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php @@ -0,0 +1,93 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + + $this->storeModel = $this->objectManager->get( + \Magento\Store\Model\Store::class + ); + + $this->createStoreModel = $this->objectManager->get( + \Magento\Store\Model\CreateStore::class + ); + + $this->websiteModel = $this->objectManager->create( + \Magento\Store\Model\Website::class + ); + $this->websiteModel->load(1); + } + + /** + * @param $data + * @dataProvider loadCreateDataProvider + */ + public function testExecute($data) + { + $defaultGroupId = $this->websiteModel->getDefaultGroupId(); + $data['group_id'] = $defaultGroupId; + + $store = $this->storeModel->setData($data); + $store = $this->createStoreModel->execute($store); + $this->assertInstanceOf(\Magento\Store\Api\Data\StoreInterface::class, $store); + $this->assertNotNull($store->getId(), 'Store was not saved correctly'); + $this->assertEquals($data['code'], $store->getCode()); + + /** @var \Magento\Framework\Registry $registry */ + $registry = $this->objectManager->get(\Magento\Framework\Registry::class); + + $registry->unregister('isSecureArea'); + $registry->register('isSecureArea', true); + + $store->delete(); + + $registry->unregister('isSecureArea'); + $registry->register('isSecureArea', false); + } + + /** + * @return array + */ + public function loadCreateDataProvider() + { + return [[ + [ + 'name' => 'code', + 'code' => 'code', + 'is_active' => 0, + 'sort_order' => 10, + ] + ]]; + } +} diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php new file mode 100644 index 0000000000000..8be7ae584fea5 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php @@ -0,0 +1,28 @@ +get( + \Magento\Store\Model\GetDefaultStoreGroup::class + ); + + $group = $getDefaultStoreGroupModel->execute(); + + $this->assertInstanceOf(\Magento\Store\Api\Data\GroupInterface::class, $group); + $this->assertNotNull($group->getId()); + $this->assertEquals('main_website_store', $group->getCode()); + } +}