Get::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
cc 2
nc 1
nop 2
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\Path;
9
10
final class Get extends CommandAbstract implements CommandInterface
11
{
12
    protected static $command = 'JSON.GET';
13
14
    private function __construct(
15
        string $key,
16
        array $paths
17
    ) {
18
        $paths = array_map(function (Path $path) {
19
            return $path->getPath();
20
        }, $paths);
21
        $this->arguments = [$key];
22
        $this->arguments = array_merge($this->arguments, $paths);
23
        $this->responseCallback = function ($result) {
24
            if ($result) {
25
                return json_decode($result);
26
            }
27
            return null;
28
        };
29
    }
30
31
    public static function createCommandWithArguments(string $key, $paths = '.') : CommandInterface
32
    {
33
        $pathObjects = [];
34
        if (!is_array($paths)) {
35
            $paths = (array) $paths;
36
        }
37
        foreach ($paths as $path) {
38
            $pathObjects[] = new Path($path);
39
        }
40
41
        return new self(
42
            $key,
43
            $pathObjects
44
        );
45
    }
46
}
47