Completed
Push — master ( 4b81f1...ff5bd6 )
by Radoje
01:35 queued 10s
created

Hydrator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Happyr\MessageSerializer\Hydrator;
6
7
use Happyr\MessageSerializer\Hydrator\Exception\ConvertToMessageFailedException;
8
use Happyr\MessageSerializer\Hydrator\Exception\HydratorNotFoundException;
9
use Happyr\MessageSerializer\Hydrator\Exception\VersionNotSupportedException;
10
11
final class Hydrator implements ArrayToMessageInterface
12
{
13
    /**
14
     * @var HydratorInterface[]
15
     */
16
    private $hydrators;
17
18 1
    public function __construct(iterable $hydrators)
19
    {
20 1
        $this->hydrators = $hydrators;
21 1
    }
22
23
    /**
24
     * @throws ConvertToMessageFailedException
25
     * @throws VersionNotSupportedException
26
     * @throws HydratorNotFoundException
27
     */
28 1
    public function toMessage(array $data)
29
    {
30
        // Default exception to be thrown.
31 1
        $exception = new HydratorNotFoundException();
32
33 1
        foreach ($this->hydrators as $hydrator) {
34
            try {
35 1
                $isSupported = $hydrator->supportsHydrate($data['identifier'] ?? '', $data['version'] ?? 0);
36
            } catch (VersionNotSupportedException $e) {
37
                $exception = $e;
38
                continue;
39
            }
40
41 1
            if (!$isSupported) {
42
                continue;
43
            }
44
45
            try {
46 1
                return $hydrator->toMessage($data['payload'] ?? [], $data['version'] ?? 0);
47
            } catch (\Throwable $throwable) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
48
                throw new ConvertToMessageFailedException(sprintf('Transformer "%s" failed to transform a message.', get_class($hydrator)), 0, $throwable);
49
            }
50
        }
51
52
        throw $exception;
53
    }
54
}
55