Passed
Push — develop ( 6bc206...75605a )
by Carsten
10:11
created

PdoUsernameChecker::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 2
rs 9.3142
c 0
b 0
f 0
1
<?php
2
namespace Germania\UserProfiles;
3
4
use Germania\UserProfiles\Exceptions\LoginNameNotAvailableException;
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
8
/**
9
 * Checks if a given username is available,
10
 * i.e. if it is not already in use.
11
 *
12
 * Example:
13
 *
14
 *     <?php
15
 *     use Germania\UserProfiles\PdoUsernameChecker;
16
 *
17
 *     $pdo    = new PDO( ... );
18
 *     $logger = new Monolog();
19
 *     $table  = 'users';
20
 *
21
 *     $checker = new PdoUsernameChecker( $pdo, $logger, $table);
22
 *     $result = $checker( 'johndoe_224' );
23
 *     ?>
24
 *
25
 * @author  Carsten Witt <[email protected]>
26
 */
27
class PdoUsernameChecker
28
{
29
30
    /**
31
     * Database table
32
     * @var string
33
     */
34
    public $table = 'users';
35
36
    /**
37
     * @var PDOStatement
0 ignored issues
show
Bug introduced by
The type Germania\UserProfiles\PDOStatement was not found. Did you mean PDOStatement? If so, make sure to prefix the type with \.
Loading history...
38
     */
39
    public $stmt;
40
41
    /**
42
     * @var LoggerInterface
43
     */
44
    public $logger;
45
46
47
    /**
48
     * @param PDO             $pdo    PDO instance
49
     * @param LoggerInterface $logger Optional: PSR-3 Logger
50
     * @param string          $table  Optional: Database table name
51
     */
52 16
    public function __construct( \PDO $pdo, LoggerInterface $logger = null, $table = null)
53
    {
54
        // Setup
55 16
        $this->logger = $logger ?: new NullLogger;
56 16
        $this->table  = $table  ?: $this->table;
57
58
        // Prepare business
59
        $sql = "SELECT id
60 16
        FROM {$this->table}
61
        WHERE user_login_name = :username
62 4
        LIMIT 1";
63
64 16
        $this->stmt = $pdo->prepare( $sql );
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo->prepare($sql) of type PDOStatement is incompatible with the declared type Germania\UserProfiles\PDOStatement of property $stmt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65
66 16
    }
67
68
69
    /**
70
     * @param  string $user_name
71
     * @return bool   TRUE if username is available
72
     *
73
     * @throws LoginNameNotAvailableException if username is *not* available
74
     */
75 12
    public function __invoke( $user_name )
76
    {
77 12
        $bool = $this->stmt->execute([
0 ignored issues
show
Unused Code introduced by
The assignment to $bool is dead and can be removed.
Loading history...
78 9
          'username' => $user_name
79 3
        ]);
80
81 12
        $available = !$this->stmt->fetchColumn();
82
83
        // Evaluate
84
        $loginfo = [
85 12
            'user_name' => $user_name,
86 9
            'result'    => $available
87 3
        ];
88
89 12
        if ($available):
90 8
            $this->logger->info("Login name chosen is available.", $loginfo );
91 8
            return $available;
92
        endif;
93
94 4
        $this->logger->warning("Login name chosen not available!?", $loginfo);
95 4
        throw new LoginNameNotAvailableException;
96
    }
97
98
99
100
}
101