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

AuthCodeRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 21.82 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 12
loc 55
rs 10
c 1
b 0
f 0
ccs 0
cts 12
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthorizationCode() 12 12 2
A setAuthorizationCode() 0 17 1
A expireAuthorizationCode() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}