Failed Conditions
Branch master (116909)
by Guilherme
08:28
created

ScopeFinderHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getScope() 0 17 5
A __construct() 0 9 3
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 6
    public function __construct($request, FormInterface $form = null)
26
    {
27 6
        $this->form = $form;
28 6
        if ($request instanceof RequestStack) {
29 1
            $this->request = $request->getCurrentRequest();
30 5
        } elseif ($request instanceof Request) {
31 4
            $this->request = $request;
32
        } else {
33 1
            throw new \InvalidArgumentException('$request must be either a Request or a RequestStack');
34
        }
35 5
    }
36
37
    /**
38
     * @return array|null
39
     */
40 5
    public function getScope()
41
    {
42 5
        $scope = $this->request->request->get('scope', false);
43
44 5
        if (!$scope) {
45 3
            $scope = $this->request->query->get('scope', false);
46
        }
47
48 5
        if (!$scope) {
49 2
            if (!$this->form) {
50 1
                return null;
51
            }
52 1
            $form = $this->form->getName();
53 1
            $scope = $this->request->request->get("{$form}[scope]", false, true);
54
        }
55
56 4
        return !is_array($scope) ? explode(' ', $scope) : $scope;
57
    }
58
}
59