Completed
Branch EDTR/master (f9130a)
by
unknown
46:59 queued 38:09
created

InputsManager::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\graphql;
4
5
use EventEspresso\core\services\collections\CollectionDetailsException;
6
use EventEspresso\core\services\collections\CollectionLoaderException;
7
use EventEspresso\core\services\graphql\inputs\InputCollection;
8
use EventEspresso\core\services\graphql\inputs\InputInterface;
9
10
/**
11
 * Class InputsManager
12
 * Loads and registers custom GraphQL Inputs and Fields
13
 *
14
 * @package EventEspresso\core\services\graphql
15
 * @author  Manzoor Wani
16
 * @since   $VID:$
17
 */
18
class InputsManager
19
{
20
21
    /**
22
     * @var InputCollection|InputInterface[] $inputs
23
     */
24
    private $inputs;
25
26
27
    /**
28
     * InputsManager constructor.
29
     *
30
     * @param InputCollection|InputInterface[] $inputs
31
     */
32
    public function __construct(InputCollection $inputs)
33
    {
34
        $this->inputs = $inputs;
35
    }
36
37
38
    /**
39
     * @throws CollectionDetailsException
40
     * @throws CollectionLoaderException
41
     * @since $VID:$
42
     */
43
    public function init()
44
    {
45
        $this->inputs->loadInputs();
46
        add_action('graphql_register_types', [$this, 'configureInputs'], 9);
47
    }
48
49
50
    /**
51
     * @since $VID:$
52
     */
53
    public function configureInputs()
54
    {
55
        // loop through the collection of inputs and register their fields
56
        foreach ($this->inputs as $input) {
57
            $this->registerInput($input);
58
        }
59
    }
60
61
62
    /**
63
     * @param InputInterface $input
64
     * @since $VID:$
65
     */
66
    public function registerInput(InputInterface $input)
67
    {
68
        $inputFields = [];
69
        foreach ($input->fields() as $field) {
70
            $fieldName = $field->name();
71
            $inputFields[ $fieldName ] = $field->toArray();
72
        }
73
        // Register the input type.
74
        register_graphql_input_type(
75
            $input->name(),
76
            [
77
                'description' => $input->description(),
78
                'fields'      => $inputFields,
79
            ]
80
        );
81
    }
82
}
83