Completed
Pull Request — master (#6)
by Trevor N.
01:14
created

UnresolvableBuilderException   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 0
dl 0
loc 61
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A forType() 0 9 1
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 builder can't be resolved, whether by
20
 * automatic lookup or not, and usually for a given type
21
 */
22
class UnresolvableBuilderException extends UnexpectedValueException
23
{
24
25
    /**
26
     * Constants
27
     */
28
29
    /**
30
     * @var string
31
     */
32
    const DEFAULT_MESSAGE = 'Unable to resolve a builder';
33
34
    /**
35
     * The exception code for when a type is used as the resolver argument
36
     *
37
     * @var int
38
     */
39
    const CODE_FOR_TYPE = 1;
40
41
    /**
42
     * The message extension (appended to the default message) for when a type
43
     * is used as the builder resolver argument
44
     *
45
     * @var string
46
     */
47
    const MESSAGE_EXTENSION_FOR_TYPE = ' for the given type `%s`';
48
49
50
    /**
51
     * Properties
52
     */
53
54
    /**
55
     * @var string
56
     */
57
    protected $message = self::DEFAULT_MESSAGE;
58
59
60
    /**
61
     * Methods
62
     */
63
64
    /**
65
     * Create an exception instance for a problem resolving a builder for a
66
     * given type
67
     *
68
     * @param string $type The type to build
69
     * @param int $code The exception code
70
     * @param Throwable|null $previous A previous exception used for chaining
71
     * @return static The newly created exception
72
     */
73 9
    public static function forType(
74
        string $type,
75
        int $code = self::CODE_FOR_TYPE,
76
        Throwable $previous = null
77
    ): self {
78 9
        $message = self::DEFAULT_MESSAGE . sprintf(self::MESSAGE_EXTENSION_FOR_TYPE, $type);
79
80 9
        return new static($message, $code, $previous);
81
    }
82
}
83