Skip to content

Commit 8ee63e7

Browse files
authored
Merge pull request #40 from TD-ellguthe/develop
Add block reset command
2 parents e65c674 + b590f29 commit 8ee63e7

File tree

3 files changed

+184
-1
lines changed

3 files changed

+184
-1
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2021 TechDivision GmbH <[email protected]> - TechDivision GmbH
4+
* All rights reserved
5+
*
6+
* This product includes proprietary software developed at TechDivision GmbH, Germany
7+
* For more information see http://www.techdivision.com/
8+
*
9+
* To obtain a valid license for using this software please contact us at
10+
11+
*/
12+
declare(strict_types=1);
13+
14+
namespace Firegento\ContentProvisioning\Model\Console;
15+
16+
use Firegento\ContentProvisioning\Api\Data\BlockEntryInterface;
17+
use Firegento\ContentProvisioning\Model\Command\ApplyBlockEntry;
18+
use Firegento\ContentProvisioning\Model\Command\ApplyMediaFiles;
19+
use Firegento\ContentProvisioning\Model\Query\GetBlockEntryList;
20+
use Magento\Framework\Console\Cli;
21+
use Symfony\Component\Console\Command\Command;
22+
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Console\Input\InputOption;
24+
use Symfony\Component\Console\Output\OutputInterface;
25+
26+
/**
27+
* @copyright Copyright (c) 2021 TechDivision GmbH <[email protected]> - TechDivision GmbH
28+
*
29+
* @link https://www.techdivision.com/
30+
* @author Team Zero <[email protected]>
31+
*/
32+
class BlockResetCommand extends Command
33+
{
34+
public const COMMAND = 'content-provisioning:block:reset';
35+
public const PARAM_KEY = 'key';
36+
public const PARAM_IDENTIFIER = 'identifier';
37+
38+
private GetBlockEntryList $getBlockEntryList;
39+
private ApplyBlockEntry $applyBlockEntry;
40+
private ApplyMediaFiles $applyMediaFiles;
41+
42+
/**
43+
* @param GetBlockEntryList $getBlockEntryList
44+
* @param ApplyBlockEntry $applyBlockEntry
45+
* @param ApplyMediaFiles $applyMediaFiles
46+
*/
47+
public function __construct(
48+
GetBlockEntryList $getBlockEntryList,
49+
ApplyBlockEntry $applyBlockEntry,
50+
ApplyMediaFiles $applyMediaFiles
51+
) {
52+
parent::__construct();
53+
54+
$this->getBlockEntryList = $getBlockEntryList;
55+
$this->applyBlockEntry = $applyBlockEntry;
56+
$this->applyMediaFiles = $applyMediaFiles;
57+
}
58+
59+
/**
60+
* Configures the current command.
61+
*/
62+
protected function configure(): void
63+
{
64+
$this->setName(self::COMMAND);
65+
$this->setDescription('Reset CMS content');
66+
$this->addOption(self::PARAM_KEY, 'k', InputOption::VALUE_OPTIONAL, 'Key');
67+
$this->addOption(self::PARAM_IDENTIFIER, 'i', InputOption::VALUE_OPTIONAL, 'Identifier');
68+
69+
parent::configure();
70+
}
71+
72+
/**
73+
* @param InputInterface $input
74+
* @param OutputInterface $output
75+
* @return int
76+
*/
77+
protected function execute(InputInterface $input, OutputInterface $output): int
78+
{
79+
try {
80+
[$search, $type] = $this->parseParams($input);
81+
} catch (\Exception $e) {
82+
$output->writeln('<error>' . $e->getMessage() . '</error>' . "\n");
83+
return Cli::RETURN_FAILURE;
84+
}
85+
86+
$blockEntries = $this->getBlockEntries($search, $type);
87+
$updateCount = 0;
88+
89+
if (empty($blockEntries)) {
90+
$output->writeln('<error>block entry not found for ' . $type . ' "' . $search . '"</error>' . "\n");
91+
return Cli::RETURN_FAILURE;
92+
}
93+
94+
try {
95+
foreach ($blockEntries as $blockEntry) {
96+
$this->applyBlockEntry->execute($blockEntry);
97+
$this->applyMediaFiles->execute($blockEntry);
98+
$updateCount++;
99+
}
100+
} catch (\Exception $exception) {
101+
$output->writeln('<error>' . $exception->getMessage() . '</error>' . "\n");
102+
return Cli::RETURN_FAILURE;
103+
}
104+
105+
$pluralS = ($updateCount > 1) ? 's' : '';
106+
$output->writeln('<info>' . $updateCount . ' block' . $pluralS . ' successfully updated</info>');
107+
108+
return Cli::RETURN_SUCCESS;
109+
}
110+
111+
/**
112+
* @param InputInterface $input
113+
* @return string[]
114+
* @throws \Exception
115+
*/
116+
private function parseParams(InputInterface $input): array
117+
{
118+
$key = $input->getOption(self::PARAM_KEY);
119+
$identifier = $input->getOption(self::PARAM_IDENTIFIER);
120+
121+
if ((!empty($key)) && (!empty($identifier))) {
122+
throw new \Exception('Provide either "' . self::PARAM_KEY . '" or "' . self::PARAM_IDENTIFIER . '", not both!');
123+
}
124+
125+
if (!empty($key)) {
126+
$search = $key;
127+
$type = self::PARAM_KEY;
128+
} elseif (!empty($identifier)) {
129+
$search = $identifier;
130+
$type = self::PARAM_IDENTIFIER;
131+
} else {
132+
throw new \Exception('Provide either "' . self::PARAM_KEY . '" or "' . self::PARAM_IDENTIFIER . '"!');
133+
}
134+
135+
return [$search, $type];
136+
}
137+
138+
/**
139+
* @param string $search
140+
* @param string $type
141+
* @return BlockEntryInterface[]
142+
*/
143+
private function getBlockEntries(string $search, string $type = self::PARAM_KEY): array
144+
{
145+
if (!in_array($type, [self::PARAM_KEY, self::PARAM_IDENTIFIER])) {
146+
return [];
147+
}
148+
149+
$method = 'get' . ucfirst($type);
150+
$entries = [];
151+
152+
foreach ($this->getBlockEntryList->get() as $blockEntry) {
153+
if ($blockEntry->$method() === $search) {
154+
$entries[] = $blockEntry;
155+
}
156+
}
157+
158+
return $entries;
159+
}
160+
}

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Only the latest patch versions of the following Magento versions are covered by
1010

1111
| PHP | Magento 2.3 | Magento 2.4 |
1212
|:---: |:---:|:---:|
13-
| 7.3 | [![Build Status](https://travis-matrix-badges.herokuapp.com/repos/magento-hackathon/m2-content-provisioning/branches/develop/1)](https://travis-ci.org/magento-hackathon/m2-content-provisioning) | [![Build Status](https://travis-matrix-badges.herokuapp.com/repos/magento-hackathon/m2-content-provisioning/branches/develop/2)](https://travis-ci.org/magento-hackathon/m2-content-provisioning) |
1413
| 7.4 | - | [![Build Status](https://travis-matrix-badges.herokuapp.com/repos/magento-hackathon/m2-content-provisioning/branches/develop/3)](https://travis-ci.org/magento-hackathon/m2-content-provisioning) |
1514

1615
## The idea behind this module
@@ -168,6 +167,29 @@ vi dev/tests/integration/etc/install-config-mysql.php
168167
php vendor/bin/phpunit -c $(pwd)/vendor/firegento/magento2-content-provisioning/Test/Integration/phpunit.xml
169168
```
170169

170+
## Console Commands
171+
```shell
172+
# reset a CMS block (all localizations) by its key
173+
bin/magento content-provisioning:block:reset --key "myKey"
174+
bin/magento content-provisioning:block:reset -k "myKey"
175+
176+
# reset a CMS blocks by its identifier
177+
bin/magento content-provisioning:block:reset --identifier "myIdentifier"
178+
bin/magento content-provisioning:block:reset --i "myIdentifier"
179+
180+
# add a CMS block by key
181+
bin/magento content-provisioning:block:apply "myKey"
182+
183+
# add a CMS page by key
184+
bin/magento content-provisioning:page:apply "myKey"
185+
186+
# list all configured CMS block entries
187+
bin/magento content-provisioning:block:list
188+
189+
# list all configured CMS page entries
190+
bin/magento content-provisioning:page:list
191+
```
192+
171193
## Issues and planned features
172194

173195
See issues to see what's planed next: https://github.com/magento-hackathon/m2-content-provisioning/issues

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<item name="contentProvisioning.BlockList" xsi:type="object">Firegento\ContentProvisioning\Model\Console\BlockListCommand</item>
8888
<item name="contentProvisioning.PageAdd" xsi:type="object">Firegento\ContentProvisioning\Model\Console\AddPageCommand</item>
8989
<item name="contentProvisioning.BlockAdd" xsi:type="object">Firegento\ContentProvisioning\Model\Console\AddBlockCommand</item>
90+
<item name="contentProvisioning.BlockReset" xsi:type="object">Firegento\ContentProvisioning\Model\Console\BlockResetCommand</item>
9091
</argument>
9192
</arguments>
9293
</type>

0 commit comments

Comments
 (0)