GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3ae4c4...a52273 )
by Lukáš
02:32 queued 14s
created

ClientRepository::createQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine\Client;
5
6
use Kdyby\Doctrine\EntityManager;
7
use Kdyby\Doctrine\EntityRepository;
8
use Kdyby\Doctrine\QueryObject;
9
use League\OAuth2\Server\Entities\ClientEntityInterface;
10
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
11
12
class ClientRepository implements ClientRepositoryInterface
13
{
14
	/**
15
	 * @var EntityManager
16
	 */
17
	private $entityManager;
18
19
	/**
20
	 * @var EntityRepository
21
	 */
22
	private $repository;
23
24
	/**
25
	 * @var callable
26
	 */
27
	private $secretValidator;
28
29
	/**
30
	 * @param EntityManager $entityManager
31
	 * @param callable|null $secretValidator
32
	 */
33
	public function __construct(EntityManager $entityManager, callable $secretValidator = null)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
34
	{
35
		$this->entityManager = $entityManager;
36
		$this->repository = $entityManager->getRepository(ClientEntity::class);
37
		$this->secretValidator = $secretValidator ?: function ($expected, $actual) { return hash_equals($expected, $actual); };
38
	}
39
40
	/**
41
	 * @param string $clientIdentifier
42
	 * @param string $grantType
43
	 * @param string|null $clientSecret
44
	 * @param bool $mustValidateSecret
45
	 * @return ClientEntityInterface|null
46
	 */
47
	public function getClientEntity($clientIdentifier, $grantType, $clientSecret = null, $mustValidateSecret = true)
48
	{
49
		/** @var ClientEntity|null $client */
50
		$client = $this->repository->fetchOne($this->createQuery()->byIdentifier($clientIdentifier));
51
		return $client
52
			&& $client->getSecret()
0 ignored issues
show
Bug Best Practice introduced by
The expression $client->getSecret() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53
			&& $mustValidateSecret
54
			&& !call_user_func($this->secretValidator, $client->getSecret(), $clientSecret)
55
			? null
56
			: $client;
57
	}
58
59
	/**
60
	 * @return QueryObject
61
	 */
62
	protected function createQuery(): QueryObject
63
	{
64
		return new ClientQuery();
65
	}
66
}
67