MessageValidator::validate0100()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 2
b 0
f 0
nc 5
nop 2
dl 0
loc 27
rs 9.5222
ccs 13
cts 13
cp 1
crap 5
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the php-gelf package.
6
 *
7
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Gelf;
14
15
use RuntimeException;
16
17
/**
18
 * Validates a given message according to the GELF standard
19
 *
20
 * @author Benjamin Zikarsky <[email protected]>
21
 * @author Joe Green
22
 */
23
class MessageValidator implements MessageValidatorInterface
24 12
{
25
    public function validate(MessageInterface $message, ?string &$reason = null): bool
26 12
    {
27 12
        switch ($message->getVersion()) {
28 5
            case "1.0":
29 7
                return $this->validate0100($message, $reason);
30 6
            case "1.1":
31
                return $this->validate0101($message, $reason);
32
        }
33 1
34
        throw new RuntimeException(
35 1
            sprintf(
36 1
                "No validator for message version '%s'",
37
                $message->getVersion()
38
            )
39
        );
40
    }
41
42
    /**
43
     * Validates a message according to 1.0 standard
44
     */
45
    public function validate0100(MessageInterface $message, ?string &$reason = null): bool
46
    {
47
        if (self::isEmpty($message->getHost())) {
48 12
            $reason = "host not set";
49
50 12
            return false;
51 2
        }
52
53 2
        if (self::isEmpty($message->getShortMessage())) {
54
            $reason = "short-message not set";
55
56 10
            return false;
57 2
        }
58
59 2
        if (self::isEmpty($message->getVersion())) {
60
            $reason = "version not set";
61
62 8
            return false;
63 1
        }
64
65 1
        if ($message->hasAdditional('id')) {
66
            $reason = "additional field 'id' is not allowed";
67
68 7
            return false;
69 2
        }
70
71 2
        return true;
72
    }
73
74 5
    /**
75
     * Validates a message according to 1.1 standard
76
     */
77
    public function validate0101(MessageInterface $message, ?string &$reason = null): bool
78
    {
79
        // 1.1 incorporates 1.0 validation standard
80
        if (!$this->validate0100($message, $reason)) {
81
            return false;
82
        }
83
84 6
        foreach ($message->getAllAdditionals() as $key => $value) {
85
            if (!preg_match('#^[\w\.\-]*$#', $key)) {
86
                $reason = sprintf(
87 6
                    "additional key '%s' contains invalid characters",
88 3
                    $key
89
                );
90
91 3
                return false;
92 1
            }
93 1
        }
94 1
95
        return true;
96
    }
97
98 1
    /**
99
     * Checks that a given scalar will later translate
100
     * to a non-empty message element
101
     *
102 2
     * Fails on null, false and empty strings
103
     */
104
    public static function isEmpty(mixed $scalar): bool
105
    {
106
        return strlen((string)$scalar) < 1;
107
    }
108
}
109