Completed
Push — issue#666 ( 8f1392...6be2d0 )
by Guilherme
03:46
created

RemoteClaimController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B validateRemoteClaimAction() 0 41 2
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\RemoteClaimsBundle\Controller;
12
13
use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
14
use FOS\RestBundle\Controller\Annotations as REST;
15
use JMS\Serializer\SerializationContext;
16
use JMS\Serializer\SerializerInterface;
17
use LoginCidadao\APIBundle\Controller\BaseController;
18
use LoginCidadao\CoreBundle\Entity\Authorization;
19
use LoginCidadao\CoreBundle\Entity\AuthorizationRepository;
20
use LoginCidadao\CoreBundle\LongPolling\LongPollingUtils;
21
use LoginCidadao\OAuthBundle\Model\AccessTokenManager;
22
use LoginCidadao\OAuthBundle\Model\ClientInterface;
23
use LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface;
24
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface;
25
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface;
26
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
30
use LoginCidadao\CoreBundle\Model\PersonInterface;
31
use LoginCidadao\OAuthBundle\Model\ClientUser;
32
use LoginCidadao\APIBundle\Security\Audit\Annotation as Audit;
33
use LoginCidadao\APIBundle\Entity\LogoutKey;
34
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
35
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
36
37
class RemoteClaimController extends BaseController
38
{
39
40
    /**
41
     * @REST\Get("/api/v1/validate-claim", name="remote_claims_validate", defaults={"_format"="json"})
42
     * @REST\View(templateVar="oidc_config")
43
     */
44
    public function validateRemoteClaimAction(Request $request)
45
    {
46
        /** @var ClaimProviderInterface|ClientInterface $provider */
47
        $provider = $this->getClient();
48
49
        $accessToken = $request->get('claim_access_token');
50
51
        /** @var RemoteClaimManagerInterface $manager */
52
        $manager = $this->get('lc.remote_claims.manager');
53
54
        $remoteClaimAuthorization = $manager->getRemoteClaimAuthorizationByAccessToken($provider, $accessToken);
0 ignored issues
show
Bug introduced by
It seems like $provider defined by $this->getClient() on line 47 can also be of type object<LoginCidadao\OAut...\Model\ClientInterface>; however, LoginCidadao\RemoteClaim...rizationByAccessToken() does only seem to accept object<LoginCidadao\Remo...ClaimProviderInterface>, 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...
55
        if (!$remoteClaimAuthorization instanceof RemoteClaimAuthorizationInterface) {
56
            throw $this->createNotFoundException("Authorization not found");
57
        }
58
        $person = $remoteClaimAuthorization->getPerson();
59
        $client = $remoteClaimAuthorization->getClient();
60
61
        /** @var AuthorizationRepository $authorizationRepo */
62
        $authorizationRepo = $this->getDoctrine()->getRepository('LoginCidadaoCoreBundle:Authorization');
63
64
        /** @var Authorization $authorization */
65
        $authorization = $authorizationRepo->findOneBy([
66
            'client' => $provider,
67
            'person' => $person,
68
        ]);
69
70
        /** @var SerializerInterface $serializer */
71
        $serializer = $this->get('jms_serializer');
72
        $serializedPerson = $serializer->serialize($person, $this->getSerializationContext($authorization->getScope()));
0 ignored issues
show
Documentation introduced by
$this->getSerializationC...horization->getScope()) is of type object<JMS\Serializer\SerializationContext>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
        $serializedClient = $serializer->serialize($client, $this->getSerializationContext(['remote_claim']));
0 ignored issues
show
Documentation introduced by
$this->getSerializationC...(array('remote_claim')) is of type object<JMS\Serializer\SerializationContext>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
75
        $response = [
76
            'claim_name' => (string)$remoteClaimAuthorization->getClaimName(),
77
            'userinfo' => $serializedPerson,
78
            'relying_party' => $serializedClient,
79
        ];
80
81
        $view = $this->view($response);
82
83
        return $this->handleView($view);
84
    }
85
}
86