GermaniaKG /
UserProfiles
| 1 | <?php |
||||
| 2 | namespace Germania\UserProfiles; |
||||
| 3 | |||||
| 4 | use Germania\UserProfiles\Exceptions\InsertUserException; |
||||
| 5 | |||||
| 6 | use Psr\Log\LoggerInterface; |
||||
| 7 | use Psr\Log\NullLogger; |
||||
| 8 | |||||
| 9 | use Ramsey\Uuid\Uuid; |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * This Callable insert a new User and returns his new ID. |
||||
| 13 | * |
||||
| 14 | * Example: |
||||
| 15 | * |
||||
| 16 | * <?php |
||||
| 17 | * use Germania\UserProfiles\PdoInsertNewUser; |
||||
| 18 | * |
||||
| 19 | * $pdo = new PDO( ... ); |
||||
| 20 | * $logger = new Monolog(); |
||||
| 21 | * $table = 'users'; |
||||
| 22 | * |
||||
| 23 | * $inserter = new PdoInsertNewUser( $pdo, $logger, $table); |
||||
| 24 | * $new_id = $inserter([ |
||||
| 25 | * 'first_name' => 'John', |
||||
| 26 | * 'last_name' => 'Doe', |
||||
| 27 | * 'display_name' => 'John Doe', |
||||
| 28 | * 'email' => '[email protected]', |
||||
| 29 | * 'login' => '[email protected]' |
||||
| 30 | * ]); |
||||
| 31 | * ?> |
||||
| 32 | * |
||||
| 33 | * @author Carsten Witt <[email protected]> |
||||
| 34 | */ |
||||
| 35 | class PdoInsertNewUser |
||||
| 36 | { |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * Database table |
||||
| 40 | * @var string |
||||
| 41 | */ |
||||
| 42 | public $table = 'users'; |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * @var PDO |
||||
| 46 | */ |
||||
| 47 | public $pdo; |
||||
| 48 | |||||
| 49 | /** |
||||
| 50 | * @var PDOStatement |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 51 | */ |
||||
| 52 | public $stmt; |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * @var LoggerInterface |
||||
| 56 | */ |
||||
| 57 | public $logger; |
||||
| 58 | |||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * @param PDO $pdo PDO instance |
||||
| 62 | * @param LoggerInterface $logger Optional: PSR-3 Logger |
||||
| 63 | * @param string $table Optional: Database table name |
||||
| 64 | */ |
||||
| 65 | 25 | public function __construct( \PDO $pdo, LoggerInterface $logger = null, $table = null ) |
|||
| 66 | { |
||||
| 67 | // Setup |
||||
| 68 | 25 | $this->pdo = $pdo; |
|||
|
0 ignored issues
–
show
It seems like
$pdo of type PDO is incompatible with the declared type Germania\UserProfiles\PDO of property $pdo.
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...
|
|||||
| 69 | 25 | $this->logger = $logger ?: new NullLogger; |
|||
| 70 | 25 | $this->table = $table ?: $this->table; |
|||
| 71 | |||||
| 72 | // Prepare business |
||||
| 73 | // For UUID creation, see |
||||
| 74 | // https://mysqlserverteam.com/storing-uuid-values-in-mysql-tables/ |
||||
| 75 | 25 | $sql = "INSERT INTO {$this->table} ( |
|||
| 76 | uuid, |
||||
| 77 | user_login_name, |
||||
| 78 | user_display_name, |
||||
| 79 | user_email, |
||||
| 80 | user_last_name, |
||||
| 81 | user_first_name, |
||||
| 82 | created |
||||
| 83 | ) VALUES ( |
||||
| 84 | UNHEX( REPLACE( :uuid,'-','' )), |
||||
| 85 | :user_login_name, |
||||
| 86 | :user_display_name, |
||||
| 87 | :user_email, |
||||
| 88 | :user_last_name, |
||||
| 89 | :user_first_name, |
||||
| 90 | :created |
||||
| 91 | 5 | )"; |
|||
| 92 | |||||
| 93 | // Prepare |
||||
| 94 | 25 | $this->stmt = $this->pdo->prepare( $sql ); |
|||
|
0 ignored issues
–
show
It seems like
$this->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...
|
|||||
| 95 | |||||
| 96 | 25 | } |
|||
| 97 | |||||
| 98 | |||||
| 99 | |||||
| 100 | |||||
| 101 | /** |
||||
| 102 | * Inserts the user using the passed array. It must contain these elements: |
||||
| 103 | * |
||||
| 104 | * - first_name |
||||
| 105 | * - last_name |
||||
| 106 | * - display_name |
||||
| 107 | |||||
| 108 | * - login |
||||
| 109 | * |
||||
| 110 | * @param array $user_data User data |
||||
| 111 | * |
||||
| 112 | * @return int New User ID |
||||
| 113 | * |
||||
| 114 | * @throws InsertUserException|UserProfileExceptionInterface if PDOStatement execution fails or missing required field. |
||||
| 115 | */ |
||||
| 116 | 20 | public function __invoke( array $user_data ) |
|||
| 117 | { |
||||
| 118 | |||||
| 119 | // Setup |
||||
| 120 | $required_fields = [ |
||||
| 121 | 20 | 'first_name', |
|||
| 122 | 4 | 'last_name', |
|||
| 123 | 4 | 'display_name', |
|||
| 124 | 4 | 'email', |
|||
| 125 | 'login' |
||||
| 126 | 4 | ]; |
|||
| 127 | |||||
| 128 | // Check data |
||||
| 129 | 20 | foreach($required_fields as $rf): |
|||
| 130 | 20 | if (!array_key_exists($rf, $user_data)) : |
|||
| 131 | |||||
| 132 | 5 | $message = "Missing field '$rf' in user data"; |
|||
| 133 | 5 | $this->logger->error( $message ); |
|||
| 134 | 17 | throw new InsertUserException( $message ); |
|||
| 135 | |||||
| 136 | endif; |
||||
| 137 | 4 | endforeach; |
|||
| 138 | |||||
| 139 | // Create UUID |
||||
| 140 | 15 | $uuid = Uuid::uuid4()->__toString(); |
|||
|
0 ignored issues
–
show
The method
__toString() does not exist on Ramsey\Uuid\UuidInterface. Did you maybe mean toString()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 141 | |||||
| 142 | // Perform |
||||
| 143 | 15 | $result = $this->stmt->execute([ |
|||
| 144 | 15 | 'uuid' => $uuid, |
|||
| 145 | 15 | 'user_login_name' => $user_data[ "login" ], |
|||
| 146 | 15 | 'user_display_name' => $user_data[ "display_name" ], |
|||
| 147 | 15 | 'user_email' => $user_data[ "email" ], |
|||
| 148 | 15 | 'user_last_name' => $user_data[ "last_name" ], |
|||
| 149 | 15 | 'user_first_name' => $user_data[ "first_name" ], |
|||
| 150 | 15 | 'created' => date('Y-m-d H:i:s') |
|||
| 151 | 15 | ]) ? $this->pdo->lastInsertId() : null; |
|||
| 152 | |||||
| 153 | |||||
| 154 | // Evaluate |
||||
| 155 | $loginfo = [ |
||||
| 156 | 15 | 'new_user_id' => $result, |
|||
| 157 | 15 | 'uuid' => $uuid, |
|||
| 158 | 15 | 'user_display_name' => $user_data[ "display_name" ] |
|||
| 159 | 3 | ]; |
|||
| 160 | 15 | if ($result): |
|||
| 161 | 10 | $this->logger->info("Successfully added new user.", $loginfo); |
|||
| 162 | 10 | return $result; |
|||
| 163 | endif; |
||||
| 164 | |||||
| 165 | 5 | $this->logger->error("Could not add new user?!", $loginfo); |
|||
| 166 | 5 | throw new InsertUserException; |
|||
| 167 | |||||
| 168 | } |
||||
| 169 | |||||
| 170 | |||||
| 171 | } |
||||
| 172 |