Passed
Push — master ( 3ed620...cf8085 )
by Evgeniy
07:05
created

convertToString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Runner\Exception;
6
7
use InvalidArgumentException;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
use function gettype;
12
use function get_class;
13
use function is_object;
14
use function is_scalar;
15
use function sprintf;
16
17
class InvalidMiddlewareResolverHandlerException extends InvalidArgumentException
18
{
19
    /**
20
     * @param mixed $handler
21
     * @return self
22
     */
23 12
    public static function create($handler): self
24
    {
25 12
        return new self(sprintf(
26
            'Handler "%s" must be an instance or name of a class that implements `%s` or `%s`'
27
            . ' interface as well as a PHP callable or array of such arguments.',
28 12
            self::convertToString($handler),
29
            MiddlewareInterface::class,
30 12
            RequestHandlerInterface::class
31
        ));
32
    }
33
34
    /**
35
     * @param mixed $response
36
     * @return self
37
     */
38 6
    public static function forCallableMissingResponse($response): self
39
    {
40 6
        return new self(sprintf(
41
            'Callable handler must returned an instance of `Psr\Http\Message\ResponseInterface`; received "%s".',
42 6
            self::convertToString($response)
43
        ));
44
    }
45
46
    /**
47
     * @param string $handler
48
     * @return self
49
     */
50
    public static function forStringNotConvertedToInstance(string $handler): self
51
    {
52
        return new self(sprintf(
53
            'String handler "%s" must be a name of a class or an identifier of'
54
            . ' a container definition that implements `%s` or `%s` interface.',
55
            $handler,
56
            MiddlewareInterface::class,
57
            RequestHandlerInterface::class
58
        ));
59
    }
60
61
    /**
62
     * @param mixed $data
63
     * @return string
64
     */
65 18
    private static function convertToString($data): string
66
    {
67 18
        return is_scalar($data) ? (string) $data : (is_object($data) ? get_class($data) : gettype($data));
68
    }
69
}
70