Passed
Push — master ( f25e4d...a2be21 )
by Benjamin
01:49
created

MessageValidator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 33
c 3
b 0
f 0
dl 0
loc 95
ccs 36
cts 36
cp 1
rs 10
wmc 13

4 Methods

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