TokenApiController::postAction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Majora\Bundle\OAuthServerBundle\Controller;
4
5
use Majora\Bundle\FrameworkExtraBundle\Controller\RestApiControllerTrait;
6
use Majora\Component\OAuth\Exception\InvalidGrantException;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
11
/**
12
 * Controller for oAuth tokens web cases.
13
 */
14
class TokenApiController extends Controller
15
{
16
    use RestApiControllerTrait;
17
18
    /**
19
     * Create a new access_token from given request throught oAuth server.
20
     *
21
     * @param Request $request
22
     *
23
     * @return Response
24
     */
25
    public function postAction(Request $request)
26
    {
27
        try {
28
            return $this->createJsonResponse($this->get('oauth.server')->grant(
29
                $this->getRequestData($request, 'snakelize')
30
            ));
31
        } catch (InvalidGrantException $e) {
32
            return new JsonResponse('Unavailable to create access token : bad credentials.', 401);
33
        } catch (\InvalidArgumentException $e) {
34
            return new JsonResponse('Invalid or missing fields for grant type.', 400);
35
        }
36
    }
37
}
38