Issues (21)

src/PdoUsernameChecker.php (3 issues)

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
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 20
    public function __construct( \PDO $pdo, LoggerInterface $logger = null, $table = null)
53
    {
54
        // Setup
55 20
        $this->logger = $logger ?: new NullLogger;
56 20
        $this->table  = $table  ?: $this->table;
57
58
        // Prepare business
59
        $sql = "SELECT id
60 20
        FROM {$this->table}
61
        WHERE user_login_name = :username
62 4
        LIMIT 1";
63
64 20
        $this->stmt = $pdo->prepare( $sql );
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo->prepare($sql) of type PDOStatement or boolean 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 20
    }
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 15
    public function __invoke( $user_name )
76
    {
77 15
        $bool = $this->stmt->execute([
0 ignored issues
show
The assignment to $bool is dead and can be removed.
Loading history...
78 12
          'username' => $user_name
79 3
        ]);
80
81 15
        $available = !$this->stmt->fetchColumn();
82
83
        // Evaluate
84
        $loginfo = [
85 15
            'user_name' => $user_name,
86 12
            'result'    => $available
87 3
        ];
88
89 15
        if ($available):
90 10
            $this->logger->info("Login name chosen is available.", $loginfo );
91 10
            return $available;
92
        endif;
93
94 5
        $this->logger->warning("Login name chosen not available!?", $loginfo);
95 5
        throw new LoginNameNotAvailableException;
96
    }
97
98
99
100
}
101