]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/HandlesSingleUser.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Console / Commands / HandlesSingleUser.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Users\Models\User;
6 use Exception;
7 use Illuminate\Console\Command;
8
9 /**
10  * @mixin Command
11  */
12 trait HandlesSingleUser
13 {
14     /**
15      * Fetch a user provided to this command.
16      * Expects the command to accept 'id' and 'email' options.
17      * @throws Exception
18      */
19     private function fetchProvidedUser(): User
20     {
21         $id = $this->option('id');
22         $email = $this->option('email');
23         if (!$id && !$email) {
24             throw new Exception("Either a --id=<number> or --email=<email> option must be provided.\nRun this command with `--help` to show more options.");
25         }
26
27         $field = $id ? 'id' : 'email';
28         $value = $id ?: $email;
29
30         $user = User::query()
31             ->where($field, '=', $value)
32             ->first();
33
34         if (!$user) {
35             throw new Exception("A user where {$field}={$value} could not be found.");
36         }
37
38         return $user;
39     }
40 }