Passed
Push — master ( 9eff33...c2decf )
by Anthony
03:23
created

ApiController::testUserToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Controller;
4
5
use PiouPiou\RibsAdminBundle\Entity\Account;
6
use PiouPiou\RibsAdminBundle\Service\Api;
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Session\Session;
11
use Symfony\Component\Routing\Annotation\Route;
12
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
13
14
class ApiController extends AbstractController
15
{
16
    /**
17
     * this method is user to authenticate a user by an api request
18
     * if success it return a token api that expire in 20 minutes
19
     * @Route("/api/users/authenticate", name="ribsadmin_api_login", methods={"POST"})
20
     * @param Request $request
21
     * @param Api $api
22
     * @param EncoderFactoryInterface $encoder
23
     * @return JsonResponse
24
     * @throws \Exception
25
     */
26
	public function login(Request $request, Api $api, EncoderFactoryInterface $encoder): JsonResponse
0 ignored issues
show
Unused Code introduced by
The parameter $encoder is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

26
	public function login(Request $request, Api $api, /** @scrutinizer ignore-unused */ EncoderFactoryInterface $encoder): JsonResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
	{
28
		$em = $this->getDoctrine()->getManager();
29
		
30
		$account = $em->getRepository(Account::class)->findOneBy([
31
			"username" => $request->get("username"),
32
		]);
33
		
34
		if ($account) {
35
			$encoder = $this->get("security.password_encoder");
0 ignored issues
show
Bug introduced by
The method get() does not exist on PiouPiou\RibsAdminBundle\Controller\ApiController. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
			/** @scrutinizer ignore-call */ 
36
   $encoder = $this->get("security.password_encoder");

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
			
37
			if ($encoder->getEncoder($account)->isPasswordValid($account->getPassword(), $request->get("password"), '') === true) {
38
				if ($account->getisActive() == false) {
39
					return new JsonResponse([
40
						"success" => false,
41
						"message" => "You account is disabled"
42
					]);
43
				}
44
				
45
				return new JsonResponse([
46
					"success" => true,
47
					"token" => $api->getToken($account)
48
				]);
49
			}
50
		}
51
		
52
		return new JsonResponse([
53
			"success" => false,
54
			"message" => "bad identifiant and/or password"
55
		]);
56
	}
57
58
    /**
59
     * method that test if user steel logged and send token or new token if it was expired
60
     * @Route("/api/users/test-token", name="ribsadmin_api_test_token", methods={"POST"})
61
     * @param Request $request
62
     * @param Api $api
63
     * @param Session $session
64
     * @return JsonResponse
65
     * @throws \Exception
66
     */
67
    public function testUserToken(Request $request, Api $api, Session $session): JsonResponse
68
    {
69
        $test_logged = $api->userIslogged($request->get("infos"), $request->get("token"));
0 ignored issues
show
Bug introduced by
It seems like $request->get('token') can also be of type null; however, parameter $token of PiouPiou\RibsAdminBundle...ice\Api::userIslogged() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        $test_logged = $api->userIslogged($request->get("infos"), /** @scrutinizer ignore-type */ $request->get("token"));
Loading history...
Bug introduced by
It seems like $request->get('infos') can also be of type null; however, parameter $infos_jwt of PiouPiou\RibsAdminBundle...ice\Api::userIslogged() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        $test_logged = $api->userIslogged(/** @scrutinizer ignore-type */ $request->get("infos"), $request->get("token"));
Loading history...
70
71
        if ($test_logged === false) {
72
            return new JsonResponse([
73
                "success" => $test_logged,
74
                "error_message" => "Votre compte a été archivé, vous ne pouvez plus vous connecter",
75
            ]);
76
        }
77
78
        return new JsonResponse([
79
            "success" => $test_logged,
80
            "token" => $api->getToken($session->get("account")),
81
        ]);
82
    }
83
}
84