Completed
Push — bugfix/EVO-7301 ( 38d957...e01ef0 )
by
unknown
11:44
created

getSelectedFieldsFromRQL()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 37.1271

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
ccs 6
cts 26
cp 0.2308
rs 5.3846
cc 8
eloc 19
nc 9
nop 1
crap 37.1271
1
<?php
2
/**
3
 * Class for exclusion strategies.
4
 */
5
namespace Graviton\RestBundle\ExclusionStrategy;
6
7
use JMS\Serializer\Exclusion\ExclusionStrategyInterface;
8
use JMS\Serializer\Metadata\ClassMetadata;
9
use JMS\Serializer\Metadata\PropertyMetadata;
10
use JMS\Serializer\Context;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
use Xiag\Rql\Parser\Query;
13
14
/**
15
 * In this Strategy we skip all properties on first level who are not selected if there is a select in rql.
16
 *
17
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
18
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
19
 * @link     http://swisscom.ch
20
 */
21
class SelectExclusionStrategy implements ExclusionStrategyInterface
22
{
23
    /**
24
     * @var Array $selectedFields
25
     */
26
    protected $selectedFields;
27
28
    /**
29
     * @var Boolean $isSelect
30
     */
31
    protected $isSelect;
32
33
    /**
34
     * Injection of the global request_stack to access the selected Fields via Query-Object
35
     * @param RequestStack $requestStack the global request_stack
36
     * @return void
37
     */
38 4
    public function getSelectedFieldsFromRQL(RequestStack $requestStack)
39
    {
40 4
        $currentRequest = $requestStack->getCurrentRequest();
41 4
        $this->selectedFields = [];
42 4
        $this->isSelect = false;
43 4
        if ($currentRequest) {
44
            $rqlQuery = $currentRequest->get('rqlQuery');
45
            if ($rqlQuery && $rqlQuery instanceof Query) {
46
                $select = $rqlQuery->getSelect();
47
                if ($select) {
48
                    $this->isSelect = true;
49
                    $this->selectedFields = $select->getFields();
50
                    // get the nested fields as well
51
                    $nestedFields = [];
52
                    foreach ($this->selectedFields as $key => $field) {
53
                        if (strstr($field, '.')) {
54
                            $nestedFields=array_merge($nestedFields, explode('.', $field));
55
                            unset($this->selectedFields[$key]);
56
                        }
57
                    }
58
                    $this->selectedFields = array_merge($this->selectedFields, $nestedFields);
59
                    // id is always included in response (bug/feature)?
60
                    if (! in_array('id', $this->selectedFields)) {
61
                        $this->selectedFields[] = 'id';
62
                    };
63
                }
64
            }
65
        }
66 4
    }
67
68
    /**
69
     * @InheritDoc: Whether the class should be skipped.
70
     * @param ClassMetadata $metadata         the ClassMetadata for the Class of the property to be serialized
71
     * @param Context       $navigatorContext the context for serialization
72
     * @return boolean
73
     */
74
    public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext)
75
    {
76
        return false;
77
    }
78
79
    /**
80
     * @InheritDoc: Whether the property should be skipped.
81
     * Skipping properties on first level who are not selected if there is a select in rql.
82
     * @param PropertyMetadata $property the property to be serialized
83
     * @param Context          $context  the context for serialization
84
     * @return boolean
85
     */
86
    public function shouldSkipProperty(PropertyMetadata $property, Context $context)
87
    {
88
        // we are only dealing with the first level of the JSON here
89
        if ($context->getDepth() > 1) {
90
            return false;
91
        }
92
        // nothing selected, default serialization
93
        if (! $this->isSelect) {
94
            return false;
95
        }
96
        return ! in_array($property->name, $this->selectedFields);
97
    }
98
}
99