GenerateAppAuth::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 22
rs 9.7998
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ArcherZdip\LaravelApiAuth\Console\Commands;
4
5
use Illuminate\Console\Command;
0 ignored issues
show
Bug introduced by
The type Illuminate\Console\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use ArcherZdip\LaravelApiAuth\Models\AppClient;
7
8
class GenerateAppAuth extends Command
9
{
10
    /**
11
     * Error messages
12
     */
13
    const MESSAGE_ERROR_INVALID_NAME_FORMAT = 'Invalid name.  Must be a lowercase alphabetic characters, numbers and hyphens less than 255 characters long.';
14
    const MESSAGE_ERROR_NAME_ALREADY_USED = 'Name is unavailable.';
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'apikey:generate {name}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = "Generate a new appId";
28
29
    /**
30
     * Create a new command instance.
31
     *
32
     * @return void
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return mixed
43
     */
44
    public function handle()
45
    {
46
        $name = $this->argument('name');
47
48
        $this->validateName($name);
49
50
        $attributes = [
51
            'name'   => $name,
52
            'appid'  => AppClient::generateAppId(),
53
            'secret' => AppClient::generateSecret(),
54
        ];
55
56
        /** @var AppClient $appClient */
57
        $appClient = AppClient::forceCreate($attributes);
58
        $headers = ['AppName', 'appId', 'secret', 'CreateAt'];
59
        $rows = [
60
            $appClient->name,
61
            $appClient->appid,
62
            $appClient->secret,
63
            $appClient->created_at
64
        ];
65
        $this->table($headers, [$rows]);
66
    }
67
68
    /**
69
     * validate name
70
     *
71
     * @param string $name
72
     * @return null
73
     */
74
    protected function validateName(string $name)
75
    {
76
        if (!AppClient::isValidName($name)) {
77
            $this->error(self::MESSAGE_ERROR_INVALID_NAME_FORMAT);
78
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
79
        }
80
81
        if (AppClient::nameExists($name)) {
82
            $this->error(self::MESSAGE_ERROR_NAME_ALREADY_USED);
83
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
84
        }
85
86
        return null;
87
    }
88
}
89