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

AccessTokenRepository::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\AccessToken;
5
6
use Kdyby\Doctrine\EntityManager;
7
use Kdyby\Doctrine\EntityRepository;
8
use Kdyby\Doctrine\QueryObject;
9
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
10
use League\OAuth2\Server\Entities\ClientEntityInterface;
11
use League\OAuth2\Server\Entities\ScopeEntityInterface;
12
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
13
14
class AccessTokenRepository implements AccessTokenRepositoryInterface
15
{
16
	/**
17
	 * @var EntityManager
18
	 */
19
	private $entityManager;
20
21
	/**
22
	 * @var EntityRepository
23
	 */
24
	private $repository;
25
26
	public function __construct(EntityManager $entityManager)
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...
27
	{
28
		$this->entityManager = $entityManager;
29
		$this->repository = $entityManager->getRepository(AccessTokenEntity::class);
30
	}
31
32
	/**
33
	 * @param ClientEntityInterface $clientEntity
34
	 * @param ScopeEntityInterface[] $scopes
35
	 * @param string|null $userIdentifier
36
	 * @return AccessTokenEntity
37
	 */
38
	public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
39
	{
40
		$accessToken = new AccessTokenEntity();
41
		$accessToken->setClient($clientEntity);
42
		foreach ($scopes as $scope) {
43
			$accessToken->addScope($scope);
44
		}
45
		$accessToken->setUserIdentifier($userIdentifier);
46
		return $accessToken;
47
	}
48
49
	/**
50
	 * @param AccessTokenEntityInterface $accessTokenEntity
51
	 */
52
	public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
53
	{
54
		$this->entityManager->persist($accessTokenEntity);
55
		$this->entityManager->flush();
56
	}
57
58
	/**
59
	 * @param string $tokenId
60
	 */
61
	public function revokeAccessToken($tokenId)
62
	{
63
		$this->repository->fetchOne($this->createQuery()->byIdentifier($tokenId))->setRevoked(true);
64
		$this->entityManager->flush();
65
	}
66
67
	/**
68
	 * @param string $tokenId
69
	 * @return bool
70
	 */
71
	public function isAccessTokenRevoked($tokenId)
72
	{
73
		return $this->repository->fetchOne($this->createQuery()->byIdentifier($tokenId))->isRevoked();
74
	}
75
76
	/**
77
	 * @return QueryObject
78
	 */
79
	protected function createQuery(): QueryObject
80
	{
81
		return new AccessTokenQuery();
82
	}
83
}
84