Passed
Push — master ( 6895b1...424f48 )
by Jesse
08:53
created

HydrationFailed::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 8
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
    private $hydrationData;
19
20
    private function __construct(
21
        string $message,
22
        int $code,
23
        Throwable $previous,
24
        array $data
25
    ) {
26
        parent::__construct($message, $code, $previous);
27
        $this->hydrationData = $data;
28
    }
29
30
    /**
31
     * Notifies the client code that an exception was encountered during the
32
     * hydration process.
33
     *
34
     * @param Throwable $exception The exception that was thrown.
35
     * @param object    $target    The object that was being hydrated.
36
     * @return HydrationFailure    The exception to throw.
37
     */
38
    public static function encountered(
39
        Throwable $exception,
40
        object $target,
41
        array $data
42
    ): HydrationFailure {
43
        return new self(sprintf(
44
            'Could not hydrate the `%s`: %s',
45
            get_class($target),
46
            $exception->getMessage()
47
        ), 0, $exception, $data);
48
    }
49
50
    public function hydrationData(): array
51
    {
52
        return $this->hydrationData;
53
    }
54
}
55