Completed
Push — master ( 5d17f3...e891dc )
by Alexandr
03:56
created

FieldsAwareConfigTrait::addFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
rs 9.4285
ccs 8
cts 8
cp 1
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 12/1/15 11:05 PM
7
*/
8
9
namespace Youshido\GraphQL\Config\Traits;
10
11
12
use Youshido\GraphQL\Field\AbstractField;
13
use Youshido\GraphQL\Field\Field;
14
use Youshido\GraphQL\Field\FieldInterface;
15
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
16
17
/**
18
 * Class FieldsAwareTrait
19
 * @package Youshido\GraphQL\Config\Traits
20
 */
21
trait FieldsAwareConfigTrait
22
{
23
    protected $fields = [];
24
25 71
    public function buildFields()
26
    {
27 71
        if (!empty($this->data['fields'])) {
28 41
            $this->addFields($this->data['fields']);
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29 41
        }
30 71
    }
31
32
    /**
33
     * Add fields from passed interface
34
     * @param AbstractInterfaceType $interfaceType
35
     * @return $this
36
     */
37 1
    public function applyInterface(AbstractInterfaceType $interfaceType)
38
    {
39 1
        $this->addFields($interfaceType->getFields());
40
41 1
        return $this;
42
    }
43
44
    /**
45
     * @param array $fieldsList
46
     * @return $this
47
     */
48 43
    public function addFields($fieldsList)
49
    {
50 43
        foreach ($fieldsList as $fieldName => $fieldConfig) {
51
52 43
            if ($fieldConfig instanceof FieldInterface) {
53 3
                $this->fields[$fieldConfig->getName()] = $fieldConfig;
54 3
                continue;
55
            } else {
56 42
                $this->addField($fieldName, $this->buildFieldConfig($fieldName, $fieldConfig));
57
            }
58 43
        }
59
60 43
        return $this;
61
    }
62
63
    /**
64
     * @param AbstractField|string $field     Field name or Field Object
65
     * @param mixed                $fieldInfo Field Type or Field Config array
66
     * @return $this
67
     */
68 51
    public function addField($field, $fieldInfo = null)
69
    {
70 51
        if (!($field instanceof FieldInterface)) {
71 51
            $field = new Field($this->buildFieldConfig($field, $fieldInfo));
72 51
        }
73
74 51
        $this->fields[$field->getName()] = $field;
75
76 51
        return $this;
77
    }
78
79 51 View Code Duplication
    protected function buildFieldConfig($name, $info = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81 51
        if (!is_array($info)) {
82
            $info = [
83 40
                'type' => $info,
84 40
                'name' => $name,
85 40
            ];
86 51
        } elseif (empty($info['name'])) {
87 32
            $info['name'] = $name;
88 32
        }
89
90 51
        return $info;
91
    }
92
93
    /**
94
     * @param $name
95
     *
96
     * @return Field
97
     */
98 28
    public function getField($name)
99
    {
100 28
        return $this->hasField($name) ? $this->fields[$name] : null;
101
    }
102
103
    /**
104
     * @param $name
105
     *
106
     * @return bool
107
     */
108 33
    public function hasField($name)
109
    {
110 33
        return array_key_exists($name, $this->fields);
111
    }
112
113 33
    public function hasFields()
114
    {
115 33
        return !empty($this->fields);
116
    }
117
118
    /**
119
     * @return Field[]
120
     */
121 33
    public function getFields()
122
    {
123 33
        return $this->fields;
124
    }
125
126 1
    public function removeField($name)
127
    {
128 1
        if ($this->hasField($name)) {
129 1
            unset($this->fields[$name]);
130 1
        }
131
132 1
        return $this;
133
    }
134
}
135