Failed Conditions
Pull Request — newinternal (#527)
by Simon
17:20 queued 07:22
created

CredentialProviderBase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCredentialData() 0 20 2
A getDatabase() 0 4 1
A getConfiguration() 0 4 1
A createNewCredential() 0 9 1
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Security\CredentialProviders;
10
11
use Waca\DataObjects\Credential;
12
use Waca\DataObjects\User;
13
use Waca\PdoDatabase;
14
use Waca\SiteConfiguration;
15
16
abstract class CredentialProviderBase implements ICredentialProvider
17
{
18
    /**
19
     * @var PdoDatabase
20
     */
21
    private $database;
22
    /**
23
     * @var SiteConfiguration
24
     */
25
    private $configuration;
26
    /** @var string */
27
    private $type;
28
29
    /**
30
     * CredentialProviderBase constructor.
31
     *
32
     * @param PdoDatabase       $database
33
     * @param SiteConfiguration $configuration
34
     * @param string            $type
35
     */
36
    public function __construct(PdoDatabase $database, SiteConfiguration $configuration, $type)
37
    {
38
        $this->database = $database;
39
        $this->configuration = $configuration;
40
        $this->type = $type;
41
    }
42
43
    /**
44
     * @param int $userId
45
     *
46
     * @return Credential
0 ignored issues
show
Documentation introduced by
Should the return type not be Credential|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
47
     */
48
    protected function getCredentialData($userId)
49
    {
50
        $sql = 'SELECT * FROM credential WHERE type = :t AND user = :u AND disabled = 0';
51
52
        $statement = $this->database->prepare($sql);
53
        $statement->execute(array(':u' => $userId, ':t' => $this->type));
54
55
        /** @var Credential $obj */
56
        $obj = $statement->fetchObject(Credential::class);
57
58
        if ($obj === false) {
59
            return null;
60
        }
61
62
        $obj->setDatabase($this->database);
63
64
        $statement->closeCursor();
65
66
        return $obj;
67
    }
68
69
    /**
70
     * @return PdoDatabase
71
     */
72
    public function getDatabase()
73
    {
74
        return $this->database;
75
    }
76
77
    /**
78
     * @return SiteConfiguration
79
     */
80
    public function getConfiguration()
81
    {
82
        return $this->configuration;
83
    }
84
85
    /**
86
     * @param User $user
87
     *
88
     * @return Credential
89
     */
90
    protected function createNewCredential(User $user)
91
    {
92
        $credential = new Credential();
93
        $credential->setDatabase($this->getDatabase());
94
        $credential->setUserId($user->getId());
95
        $credential->setType($this->type);
96
97
        return $credential;
98
    }
99
}