Completed
Push — bugfix/EVO-7301 ( cec151 )
by
unknown
19:24 queued 13:48
created

SelectExclusionStrategy::shouldSkipClass()   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 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
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
12
/**
13
 * In this Strategy we skip all properties on first level who are not selected if there is a select in rql.
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class SelectExclusionStrategy implements ExclusionStrategyInterface
20
{
21
    /**
22
     * @InheritDoc: Whether the class should be skipped.
23
     * @param ClassMetadata $metadata         blabla
24
     * @param Context       $navigatorContext blabla
25
     * @return boolean
26
     */
27
    public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext)
28
    {
29
        return false;
30
    }
31
32
    /**
33
     * @InheritDoc: Whether the property should be skipped.
34
     * Skipping properties on first level who are not selected if there is a select in rql.
35
     * @param PropertyMetadata $property blabla
36
     * @param Context          $context  blabla
37
     * @return boolean
38
     */
39
    public function shouldSkipProperty(PropertyMetadata $property, Context $context)
0 ignored issues
show
Coding Style introduced by
shouldSkipProperty uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
40
    {
41
        // we are only dealing with the first level of the JSON here
42
        if ($context->getDepth() > 1) {
43
            return false;
44
        }
45
46
        $select = false;
47
        $selectVars=[];
48
        foreach ($_GET as $getVar => $val) {
49
            if (strstr($getVar, 'select(')) {
50
                preg_match('/\((.*?)\)/', $getVar, $match);
51
                $select = true;
52
                $selectVars = explode(',', $match[1]);
53
                break;
54
            }
55
        }
56
        if ($select && ! in_array($property->name, $selectVars)) {
57
            return true;
58
        } else {
59
            return false;
60
        }
61
    }
62
}
63