GermaniaKG /
UserProfiles
| 1 | <?php |
||
| 2 | namespace Germania\UserProfiles; |
||
| 3 | |||
| 4 | use Germania\UserProfiles\Exceptions\SetApiKeyException; |
||
| 5 | use Psr\Log\LoggerInterface; |
||
| 6 | use Psr\Log\NullLogger; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * This Callable sets a new random API key for a user's ID. |
||
| 10 | * |
||
| 11 | * Example: |
||
| 12 | * |
||
| 13 | * use Germania\UserProfiles\PdoApiKeySetter; |
||
| 14 | * |
||
| 15 | * $pdo = new PDO( ... ); |
||
| 16 | * $rnd = function() { return 'ABCDEF'; }; |
||
| 17 | * $logger = new Monolog(); |
||
| 18 | * $table = 'users'; |
||
| 19 | * |
||
| 20 | * $setter = new PdoApiKeySetter( $pdo, $random_gen, $logger, $table); |
||
| 21 | * $result = $setter( 42 ); |
||
| 22 | * ?> |
||
| 23 | * |
||
| 24 | * @author Carsten Witt <[email protected]> |
||
| 25 | */ |
||
| 26 | class PdoApiKeySetter |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Database table |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | public $table = 'users'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var PDOStatement |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 37 | */ |
||
| 38 | public $stmt; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var LoggerInterface |
||
| 42 | */ |
||
| 43 | public $logger; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Callable |
||
| 47 | */ |
||
| 48 | public $random_gen; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param PDO $pdo PDO instance |
||
| 52 | * @param Callable $random_gen Callable Random Generator |
||
| 53 | * @param LoggerInterface $logger Optional: PSR-3 Logger |
||
| 54 | * @param string $table Optional: Database table name |
||
| 55 | */ |
||
| 56 | 20 | public function __construct( \PDO $pdo, Callable $random_gen, LoggerInterface $logger = null, $table = null ) |
|
| 57 | { |
||
| 58 | |||
| 59 | // Prerequisites |
||
| 60 | 20 | $this->table = $table ?: $this->table; |
|
| 61 | 20 | $this->random_gen = $random_gen; |
|
| 62 | 20 | $this->logger = $logger; |
|
| 63 | |||
| 64 | // Prepare business |
||
| 65 | 20 | $sql = "UPDATE {$this->table} |
|
| 66 | SET api_key = :apikey |
||
| 67 | WHERE id = :user_id |
||
| 68 | 4 | LIMIT 1"; |
|
| 69 | |||
| 70 | // Store for later use |
||
| 71 | 20 | $this->stmt = $pdo->prepare( $sql ); |
|
|
0 ignored issues
–
show
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...
|
|||
| 72 | 20 | } |
|
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * @param int $user_id |
||
| 77 | * |
||
| 78 | * @return bool TRUE on success |
||
| 79 | * |
||
| 80 | * @throws SetApiKeyException if PDOStatement execution fails. |
||
| 81 | */ |
||
| 82 | 15 | public function __invoke( $user_id ) |
|
| 83 | { |
||
| 84 | // Calc values |
||
| 85 | 15 | $randomgen = $this->random_gen; |
|
| 86 | 15 | $apikey = $randomgen(); |
|
| 87 | |||
| 88 | // Perform |
||
| 89 | 15 | $result = $this->stmt->execute([ |
|
| 90 | 15 | 'apikey' => $apikey, |
|
| 91 | 12 | 'user_id' => $user_id |
|
| 92 | 3 | ]); |
|
| 93 | |||
| 94 | // Evaluate |
||
| 95 | $loginfo = [ |
||
| 96 | 15 | 'user_id' => $user_id, |
|
| 97 | 15 | 'mb_strlen' => mb_strlen($apikey ), |
|
| 98 | 12 | 'result' => $result |
|
| 99 | 3 | ]; |
|
| 100 | |||
| 101 | 15 | if ($result): |
|
| 102 | 10 | $this->logger->info("Successfully created API key.", $loginfo); |
|
| 103 | 10 | return $result; |
|
| 104 | endif; |
||
| 105 | |||
| 106 | 5 | $this->logger->warning("Could not set API key?!", $loginfo); |
|
| 107 | 5 | throw new SetApiKeyException; |
|
| 108 | } |
||
| 109 | } |
||
| 110 |