Completed
Push — master ( 9628d6...a206dc )
by Derek Stephen
09:38
created

AuthCodeRepository::setAuthorizationCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
eloc 14
nc 1
nop 6
crap 2
1
<?php
2
3
namespace OAuth\Repository;
4
5
use DateTime;
6
use Doctrine\ORM\EntityRepository;
7
use OAuth\AuthCode;
8
9
class AuthCodeRepository extends EntityRepository
10
{
11
    /**
12
     * @param $code
13
     * @return null|array
14
     */
15 View Code Duplication
    public function getAuthorizationCode($code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
    {
17
        $authCode = $this->findOneBy(['code' => $code]);
18
        if ($authCode) {
19
            $authCode = $authCode->toArray();
20
            /** @var DateTime $date */
21
            $date = $authCode['expires'];
22
            $authCode['expires'] = $date->getTimestamp();
23
            return $authCode;
24
        }
25
        return null;
26
    }
27
28
    /**
29
     * @param $code
30
     * @param $clientIdentifier
31
     * @param $userEmail
32
     * @param $redirectUri
33
     * @param $expires
34
     * @param null $scope
35
     */
36
    public function setAuthorizationCode($code, $clientIdentifier, $userEmail, $redirectUri, $expires, $scope = null)
37
    {
38
        $client = $this->_em->getRepository('YourNamespace\Entity\OAuthClient')->findOneBy(array('client_identifier' => $clientIdentifier));
39
        $user = $this->_em->getRepository('YourNamespace\Entity\OAuthUser')->findOneBy(['email' => $userEmail]);
40
        $date = new DateTime();
41
        $date->setTimestamp($expires);
42
        $authCode = AuthCode::fromArray([
43
            'code'           => $code,
44
            'client'         => $client,
45
            'user'           => $user,
46
            'redirect_uri'   => $redirectUri,
47
            'expires'        => $date,
48
            'scope'          => $scope,
49
        ]);
50
        $this->_em->persist($authCode);
51
        $this->_em->flush();
52
    }
53
54
    /**
55
     * @param $code
56
     */
57
    public function expireAuthorizationCode($code)
58
    {
59
        $authCode = $this->findOneBy(['code' => $code]);
60
        $this->_em->remove($authCode);
0 ignored issues
show
Bug introduced by
It seems like $authCode defined by $this->findOneBy(array('code' => $code)) on line 59 can also be of type null; however, Doctrine\ORM\EntityManager::remove() does only seem to accept object, 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...
61
        $this->_em->flush();
62
    }
63
}