diff --git a/Model/Console/AddBlockCommand.php b/Model/Console/AddBlockCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..e3bc4ca055ee6da6017dddf7a03ec3c5bb98fcd6 --- /dev/null +++ b/Model/Console/AddBlockCommand.php @@ -0,0 +1,66 @@ +<?php +declare(strict_types=1); + +namespace Firegento\ContentProvisioning\Model\Console; + +use Firegento\ContentProvisioning\Model\Command\ApplyBlockEntry; +use Firegento\ContentProvisioning\Model\Query\GetBlockEntryByKey; +use Firegento\ContentProvisioning\Model\Query\GetBlockEntryList\Proxy as GetBlockEntryList; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class AddBlockCommand extends Command +{ + const ARG_BLOCK_KEY = 'key'; + + /** + * @var GetBlockEntryByKey + */ + private $getBlockEntryByKey; + + /** + * @var ApplyBlockEntry + */ + private $applyBlockEntry; + + /** + * @param GetBlockEntryList $getBlockEntryByKey + * @param string|null $name + */ + public function __construct( + GetBlockEntryByKey $getBlockEntryByKey, + ApplyBlockEntry $applyBlockEntry, + string $name = null + ) { + parent::__construct($name); + $this->getBlockEntryByKey = $getBlockEntryByKey; + $this->applyBlockEntry = $applyBlockEntry; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $key = $input->getArgument(self::ARG_BLOCK_KEY); + $block = $this->getBlockEntryByKey->get($key); + $this->applyBlockEntry->execute($block); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this->setName('content-provisioning:block:apply'); + $this->setDescription('Add a block by key'); + $this->addArgument( + self::ARG_BLOCK_KEY, + InputArgument::REQUIRED, + 'The key of the block to apply.' + ); + parent::configure(); + } +} diff --git a/Model/Console/AddPageCommand.php b/Model/Console/AddPageCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..fb34b57e413c4de00d8fdf1043d4e6eaa2c4bf4a --- /dev/null +++ b/Model/Console/AddPageCommand.php @@ -0,0 +1,68 @@ +<?php +declare(strict_types=1); + +namespace Firegento\ContentProvisioning\Model\Console; + +use Firegento\ContentProvisioning\Model\Command\ApplyPageEntry; +use Firegento\ContentProvisioning\Model\Query\GetPageEntryByKey; +use Firegento\ContentProvisioning\Model\Query\GetPageEntryList\Proxy as GetPageEntryList; +use Firegento\ContentProvisioning\Model\Query\GetPagesByPageEntry\Proxy as GetPagesByPageEntry; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class AddPageCommand extends Command +{ + const ARG_PAGE_KEY = 'key'; + + /** + * @var GetPageEntryByKey + */ + private $getPageEntryByKey; + + /** + * @var ApplyPageEntry + */ + private $applyPageEntry; + + /** + * @param GetPageEntryList $getAllContentEntries + * @param GetPagesByPageEntry $getPagesByPageEntry + * @param string|null $name + */ + public function __construct( + GetPageEntryByKey $getPageEntryByKey, + ApplyPageEntry $applyPageEntry, + string $name = null + ) { + parent::__construct($name); + $this->getPageEntryByKey = $getPageEntryByKey; + $this->applyPageEntry = $applyPageEntry; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $key = $input->getArgument(self::ARG_PAGE_KEY); + $page = $this->getPageEntryByKey->get($key); + $this->applyPageEntry->execute($page); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this->setName('content-provisioning:page:apply'); + $this->setDescription('Add a page by key'); + $this->addArgument( + self::ARG_PAGE_KEY, + InputArgument::REQUIRED, + 'The key of the page to apply.' + ); + parent::configure(); + } +} diff --git a/Model/Query/GetBlockEntryByKey.php b/Model/Query/GetBlockEntryByKey.php new file mode 100644 index 0000000000000000000000000000000000000000..8515fcb3a1acdc197ff33cfe9f6e8cfb1649d678 --- /dev/null +++ b/Model/Query/GetBlockEntryByKey.php @@ -0,0 +1,51 @@ +<?php +declare(strict_types=1); + +namespace Firegento\ContentProvisioning\Model\Query; + +use Firegento\ContentProvisioning\Api\ConfigurationInterface; +use Firegento\ContentProvisioning\Api\Data\BlockEntryInterface; +use Firegento\ContentProvisioning\Api\Data\BlockEntryInterfaceFactory; +use Magento\Framework\Exception\NotFoundException; + +class GetBlockEntryByKey +{ + /** + * @var ConfigurationInterface + */ + private $configuration; + + /** + * @var BlockEntryInterfaceFactory + */ + private $blockEntryFactory; + + /** + * @param ConfigurationInterface $configuration + * @param BlockEntryInterfaceFactory $blockEntryFactory + */ + public function __construct( + ConfigurationInterface $configuration, + BlockEntryInterfaceFactory $blockEntryFactory + ) { + $this->configuration = $configuration; + $this->blockEntryFactory = $blockEntryFactory; + } + + /** + * @return BlockEntryInterface[] + * @throws NotFoundException + */ + public function get(string $key) + { + $blocks = $this->configuration->getList()['blocks'] ?? []; + + if (!array_key_exists($key, $blocks)) { + throw new NotFoundException(__('Block with key %1 not found.', $key)); + } + + $item = $this->blockEntryFactory->create(['data' => $blocks[$key]]); + + return $item; + } +} diff --git a/Model/Query/GetPageEntryByKey.php b/Model/Query/GetPageEntryByKey.php new file mode 100644 index 0000000000000000000000000000000000000000..99e91038c188d2de8cc7383b18e4097b860c1e38 --- /dev/null +++ b/Model/Query/GetPageEntryByKey.php @@ -0,0 +1,53 @@ +<?php +declare(strict_types=1); + +namespace Firegento\ContentProvisioning\Model\Query; + +use Firegento\ContentProvisioning\Api\ConfigurationInterface; +use Firegento\ContentProvisioning\Api\Data\PageEntryInterface; +use Firegento\ContentProvisioning\Api\Data\PageEntryInterfaceFactory; +use Magento\Framework\Exception\NotFoundException; + +class GetPageEntryByKey +{ + /** + * @var ConfigurationInterface + */ + private $configuration; + + /** + * @var PageEntryInterfaceFactory + */ + private $pageEntryFactory; + + /** + * @param ConfigurationInterface $configuration + * @param PageEntryInterfaceFactory $pageEntryFactory + */ + public function __construct( + ConfigurationInterface $configuration, + PageEntryInterfaceFactory $pageEntryFactory + ) { + $this->configuration = $configuration; + $this->pageEntryFactory = $pageEntryFactory; + } + + /** + * @param string $key + * + * @return PageEntryInterface + * @throws NotFoundException + */ + public function get(string $key) + { + $pages = $this->configuration->getList()['pages'] ?? []; + + if (!array_key_exists($key, $pages)) { + throw new NotFoundException(__('Page with key %1 not found.', $key)); + } + + $item = $this->pageEntryFactory->create(['data' => $pages[$key]]); + + return $item; + } +} diff --git a/etc/di.xml b/etc/di.xml index 9442de59a3b6c33e4e2a4d5eed0e4c1da3ee5c9f..8243449ee6fc4123fef8954e3bd11b104779f91f 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -85,6 +85,8 @@ <argument name="commands" xsi:type="array"> <item name="contentProvisioning.PageList" xsi:type="object">Firegento\ContentProvisioning\Model\Console\PageListCommand</item> <item name="contentProvisioning.BlockList" xsi:type="object">Firegento\ContentProvisioning\Model\Console\BlockListCommand</item> + <item name="contentProvisioning.PageAdd" xsi:type="object">Firegento\ContentProvisioning\Model\Console\AddPageCommand</item> + <item name="contentProvisioning.BlockAdd" xsi:type="object">Firegento\ContentProvisioning\Model\Console\AddBlockCommand</item> </argument> </arguments> </type>