Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — 0.9 (#692)
by
unknown
02:58
created

ValidationException::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
namespace Respect\Validation\Exceptions;
3
4
use DateTime;
5
use InvalidArgumentException;
6
7
class ValidationException extends InvalidArgumentException implements ValidationExceptionInterface
8
{
9
    const MODE_DEFAULT = 1;
10
    const MODE_NEGATIVE = 2;
11
    const STANDARD = 0;
12
    public static $defaultTemplates = array(
13
        self::MODE_DEFAULT => array(
14
            self::STANDARD => 'Data validation failed for %s',
15
        ),
16
        self::MODE_NEGATIVE => array(
17
            self::STANDARD => 'Data validation failed for %s',
18
        ),
19
    );
20
    protected $id = 'validation';
21
    protected $mode = self::MODE_DEFAULT;
22
    protected $name = '';
23
    protected $template = '';
24
    protected $params = array();
25
26 547
    public static function format($template, array $vars = array())
27
    {
28 547
        return preg_replace_callback(
29 547
            '/{{(\w+)}}/',
30 547
            function ($match) use ($vars) {
31 546
                return isset($vars[$match[1]]) ? $vars[$match[1]] : $match[0];
32 547
            },
33
            $template
34 547
        );
35
    }
36
37 552
    public static function stringify($value)
38
    {
39 552
        if (is_string($value)) {
40 544
            return $value;
41 548
        } elseif (is_array($value)) {
42 98
            return 'Array'; //FIXME
43 546
        } elseif (is_object($value)) {
44 68
            return static::stringifyObject($value);
45
        } else {
46 542
            return (string) $value;
47
        }
48
    }
49
50 68
    public static function stringifyObject($value)
51
    {
52 68
        if (method_exists($value, '__toString')) {
53
            return (string) $value;
54 68
        } elseif ($value instanceof DateTime) {
55 5
            return $value->format('Y-m-d H:i:s');
56
        } else {
57 63
            return "Object of class ".get_class($value);
58
        }
59
    }
60
61 4
    public function __toString()
62
    {
63 4
        return $this->getMainMessage();
64
    }
65
66 409
    public function chooseTemplate()
67
    {
68 409
        return key(static::$defaultTemplates[$this->mode]);
69
    }
70
71 542
    public function configure($name, array $params = array())
72
    {
73 542
        $this->setName($name);
74 542
        $this->setParams($params);
75 542
        $this->message = $this->getMainMessage();
76 542
        $this->setId($this->guessId());
77
78 542
        return $this;
79
    }
80
81 544
    public function getName()
82
    {
83 544
        return $this->name;
84
    }
85
86 4
    public function getId()
87
    {
88 4
        return $this->id;
89
    }
90
91 544
    public function getMainMessage()
92
    {
93 544
        $vars = $this->getParams();
94 544
        $vars['name'] = $this->getName();
95 544
        $template = $this->getTemplate();
96 544
        if (isset($vars['translator']) && is_callable($vars['translator'])) {
97
            $template = call_user_func($vars['translator'], $template);
98
        }
99
100 544
        return static::format($template, $vars);
101
    }
102
103 168
    public function getParam($name)
104
    {
105 168
        return $this->hasParam($name) ? $this->params[$name] : false;
106
    }
107
108 544
    public function getParams()
109
    {
110 544
        return $this->params;
111
    }
112
113 544
    public function getTemplate()
114
    {
115 544
        if (!empty($this->template)) {
116 11
            return $this->template;
117
        } else {
118 544
            return $this->template = $this->buildTemplate();
119
        }
120
    }
121
122 168
    public function hasParam($name)
123
    {
124 168
        return isset($this->params[$name]);
125
    }
126
127 542
    public function setId($id)
128
    {
129 542
        $this->id = $id;
130
131 542
        return $this;
132
    }
133
134 542
    public function setName($name)
135
    {
136 542
        $this->name = static::stringify($name);
137
138 542
        return $this;
139
    }
140
141 17
    public function setMode($mode)
142
    {
143 17
        $this->mode = $mode;
144 17
        $this->template = $this->buildTemplate();
145
146 17
        return $this;
147
    }
148
149 545
    public function setParam($key, $value)
150
    {
151 545
        $this->params[$key] = ($key == 'translator') ? $value : static::stringify($value);
152
153 545
        return $this;
154
    }
155
156 542
    public function setParams(array $params)
157
    {
158 542
        foreach ($params as $key => $value) {
159 540
            $this->setParam($key, $value);
160 542
        }
161
162 542
        return $this;
163
    }
164
165 6
    public function setTemplate($template)
166
    {
167 6
        $this->template = $template;
168
169 6
        return $this;
170
    }
171
172 544
    protected function buildTemplate()
173
    {
174 544
        $templateKey = $this->chooseTemplate();
175
176 544
        return static::$defaultTemplates[$this->mode][$templateKey];
177
    }
178
179 542
    protected function guessId()
180
    {
181 542
        if (!empty($this->id) && $this->id != 'validation') {
182
            return $this->id;
183
        }
184
185 542
        $pieces = explode('\\', get_called_class());
186 542
        $exceptionClassShortName = end($pieces);
187 542
        $ruleClassShortName = str_replace('Exception', '', $exceptionClassShortName);
188 542
        $ruleName = lcfirst($ruleClassShortName);
189
190 542
        return $ruleName;
191
    }
192
}
193