Hydrator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
c 3
b 1
f 0
lcom 0
cbo 0
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 14 2
A validateRawData() 0 14 3
1
<?php
2
3
namespace GFG\DTOContext\Factory;
4
5
use GFG\DTOContext\Context;
6
7
class Hydrator implements HydratorInterface
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    public function hydrate(
13
        Context\ContextInterface $context,
14
        array $rawData
15
    ) {
16
        if (!isset($rawData['info'])) {
17
            $rawData['info'] = [];
18
        }
19
20
        $this->validateRawData($rawData);
21
22
        $context->setName($rawData['name'])
23
            ->setFullInfo($rawData['info'])
24
            ->setHash($rawData['hash']);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function validateRawData(array $rawData)
31
    {
32
        if (!isset($rawData['info'])) {
33
            $rawData['info'] = [];
34
        }
35
        if (count(array_diff(
36
            ['name', 'data_wrapper', 'info', 'hash', 'data'],
37
            array_keys($rawData)
38
        ))) {
39
            throw new \InvalidArgumentException(
40
                'Invalid data to hydrate context: ' . json_encode($rawData)
41
            );
42
        }
43
    }
44
}
45