Completed
Push — bugfix/EVO-7301 ( 4353d6...09b50f )
by
unknown
236:09 queued 174:52
created

SelectExclusionStrategy::shouldSkipProperty()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 0
cts 3
cp 0
rs 8.8571
cc 5
eloc 10
nc 4
nop 2
crap 30
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 RequestStack $requestStack
25
     */
26
    protected $requestStack;
27
28
    /**
29
     * for injection of the global request_stack to access the XiagQuery-Object
30
     * @param RequestStack $requestStack the global request_stack
31
     * @return void
32
     */
33
    public function setRequestStack(RequestStack $requestStack)
34
    {
35
        $this->requestStack = $requestStack;
36
    }
37
38
    /**
39
     * @InheritDoc: Whether the class should be skipped.
40
     * @param ClassMetadata $metadata         the ClassMetadata for the Class of the property to be serialized
41
     * @param Context       $navigatorContext the context for serialization
42
     * @return boolean
43
     */
44
    public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext)
45
    {
46
        return false;
47
    }
48
49
    /**
50
     * @InheritDoc: Whether the property should be skipped.
51
     * Skipping properties on first level who are not selected if there is a select in rql.
52
     * @param PropertyMetadata $property the property to be serialized
53
     * @param Context          $context  the context for serialization
54
     * @return boolean
55
     */
56
    public function shouldSkipProperty(PropertyMetadata $property, Context $context)
57
    {
58
        // we are only dealing with the first level of the JSON here
59
        if ($context->getDepth() > 1) {
60
            return false;
61
        }
62
63
        $return = false;
64
        $xiagQuery = $this->requestStack->getCurrentRequest()->get('rqlQuery');
65
        if ($xiagQuery && $xiagQuery instanceof Query) {
66
            $select = $xiagQuery->getSelect();
67
            if ($select) {
68
                $return = ! in_array($property->name, $select->getFields());
69
            }
70
        }
71
        return $return;
72
    }
73
}
74