IncompatibleProcessException   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 84
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A forRequiredContextCompatibility() 0 16 4
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 Incoming\Hydrator\Builder;
16
use Incoming\Hydrator\Hydrator;
17
use Throwable;
18
use UnexpectedValueException;
19
20
/**
21
 * An exception to be thrown when a hydrator or builder is incompatible with an
22
 * expected process strategy, such as when a non-context-compatible hydrator is
23
 * provided with a non-null context.
24
 */
25
class IncompatibleProcessException extends UnexpectedValueException
26
{
27
28
    /**
29
     * Constants
30
     */
31
32
    /**
33
     * The default exception message.
34
     *
35
     * @var string
36
     */
37
    const DEFAULT_MESSAGE = 'Provided or resolved process is not compatible';
38
39
    /**
40
     * The exception code for when context compatibility is required.
41
     *
42
     * @var int
43
     */
44
    const CODE_FOR_REQUIRED_CONTEXT_COMPATIBILITY = 1;
45
46
    /**
47
     * The exception code for when a hydrator is used as the process.
48
     *
49
     * @var int
50
     */
51
    const CODE_FOR_HYDRATOR = 2;
52
53
    /**
54
     * The exception code for when a builder is used as the process.
55
     *
56
     * @var int
57
     */
58
    const CODE_FOR_BUILDER = 4;
59
60
    /**
61
     * The message extension (appended to the default message) for when context
62
     * compatibility is required.
63
     *
64
     * @var string
65
     */
66
    const MESSAGE_EXTENSION_FOR_REQUIRED_CONTEXT_COMPATIBILITY = ' due to requiring context compatibility';
67
68
69
    /**
70
     * Properties
71
     */
72
73
    /**
74
     * The exception message.
75
     *
76
     * @var string
77
     */
78
    protected $message = self::DEFAULT_MESSAGE;
79
80
81
    /**
82
     * Methods
83
     */
84
85
    /**
86
     * Create an exception instance for when context compatibility is required.
87
     *
88
     * @param object|null $process The incompatible process.
89
     * @param int $code The exception code.
90
     * @param Throwable|null $previous A previous exception used for chaining.
91
     * @return static The newly created exception.
92
     */
93 18
    public static function forRequiredContextCompatibility(
94
        $process = null,
95
        int $code = self::CODE_FOR_REQUIRED_CONTEXT_COMPATIBILITY,
96
        Throwable $previous = null
97
    ): self {
98 18
        if (self::CODE_FOR_REQUIRED_CONTEXT_COMPATIBILITY === $code) {
99 15
            if ($process instanceof Hydrator) {
100 6
                $code += self::CODE_FOR_HYDRATOR;
101 9
            } elseif ($process instanceof Builder) {
102 6
                $code += self::CODE_FOR_BUILDER;
103
            }
104
        }
105
106 18
        $message = self::DEFAULT_MESSAGE . self::MESSAGE_EXTENSION_FOR_REQUIRED_CONTEXT_COMPATIBILITY;
107
108 18
        return new static($message, $code, $previous);
109
    }
110
}
111