Skip to content

Commit 14c8527

Browse files
committed
feat: Added better output message handling
1 parent a5166ed commit 14c8527

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/SubCommands/SubCommand.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class SubCommand {
4545
*/
4646
public string $successMessage;
4747

48+
/**
49+
* @var string Message to output if --remove command execution was successful
50+
*/
51+
public string $removalMessage;
52+
4853
/**
4954
* @var array Command line arguments passed as $assoc_args
5055
*/
@@ -117,6 +122,24 @@ private function setRuleContent() : string|array {
117122
return $result;
118123
}
119124

125+
/**
126+
* Returns the output message
127+
*
128+
* @param string $type
129+
*
130+
* @return string
131+
* @todo I am not too happy about this
132+
*/
133+
private function getOutputMessage(string $type = 'success') : string {
134+
$message = $this->{$type. 'Message'} ;
135+
136+
if($this->serverType === 'nginx') {
137+
$message .= PHP_EOL . 'Since you are using nginx you need to restart web server manually. If you copied rules manually, this command will have no effect.';
138+
}
139+
140+
return $message;
141+
}
142+
120143
/**
121144
* Outputs the result of command execution
122145
*
@@ -135,10 +158,21 @@ public function output() {
135158
} else {
136159
try {
137160
$fileManager = new FileManager($this->filePath);
138-
$result = $fileManager->add($this->ruleContent, $this->ruleName);
139161

140-
if($result) {
141-
WP_CLI::success($this->successMessage);
162+
if(isset($this->commandArguments['remove']) && $this->commandArguments['remove'] === true) {
163+
//We need to remove the rule from file
164+
$result = $fileManager->remove($this->ruleName);
165+
166+
if($result) {
167+
WP_CLI::success($this->getOutputMessage('removal'));
168+
}
169+
} else {
170+
//Add the rule
171+
$result = $fileManager->add($this->ruleContent, $this->ruleName);
172+
173+
if($result) {
174+
WP_CLI::success($this->getOutputMessage('success'));
175+
}
142176
}
143177
} catch(FileDoesNotExist|RuleAlreadyExist|FileIsNotWritable|FileIsNotReadable $e) {
144178
WP_CLI::error($e->getMessage());

0 commit comments

Comments
 (0)