Completed
Push — master ( c3efa2...a812ec )
by Guilherme
13s
created

ScopeFinderHelper::getScope()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 10
nop 0
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OAuthBundle\Helper;
12
13
use Symfony\Component\Form\FormInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
17
class ScopeFinderHelper
18
{
19
    /** @var Request */
20
    private $request;
21
22
    /** @var FormInterface */
23
    private $form;
24
25
    public function __construct($request, FormInterface $form = null)
26
    {
27
        $this->form = $form;
28
        if ($request instanceof RequestStack) {
29
            $this->request = $request->getCurrentRequest();
30
        } elseif ($request instanceof Request) {
31
            $this->request = $request;
32
        } else {
33
            throw new \InvalidArgumentException('$request must be either a Request or a RequestStack');
34
        }
35
    }
36
37
    /**
38
     * @return array|null
39
     */
40
    public function getScope()
41
    {
42
        $scope = $this->request->request->get('scope', false);
43
44
        if (!$scope) {
45
            $scope = $this->request->query->get('scope', false);
46
        }
47
48
        if (!$scope) {
49
            if (!$this->form) {
50
                return null;
51
            }
52
            $form = $this->form->getName();
53
            $scope = $this->request->request->get("{$form}[scope]", false, true);
54
        }
55
56
        return !is_array($scope) ? explode(' ', $scope) : $scope;
57
    }
58
}
59