Completed
Push — master ( e5fb04...e1ec82 )
by Paweł
30:31 queued 18:38
created

TokenController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ApiBundle\Controller;
13
14
use FOS\RestBundle\Decoder\DecoderProviderInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\ParameterBag;
17
use OAuth2\OAuth2;
18
use OAuth2\OAuth2ServerException;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
21
22
/**
23
 * @see \FOS\OAuthServerBundle\Controller\TokenController
24
 * @see \FOS\RestBundle\EventListener\BodyListener
25
 *
26
 * @author Łukasz Chruściel <[email protected]>
27
 */
28
final class TokenController
29
{
30
    /**
31
     * @var OAuth2
32
     */
33
    private $server;
34
35
    /**
36
     * @var DecoderProviderInterface
37
     */
38
    private $decoderProvider;
39
40
    /**
41
     * @param OAuth2 $server
42
     * @param DecoderProviderInterface $decoderProvider
43
     */
44
    public function __construct(OAuth2 $server, DecoderProviderInterface $decoderProvider)
45
    {
46
        $this->server = $server;
47
        $this->decoderProvider = $decoderProvider;
48
    }
49
50
    /**
51
     * @param Request $request
52
     *
53
     * @return Response
54
     */
55
    public function tokenAction(Request $request)
56
    {
57
        $this->convertContentToRequestParameters($request);
58
59
        try {
60
            return $this->server->grantAccessToken($request);
61
        } catch (OAuth2ServerException $e) {
62
            return $e->getHttpResponse();
63
        }
64
    }
65
66
    /**
67
     * Additional action to keep default behaviour of FosRestBundle. Main aim is to turn off data normalization.
68
     *
69
     * @param Request $request
70
     */
71
    private function convertContentToRequestParameters(Request $request)
72
    {
73
        $format = $request->getFormat($request->headers->get('Content-Type', '')) ?: $request->getRequestFormat();
74
        $content = $request->getContent();
75
76
        if (empty($content)) {
77
            return;
78
        }
79
80
        if (!$this->decoderProvider->supports($format)) {
81
            return;
82
        }
83
84
        $decoder = $this->decoderProvider->getDecoder($format);
85
        $data = $decoder->decode($content);
0 ignored issues
show
Bug introduced by
It seems like $content defined by $request->getContent() on line 74 can also be of type resource; however, FOS\RestBundle\Decoder\DecoderInterface::decode() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
86
87
        if (!is_array($data)) {
88
            throw new BadRequestHttpException(sprintf('Invalid %s message received', $format));
89
        }
90
91
        $request->request = new ParameterBag($data);
92
    }
93
}
94