UnresolvableHydratorException   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 75
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A forModel() 0 13 2
1
<?php
2
/**
3
 * Incoming
4
 *
5
 * @author    Trevor Suarez (Rican7)
6
 * @copyright (c) Trevor Suarez
7
 * @link      https://github.com/Rican7/incoming
8
 * @license   MIT
9
 */
10
11
declare(strict_types=1);
12
13
namespace Incoming\Hydrator\Exception;
14
15
use Throwable;
16
use UnexpectedValueException;
17
18
/**
19
 * An exception to be thrown when a hydrator can't be resolved, whether by
20
 * automatic lookup or not, and usually for a given model.
21
 */
22
class UnresolvableHydratorException extends UnexpectedValueException
23
{
24
25
    /**
26
     * Constants
27
     */
28
29
    /**
30
     * The default exception message.
31
     *
32
     * @var string
33
     */
34
    const DEFAULT_MESSAGE = 'Unable to resolve a hydrator';
35
36
    /**
37
     * The exception code for when a model is used as the resolver argument.
38
     *
39
     * @var int
40
     */
41
    const CODE_FOR_MODEL = 1;
42
43
    /**
44
     * The message extension (appended to the default message) for when a model
45
     * is used as the hydrator resolver argument.
46
     *
47
     * @var string
48
     */
49
    const MESSAGE_EXTENSION_FOR_MODEL = ' for the given model';
50
51
    /**
52
     * The message extension format for providing type information.
53
     *
54
     * @var string
55
     */
56
    const MESSAGE_EXTENSION_TYPE_FORMAT = ' of type `%s`';
57
58
59
    /**
60
     * Properties
61
     */
62
63
    /**
64
     * The exception message.
65
     *
66
     * @var string
67
     */
68
    protected $message = self::DEFAULT_MESSAGE;
69
70
71
    /**
72
     * Methods
73
     */
74
75
    /**
76
     * Create an exception instance for a problem resolving a hydrator for a
77
     * given model.
78
     *
79
     * @param mixed $model The model to hydrate.
80
     * @param int $code The exception code.
81
     * @param Throwable|null $previous A previous exception used for chaining.
82
     * @return static The newly created exception.
83
     */
84 12
    public static function forModel(
85
        $model,
86
        int $code = self::CODE_FOR_MODEL,
87
        Throwable $previous = null
88
    ): self {
89 12
        $message = self::DEFAULT_MESSAGE . self::MESSAGE_EXTENSION_FOR_MODEL;
90
91 12
        $message .= sprintf(
92 12
            self::MESSAGE_EXTENSION_TYPE_FORMAT,
93 12
            is_object($model) ? get_class($model) : gettype($model)
94
        );
95
96 12
        return new static($message, $code, $previous);
97
    }
98
}
99