Completed
Push — bugfix/EVO-7301 ( 7a779c...4a7e29 )
by
unknown
67:25 queued 06:04
created

SelectExclusionStrategy::setRequestStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
13
/**
14
 * In this Strategy we skip all properties on first level who are not selected if there is a select in rql.
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class SelectExclusionStrategy implements ExclusionStrategyInterface
21
{
22
    /**
23
     * @var RequestStack $requestStack
24
     */
25
    protected $requestStack;
26
27
    /**
28
     * for injection of the global request_stack to access the XiagQuery-Object
29
     * @param RequestStack $requestStack the global request_stack
30
     * @return void
31
     */
32
    public function setRequestStack(RequestStack $requestStack)
33
    {
34
        $this->requestStack = $requestStack;
35
    }
36
37
    /**
38
     * @InheritDoc: Whether the class should be skipped.
39
     * @param ClassMetadata $metadata         the ClassMetadata for the Class of the property to be serialized
40
     * @param Context       $navigatorContext the context for serialization
41
     * @return boolean
42
     */
43
    public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext)
44
    {
45
        return false;
46
    }
47
48
    /**
49
     * @InheritDoc: Whether the property should be skipped.
50
     * Skipping properties on first level who are not selected if there is a select in rql.
51
     * @param PropertyMetadata $property the property to be serialized
52
     * @param Context          $context  the context for serialization
53
     * @return boolean
54
     */
55
    public function shouldSkipProperty(PropertyMetadata $property, Context $context)
56
    {
57
        // we are only dealing with the first level of the JSON here
58
        if ($context->getDepth() > 1) {
59
            return false;
60
        }
61
62
        $return = false;
63
        $xiagQuery = $this->requestStack->getCurrentRequest()->get('rqlQuery');
64
        if ($xiagQuery) {
65
            $select = $xiagQuery->getSelect();
66
            if ($select) {
67
                $return = ! in_array($property->name, $select->getFields());
68
            }
69
        }
70
        return $return;
71
    }
72
}
73