1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Redislabs\Module\ReJSON\Command; |
5
|
|
|
|
6
|
|
|
use Redislabs\Interfaces\CommandInterface; |
7
|
|
|
use Redislabs\Command\CommandAbstract; |
8
|
|
|
use Redislabs\Module\ReJSON\Exceptions\InvalidDebugSubcommandException; |
9
|
|
|
use Redislabs\Module\ReJSON\Path; |
10
|
|
|
use function in_array; |
11
|
|
|
|
12
|
|
|
final class Debug extends CommandAbstract implements CommandInterface |
13
|
|
|
{ |
14
|
|
|
protected static $command = 'JSON.DEBUG'; |
15
|
|
|
|
16
|
|
|
private static $validSubCommands = ['MEMORY', 'HELP']; |
17
|
|
|
|
18
|
|
|
private function __construct( |
19
|
|
|
string $subcommand |
20
|
|
|
) { |
21
|
|
|
$this->arguments = [$subcommand]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function addArgument($arg) |
25
|
|
|
{ |
26
|
|
|
$this->arguments[] = $arg; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function withArguments(string $key, Path $path) |
30
|
|
|
{ |
31
|
|
|
$new = clone $this; |
32
|
|
|
$new->addArgument($key); |
33
|
|
|
$new->addArgument($path->getPath()); |
34
|
|
|
return $new; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function createCommandWithArguments(string $subcommand, ?string $key, string $path) : CommandInterface |
38
|
|
|
{ |
39
|
|
|
if (!in_array($subcommand, self::$validSubCommands, true)) { |
40
|
|
|
throw new InvalidDebugSubcommandException( |
41
|
|
|
sprintf('%s is not a valid debug subcommand.', $subcommand) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
if ($subcommand === 'MEMORY') { |
45
|
|
|
return self::createCommandWithMemorySubCommandAndArguments($key, $path); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
return self::createCommandWithHelpSubCommandAndArguments(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function createCommandWithMemorySubCommandAndArguments(string $key, string $path) : CommandInterface |
51
|
|
|
{ |
52
|
|
|
$debugObj = new self( |
53
|
|
|
'MEMORY' |
54
|
|
|
); |
55
|
|
|
return $debugObj->withArguments($key, new Path($path)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function createCommandWithHelpSubCommandAndArguments() : CommandInterface |
59
|
|
|
{ |
60
|
|
|
return new self( |
61
|
|
|
'HELP' |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|