Completed
Push — master ( 76cdd3...1c9573 )
by David
14s
created

FactoryHydrator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 5
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 6 2
A canHydrate() 0 3 1
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Hydrators;
5
6
use GraphQL\Type\Definition\InputObjectType;
7
use TheCodingMachine\GraphQL\Controllers\GraphQLException;
8
use TheCodingMachine\GraphQL\Controllers\Hydrators\HydratorInterface;
9
use TheCodingMachine\GraphQL\Controllers\Types\ResolvableInputObjectType;
10
11
/**
12
 * Hydrates input types based on the Factory annotation.
13
 */
14
class FactoryHydrator implements HydratorInterface
15
{
16
17
    /**
18
     * Hydrates/returns an object based on a PHP array and a GraphQL type.
19
     *
20
     * @param mixed[] $data
21
     * @param InputObjectType $type
22
     * @return object
23
     * @throws CannotHydrateException
24
     */
25
    public function hydrate(array $data, InputObjectType $type)
26
    {
27
        if ($type instanceof ResolvableInputObjectType) {
28
            return $type->resolve($data);
29
        }
30
        throw CannotHydrateException::createForInputType($type->name);
31
    }
32
33
    /**
34
     * Whether this hydrate can hydrate the passed data.
35
     *
36
     * @param mixed[] $data
37
     * @param InputObjectType $type
38
     * @return bool
39
     */
40
    public function canHydrate(array $data, InputObjectType $type): bool
41
    {
42
        return $type instanceof ResolvableInputObjectType;
43
    }
44
}
45