HydrationFailed::encountered()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydrator;
5
6
use function get_class;
7
use RuntimeException;
8
use function sprintf;
9
use Throwable;
10
11
/**
12
 * Notifies the client code that the hydration of an object failed.
13
 *
14
 * @author  Stratadox
15
 */
16
final class HydrationFailed extends RuntimeException implements HydrationFailure
17
{
18
    /**
19
     * Notifies the client code that an exception was encountered during the
20
     * hydration process.
21
     *
22
     * @param Throwable $exception The exception that was thrown.
23
     * @param object    $target    The object that was being hydrated.
24
     * @return HydrationFailure    The exception to throw.
25
     */
26
    public static function encountered(
27
        Throwable $exception,
28
        object $target
29
    ): HydrationFailure {
30
        return new self(sprintf(
31
            'Could not hydrate the `%s`: %s',
32
            get_class($target),
33
            $exception->getMessage()
34
        ), 0, $exception);
35
    }
36
}
37