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

Hydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 56.25%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 44
c 0
b 0
f 0
ccs 9
cts 16
cp 0.5625
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toMessage() 0 26 5
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