Issues (69)

src/Console/Commands/PutAppAuth.php (10 issues)

1
<?php
2
3
namespace ArcherZdip\LaravelApiAuth\Console\Commands;
4
5
use Illuminate\Support\Arr;
0 ignored issues
show
The type Illuminate\Support\Arr 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 Illuminate\Console\Command;
0 ignored issues
show
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...
7
use ArcherZdip\LaravelApiAuth\Models\AppClient;
8
9
class PutAppAuth extends Command
10
{
11
    /**
12
     * Error message
13
     */
14
    const MESSAGE_ERROR_PARAMS_MANY = "The put params is to many. There can only be one condition";
15
    const MESSAGE_ERROR_APPID_DOES_NOT_EXIST = 'AppId does not exist.';
16
    const MESSAGE_ERROR_APP_TRASHED = "This app has trashed";
17
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = "apikey:put {appid}
24
                            {--A|activate : Activate an App by appid}
25
                            {--F|deactivate : Deactivate an App by appid}
26
                            {--D|delete : Delete an App by appid}
27
                            {--R|refresh : refresh an app's secret by appid}";
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Change an AppId status';
35
36
    /**
37
     * Create a new command instance.
38
     *
39
     * @return void
40
     */
41
    public function __construct()
42
    {
43
        parent::__construct();
44
    }
45
46
    /**
47
     * Execute the console command.
48
     *
49
     * @return mixed
50
     * @throws \Exception
51
     */
52
    public function handle()
53
    {
54
        $appid = $this->argument('appid');
55
        /** @var AppClient $appClient */
56
        $appClient = $this->validateAppId($appid);
57
        $name = $appClient->name;
58
59
        $options = $this->options();
60
        $options = Arr::where($options, function ($option) {
61
            if ($option) {
62
                return $option;
63
            }
64
        });
65
66
        // too many params
67
        if (count($options) > 1) {
68
            $this->error(self::MESSAGE_ERROR_PARAMS_MANY);
69
            exit();
0 ignored issues
show
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...
70
        }
71
72
        // is trashed
73
        if ($appClient->trashed()) {
74
            $this->error(self::MESSAGE_ERROR_APP_TRASHED);
75
            exit();
0 ignored issues
show
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...
76
        }
77
78
        if (Arr::has($options, 'activate')) {
79
            // activate app
80
            if ($appClient->active) {
81
                $this->info('App "' . $name . '" is already active');
82
                exit();
0 ignored issues
show
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...
83
            }
84
            $appClient->active = AppClient::ACTIVATE;
85
            $appClient->save();
86
            $this->info("Activate app succ, name: {$name}");
87
88
        } elseif (Arr::has($options, 'deactivate')) {
89
            // deactivate app
90
            if (!$appClient->active) {
91
                $this->info('App "' . $name . '" is already deactivate');
92
                exit();
0 ignored issues
show
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...
93
            }
94
            $appClient->active = AppClient::DEACTIVATE;
95
            $appClient->save();
96
            $this->info("Deactivate app succ, name: {$name}");
97
98
        } elseif (Arr::has($options, 'delete')) {
99
            // delete app
100
            $confirmMessage = "Are you sure you want to delete AppId:{$appid}, name:{$name} ?";
101
102
            if ($this->confirm($confirmMessage)) {
103
                $appClient->delete();
104
                $this->info('Deleted app succ, name: ' . $name);
105
            }
106
            exit();
0 ignored issues
show
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...
107
        } elseif (Arr::has($options, 'refresh')) {
108
            // refresh this app secret
109
            $confirmMessage = "Are you sure you want to refresh this app secret, AppId:{$appid}, name:{$name} ?";
110
111
            if ($this->confirm($confirmMessage)) {
112
                $appClient->secret = AppClient::generateSecret();
113
                $appClient->save();
114
                $this->info('Refresh app secret succ, name: ' . $name);
115
            } else {
116
                exit();
0 ignored issues
show
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...
117
            }
118
        }
119
120
        // get this record detail
121
        $headers = ['AppName', 'AppId', 'Secret', 'Status', 'CreateAt'];
122
        $status = $appClient->active ? 'active' : 'deactivated';
123
        $status = $appClient->trashed() ? 'deleted' : $status;
124
125
        $rows = [[
126
            $appClient->name,
127
            $appClient->appid,
128
            $appClient->secret,
129
            $status,
130
            $appClient->created_at
131
        ]];
132
133
        $this->table($headers, $rows);
134
    }
135
136
    /**
137
     * Validate AppId
138
     *
139
     * @param $appId
140
     * @return mixed
141
     */
142
    protected function validateAppId(string $appId): AppClient
143
    {
144
        if (!AppClient::appIdExists($appId)) {
145
            $this->error(self::MESSAGE_ERROR_APPID_DOES_NOT_EXIST);
146
            exit();
0 ignored issues
show
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...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return ArcherZdip\LaravelApiAuth\Models\AppClient. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
147
        }
148
149
        return AppClient::withTrashed()->where('appid', '=', $appId)->first();
150
    }
151
}
152