GetBlockTemplateValidator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 36
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateCapabilities() 0 13 4
A validateMode() 0 4 3
A validate() 0 9 2
1
<?php
2
3
namespace ZenCash\Rpc\Validation\Mining;
4
5
use ZenCash\Rpc\Validation\Exception\JsonFormatException;
6
use ZenCash\Rpc\Validation\JsonValidator;
7
8
final class GetBlockTemplateValidator implements JsonValidator
9
{
10
    /** @throws JsonFormatException */
11 8
    public static function validate(string $json): void
12
    {
13 8
        if (!$decoded = json_decode($json)) {
14 1
            throw new JsonFormatException("JSON object has an invalid structure:\n$json");
15
        }
16
17 7
        self::validateMode($decoded);
18
19 6
        self::validateCapabilities($decoded);
20 4
    }
21
22
    /** @throws JsonFormatException */
23 7
    private static function validateMode(object $decoded): void
24
    {
25 7
        if (($decoded->mode ?? false) && ($decoded->mode !== 'template')) {
26 1
            throw new JsonFormatException("JSON property `mode` must be either 'template' or omitted.");
27
        }
28 6
    }
29
30
    /** @throws JsonFormatException */
31 6
    private static function validateCapabilities(object $decoded): void
32
    {
33 6
        if ($decoded->capabilities ?? false) {
34 3
            if (!is_array($decoded->capabilities)) {
35 1
                throw new JsonFormatException(
36
                    'JSON property `capabilities` must be of type `array`, `' .
37 1
                    gettype($decoded->capabilities) . '` provided.'
38
                );
39
            }
40
41 2
            array_walk($decoded->capabilities, function($item) {
42 2
                if (!is_string($item)) {
43 1
                    throw new JsonFormatException("JSON property `capabilities` must only contain strings.");
44
                }
45 2
            });
46
        }
47 4
    }
48
}
49