create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
eloc 6
cc 1
crap 1
nc 1
nop 1
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 8
    public static function create($handler): self
24
    {
25 8
        return new self(sprintf(
26 8
            'Handler "%s" must be an instance or name of a class that implements `%s` or `%s`'
27 8
            . ' interface as well as a PHP callable or array of such arguments.',
28 8
            self::convertToString($handler),
29 8
            MiddlewareInterface::class,
30 8
            RequestHandlerInterface::class
31 8
        ));
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 6
            'Callable handler must returned an instance of `Psr\Http\Message\ResponseInterface`; received "%s".',
42 6
            self::convertToString($response)
43 6
        ));
44
    }
45
46
    /**
47
     * @param string $handler
48
     * @return self
49
     */
50 4
    public static function forStringNotConvertedToInstance(string $handler): self
51
    {
52 4
        return new self(sprintf(
53 4
            'String handler "%s" must be a name of a class or an identifier of'
54 4
            . ' a container definition that implements `%s` or `%s` interface.',
55 4
            $handler,
56 4
            MiddlewareInterface::class,
57 4
            RequestHandlerInterface::class
58 4
        ));
59
    }
60
61
    /**
62
     * @param mixed $data
63
     * @return string
64
     */
65 14
    private static function convertToString($data): string
66
    {
67 14
        return is_scalar($data) ? (string) $data : (is_object($data) ? get_class($data) : gettype($data));
68
    }
69
}
70