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

ScopeFinderHelper::getScope()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 10
nop 0
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 5
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 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