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 — master (#957)
by Henrique
02:18
created

ValidationException::createMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Respect\Validation\Exceptions;
15
16
use InvalidArgumentException;
17
use function call_user_func;
18
use function Respect\Stringifier\stringify;
19
20
/**
21
 * Default exception class for rule validations.
22
 *
23
 * @author Alexandre Gomes Gaigalas <[email protected]>
24
 * @author Henrique Moody <[email protected]>
25
 */
26
class ValidationException extends InvalidArgumentException implements ExceptionInterface
27
{
28
    public const MODE_DEFAULT = 'default';
29
    public const MODE_NEGATIVE = 'negative';
30
    public const STANDARD = 'standard';
31
32
    /**
33
     * Contains the default templates for exception message.
34
     *
35
     * @var array
36
     */
37
    public static $defaultTemplates = [
38
        self::MODE_DEFAULT => [
39
            self::STANDARD => 'Data validation failed for {{name}}',
40
        ],
41
        self::MODE_NEGATIVE => [
42
            self::STANDARD => 'Data validation failed for {{name}}',
43
        ],
44
    ];
45
46
    /**
47
     * @var mixed
48
     */
49
    private $input;
50
51
    /**
52
     * @var string
53
     */
54
    private $id;
55
56
    /**
57
     * @var string
58
     */
59
    private $mode = self::MODE_DEFAULT;
60
61
    /**
62
     * @var array
63
     */
64
    private $params = [];
65
66
    /**
67
     * @var callable
68
     */
69
    private $translator;
70
71
    /**
72
     * @var string
73
     */
74
    private $template;
75
76
    public function __construct($input, string $id, array $params, callable $translator)
77
    {
78
        $this->input = $input;
79
        $this->id = $id;
80
        $this->params = $params;
81
        $this->translator = $translator;
82
        $this->template = $this->chooseTemplate();
83
        $this->message = $this->createMessage();
84
    }
85
86
    public function updateMode(string $mode): void
87
    {
88
        $this->mode = $mode;
89
        $this->message = $this->createMessage();
90
    }
91
92
    public function updateTemplate(string $template): void
93
    {
94
        $this->template = $template;
95
        $this->message = $this->createMessage();
96
    }
97
98
    public function updateParams(array $params): void
99
    {
100
        $this->params = $params;
101
        $this->message = $this->createMessage();
102
    }
103
104
    public function getId(): string
105
    {
106
        return $this->id;
107
    }
108
109
    public function getParam($name)
110
    {
111
        return $this->params[$name] ?? null;
112
    }
113
114
    public function getParams(): array
115
    {
116
        return $this->params;
117
    }
118
119
    public function __toString(): string
120
    {
121
        return $this->getMessage();
122
    }
123
124
    public function chooseTemplate()
125
    {
126
        return key(static::$defaultTemplates[$this->mode]);
127
    }
128
129
    protected function hasCustomTemplate(): bool
130
    {
131
        return false === isset(static::$defaultTemplates[$this->mode][$this->template]);
132
    }
133
134
    private function createMessage(): string
135
    {
136
        $template = $this->createTemplate($this->mode, $this->template);
137
        $params = $this->getParams();
138
        $params['name'] = $params['name'] ?? stringify($this->input);
139
        $params['input'] = $this->input;
140
141
        return $this->format($template, $params);
142
    }
143
144
    private function createTemplate(string $mode, string $template): string
145
    {
146
        if (isset(static::$defaultTemplates[$mode][$template])) {
147
            $template = static::$defaultTemplates[$mode][$template];
148
        }
149
150
        return call_user_func($this->translator, $template);
151
    }
152
153
    private function format($template, array $vars = []): string
154
    {
155
        return preg_replace_callback(
156
            '/{{(\w+)}}/',
157
            function ($match) use ($vars) {
158
                if (!isset($vars[$match[1]])) {
159
                    return $match[0];
160
                }
161
162
                $value = $vars[$match[1]];
163
                if ('name' == $match[1] && is_string($value)) {
164
                    return $value;
165
                }
166
167
                return stringify($value);
168
            },
169
            $template
170
        );
171
    }
172
}
173