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

ScopeFinderHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
B getScope() 0 18 5
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