MiddlewareResolverException::convertToString()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Request Handler
5
 *
6
 * Platine Request Handler is the implementation of PSR 15
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Request Handler
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
declare(strict_types=1);
32
33
namespace Platine\Http\Handler\Exception;
34
35
use InvalidArgumentException;
36
use Platine\Http\Handler\MiddlewareInterface;
37
use Platine\Http\Handler\RequestHandlerInterface;
38
use Platine\Http\ResponseInterface;
39
40
/**
41
 * @class MiddlewareResolverException
42
 * @package Platine\Http\Handler\Exception
43
 */
44
class MiddlewareResolverException extends InvalidArgumentException
45
{
46
    /**
47
     * @param  mixed $response
48
     * @return self
49
     */
50
    public static function forCallableMissingResponse(mixed $response): self
51
    {
52
        return new self(
53
            sprintf(
54
                'Callable handler must returned an instance of "%s"; but received "%s"' .
55
                        ' Interface as well as a PHP callable.',
56
                ResponseInterface::class,
57
                self::convertToString($response)
58
            )
59
        );
60
    }
61
62
    /**
63
     * @param  string $handler
64
     * @return self
65
     */
66
    public static function forStringNotConvertedToInstance(string $handler): self
67
    {
68
        return new self(
69
            sprintf(
70
                'String handler %s must be a name of class or an identifier of' .
71
                        ' a container definition that implements "%s" or "%s" interface.',
72
                $handler,
73
                MiddlewareInterface::class,
74
                RequestHandlerInterface::class
75
            )
76
        );
77
    }
78
79
    /**
80
     * Convert the given argument to string
81
     * @param  mixed $data
82
     * @return string
83
     */
84
    protected static function convertToString(mixed $data): string
85
    {
86
        return is_scalar($data)
87
                ? (string) $data
88
                : (is_object($data)
89
                    ? get_class($data)
90
                    : gettype($data)
91
                );
92
    }
93
}
94