Skip to content

Commit e749701

Browse files
committed
Validate "Command options" #222
1 parent c9f09f9 commit e749701

7 files changed

Lines changed: 62 additions & 4 deletions

File tree

src/Controller/ConfigController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function save(Request $request): JsonResponse
153153
$configuration->setExecutorSettings($executorClass->getStorageValue());
154154

155155
try {
156+
$executorClass->validateConfiguration($configuration);
156157
$configuration->save(['oldId' => $request_configuration]);
157158
} catch (\Exception $e) {
158159
return $this->jsonResponse(['success' => false, 'message' => $e->getMessage()]);

src/Executor/AbstractExecutor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,12 @@ public function setDataFromResource(Configuration $configuration): Configuration
268268

269269
return $configuration;
270270
}
271+
272+
/**
273+
* Validate the configuration settings and throw exception if something is wrong
274+
*/
275+
public function validateConfiguration(Configuration $configuration): void
276+
{
277+
278+
}
271279
}

src/Executor/PimcoreCommand.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
namespace Elements\Bundle\ProcessManagerBundle\Executor;
99

10+
use Elements\Bundle\ProcessManagerBundle\Model\Configuration;
1011
use Elements\Bundle\ProcessManagerBundle\Model\MonitoringItem;
1112
use Elements\Bundle\ProcessManagerBundle\Service\CommandsValidator;
13+
use Exception;
1214
use Pimcore\Tool\Console;
1315

1416
class PimcoreCommand extends AbstractExecutor
@@ -22,6 +24,7 @@ class PimcoreCommand extends AbstractExecutor
2224
* @param null | MonitoringItem $monitoringItem
2325
*
2426
* @return mixed
27+
* @throws Exception
2528
*/
2629
public function getCommand($callbackSettings = [], $monitoringItem = null)
2730
{
@@ -37,7 +40,7 @@ public function getCommand($callbackSettings = [], $monitoringItem = null)
3740
$commands = \Pimcore::getKernel()->getContainer()->get(CommandsValidator::class)->getValidCommands();
3841

3942
if (!array_key_exists($this->getValues()['command'], $commands)) {
40-
throw new \Exception('Invalid command - not in valid commands');
43+
throw new Exception('Invalid command - not in valid commands');
4144
}
4245
/**
4346
* @var \Pimcore\Console\AbstractCommand $commandObject
@@ -55,4 +58,21 @@ public function getCommand($callbackSettings = [], $monitoringItem = null)
5558

5659
return $command;
5760
}
61+
62+
public function validateConfiguration(Configuration $configuration): void
63+
{
64+
65+
if($configuration->getExecutorSettings()){
66+
$settings = $configuration->getExecutorSettingsAsArray();
67+
$values = $settings['values'];
68+
if(!$values['command']){
69+
throw new Exception('Please provide a command.');
70+
}
71+
$commandValidator = \Pimcore::getKernel()->getContainer()->get(CommandsValidator::class);
72+
$commands = \Pimcore::getKernel()->getContainer()->get(CommandsValidator::class)->getValidCommands();
73+
$commandValidator->validateCommandConfiguration($commands[$values['command']], $configuration);
74+
75+
76+
}
77+
}
5878
}

src/Model/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ public function getExecutorSettings(): string
139139
return $this->executorSettings;
140140
}
141141

142+
public function getExecutorSettingsAsArray(): array
143+
{
144+
return json_decode($this->getExecutorSettings(), true);
145+
}
146+
147+
142148
/**
143149
* @return $this
144150
*/

src/Resources/public/js/executor/class/abstractExecutor.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ pimcore.plugin.processmanager.executor.class.abstractExecutor = Class.create(pim
278278
Ext.getCmp('plugin_pm_config_list_panel').store.reload();
279279
}
280280
else {
281-
pimcore.helpers.showNotification(t("error"), t("plugin_pm_config_saved_error"), "error", t(rdata.message));
281+
let errorMessage = t("plugin_pm_config_saved_error");
282+
if(rdata.message) {
283+
errorMessage += ': ' + rdata.message
284+
}
285+
pimcore.helpers.showNotification(t("error"), errorMessage , "error");
282286
}
283287
} catch (e) {
284288
pimcore.helpers.showNotification(t("error"), t("plugin_pm_config_saved_error"), "error");

src/Resources/public/js/executor/class/pimcoreCommand.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pimcore.plugin.processmanager.executor.class.pimcoreCommand = Class.create(pimco
1717
}
1818

1919
this.command = {
20-
fieldLabel: t("plugin_pm_command"),
2120
xtype: "combo",
2221
editable: false,
2322
name: "command",
@@ -26,8 +25,10 @@ pimcore.plugin.processmanager.executor.class.pimcoreCommand = Class.create(pimco
2625
store: store,
2726
mode: "local",
2827
width: "100%",
29-
triggerAction: "all"
28+
triggerAction: "all",
29+
mandatory: true
3030
}
31+
this.command.fieldLabel = this.getFieldLabel('command',this.command)
3132
return this.command;
3233
},
3334

src/Service/CommandsValidator.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Elements\Bundle\ProcessManagerBundle\Service;
99

1010
use Elements\Bundle\ProcessManagerBundle\ExecutionTrait;
11+
use Elements\Bundle\ProcessManagerBundle\Model\Configuration;
12+
use Exception;
1113
use Pimcore\Console\Application;
1214
use Symfony\Component\Console\Command\Command;
1315
use Symfony\Component\Console\Command\LazyCommand;
@@ -40,6 +42,22 @@ public function __construct(string $strategy = 'default', array $whiteList = [],
4042
$this->setBlackList($blackList);
4143
}
4244

45+
public function validateCommandConfiguration(LazyCommand | Command $command, Configuration $configuration): void
46+
{
47+
48+
$settings = $configuration->getExecutorSettingsAsArray();
49+
$values = $settings['values'];
50+
51+
$commandOptions = $values['commandOptions'] ?? '';
52+
53+
54+
55+
//Todo: check if command options are valid
56+
//and throw an error if they are not valid
57+
58+
# throw new Exception('Command options are not valid');
59+
60+
}
4361
/**
4462
* @return array<mixed>
4563
*/

0 commit comments

Comments
 (0)