Passed
Push — develop ( 906464...fc23db )
by nguereza
18:23 queued 16:08
created

forStringNotConvertedToInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
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
class MiddlewareResolverException extends InvalidArgumentException
41
{
42
    /**
43
     * Create the Exception instance
44
     * @param  mixed $handler
45
     * @return self
46
     */
47
    public static function create($handler): self
48
    {
49
        return new self(
50
            sprintf(
51
                'Handler %s must be an instance or name of class that implements "%s" or "%s"' .
52
                        ' Interface as well as a PHP callable.',
53
                self::convertToString($handler),
54
                MiddlewareInterface::class,
55
                RequestHandlerInterface::class
56
            )
57
        );
58
    }
59
60
    /**
61
     * @param  mixed $response
62
     * @return self
63
     */
64
    public static function forCallableMissingResponse($response): self
65
    {
66
        return new self(
67
            sprintf(
68
                'Callable handler must returned an instance "%s"; but received "%s"' .
69
                        ' Interface as well as a PHP callable.',
70
                ResponseInterface::class,
71
                self::convertToString($response)
72
            )
73
        );
74
    }
75
76
    /**
77
     * @param  string $handler
78
     * @return self
79
     */
80
    public static function forStringNotConvertedToInstance(string $handler): self
81
    {
82
        return new self(
83
            sprintf(
84
                'String handler %s must be a name of class or an identifier of' .
85
                        ' a container definition that implements "%s" or "%s" interface.',
86
                $handler,
87
                MiddlewareInterface::class,
88
                RequestHandlerInterface::class
89
            )
90
        );
91
    }
92
93
    /**
94
     * Convert the given argument to string
95
     * @param  mixed $data
96
     * @return string
97
     */
98
    protected static function convertToString($data): string
99
    {
100
        return is_scalar($data)
101
                ? (string) $data
102
                : (is_object($data)
103
                    ? get_class($data)
104
                    : gettype($data)
105
                );
106
    }
107
}
108