Completed
Pull Request — develop (#640)
by Narcotic
08:00 queued 01:02
created

ContextFactoryAbstract::setGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * abstract for context factories
4
 */
5
6
namespace Graviton\RestBundle\Serializer;
7
8
use JMS\Serializer\Context;
9
use JMS\Serializer\Exclusion\ExclusionStrategyInterface;
10
use JMS\Serializer\Exclusion\GroupsExclusionStrategy;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\RequestStack;
13
14
/**
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  https://opensource.org/licenses/MIT MIT License
17
 * @link     http://swisscom.ch
18
 */
19
abstract class ContextFactoryAbstract
20
{
21
22
    /**
23
     * @var RequestStack
24
     */
25
    private $requestStack = null;
26
27
    /**
28
     * @var bool
29
     */
30
    private $setSerializeNull = true;
31
32
    /**
33
     * @var null|array
34
     */
35
    private $groups = null;
36
37
    /**
38
     * @var string
39
     */
40
    private $overrideHeaderName = null;
41
42
    /**
43
     * @var bool
44
     */
45
    private $overrideHeaderAllowed = false;
46
47
    /**
48
     * @var array
49
     */
50
    private $exclusionStrategies = [];
51
52
    /**
53
     * set RequestStack
54
     *
55
     * @param RequestStack $requestStack requestStack
56
     *
57
     * @return void
58
     */
59 2
    public function setRequestStack($requestStack)
60
    {
61 2
        $this->requestStack = $requestStack;
62 2
    }
63
64
    /**
65
     * set SetSerializeNull
66
     *
67
     * @param bool $setSerializeNull setSerializeNull
68
     *
69
     * @return void
70
     */
71 2
    public function setSetSerializeNull($setSerializeNull)
72
    {
73 2
        $this->setSerializeNull = $setSerializeNull;
74 2
    }
75
76
    /**
77
     * set Groups
78
     *
79
     * @param array $groups groups
80
     *
81
     * @return void
82
     */
83 2
    public function setGroups($groups)
84
    {
85 2
        $this->groups = $groups;
86 2
    }
87
88
    /**
89
     * set OverrideHeaderName
90
     *
91
     * @param null $overrideHeaderName overrideHeaderName
92
     *
93
     * @return void
94
     */
95 2
    public function setOverrideHeaderName($overrideHeaderName)
96
    {
97 2
        $this->overrideHeaderName = $overrideHeaderName;
98 2
    }
99
100
    /**
101
     * set OverrideHeaderAllowed
102
     *
103
     * @param bool $overrideHeaderAllowed overrideHeaderAllowed
104
     *
105
     * @return void
106
     */
107 2
    public function setOverrideHeaderAllowed($overrideHeaderAllowed)
108
    {
109 2
        $this->overrideHeaderAllowed = $overrideHeaderAllowed;
110 2
    }
111
112
    /**
113
     * set ExclusionStrategies
114
     *
115
     * @param ExclusionStrategyInterface $exclusionStrategy exclusionStrategy
116
     *
117
     * @return void
118
     */
119
    public function addExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy)
120
    {
121
        $this->exclusionStrategies[] = $exclusionStrategy;
122
    }
123
124
    /**
125
     * sets the necessary properties on the context
126
     *
127
     * @param Context $context context
128
     *
129
     * @return Context context
130
     */
131
    protected function workOnInstance(Context $context)
132
    {
133
        $context = $context->setSerializeNull($this->setSerializeNull);
134
135
        // group override?
136
        if (true === $this->overrideHeaderAllowed &&
137
            $this->requestStack instanceof RequestStack &&
138
            $this->requestStack->getCurrentRequest() instanceof Request
139
        ) {
140
            $headerValue = $this->requestStack->getCurrentRequest()->headers->get($this->overrideHeaderName);
141
            if (!is_null($headerValue)) {
142
                $this->groups = array_map('trim', explode(',', $headerValue));
143
            }
144
        }
145
146
        $serializerGroups = [GroupsExclusionStrategy::DEFAULT_GROUP];
147
        if (is_array($this->groups) && !empty($this->groups)) {
148
            $serializerGroups = array_merge($serializerGroups, $this->groups);
149
        }
150
151
        foreach ($this->exclusionStrategies as $strategy) {
152
            $context->addExclusionStrategy($strategy);
153
        }
154
155
        return $context->setGroups($serializerGroups);
156
    }
157
}
158