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