Completed
Branch EDTR/master (5c103b)
by
unknown
10:13 queued 38s
created

GraphQLData::setParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\entities\admin\GraphQLData;
4
5
use Exception;
6
7
/**
8
 * Class GraphQLData
9
 * Description
10
 *
11
 * @package EventEspresso\core\domain\entities\admin\GraphQLData
12
 * @author  Manzoor Wani
13
 * @since   $VID:$
14
 */
15
abstract class GraphQLData implements GraphQLDataInterface
16
{
17
18
    /**
19
     * @var string $namespace The graphql namespace/prefix.
20
     */
21
    protected $namespace = 'Espresso';
22
23
    /**
24
     * @var array $params
25
     */
26
    private $params = [];
27
28
29
    /**
30
     * @param array $params
31
     */
32
    public function setParams(array $params)
33
    {
34
        $this->params = $params;
35
    }
36
37
38
39
    /**
40
     * @param string $field_key
41
     * @param array  $where_params
42
     * @return mixed|null
43
     * @since $VID:$
44
     */
45
    protected function getQueryResponse($field_key, array $where_params = [])
46
    {
47
        if (! empty($where_params)) {
48
            if (! array_key_exists('variables', $this->params)) {
49
                $this->params['variables'] = [];
50
            }
51
            $this->params['variables']['where'] = $where_params;
52
        }
53
54
        $responseData = $this->makeGraphQLRequest($this->params);
55
        return ! empty($responseData[ $field_key ]) ? $responseData[ $field_key ] : null;
56
    }
57
58
59
    /**
60
     * @param array $data
61
     * @return array
62
     * @since $VID:$
63
     */
64
    protected function makeGraphQLRequest($data)
65
    {
66
        try {
67
            $response = graphql($data);
68
            if (! empty($response['data'])) {
69
                return $response['data'];
70
            }
71
            return null;
72
        } catch (Exception $e) {
73
            // do something with the errors thrown
74
            return null;
75
        }
76
    }
77
}
78