Passed
Push — main ( 69451c...874877 )
by Garbuz
03:25
created

src/Commands/CreateTokenCommand.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Garbuzivan\Laraveltokens\Commands;
6
7
use App\Models\User;
8
use Carbon\Carbon;
9
use Garbuzivan\Laraveltokens\Exceptions\UserNotExistsException;
10
use Garbuzivan\Laraveltokens\TokenManager;
11
use Illuminate\Console\Command;
12
use Illuminate\Support\Composer;
13
14
class CreateTokenCommand extends Command
15
{
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'tokens:create {title?} {day?} {user_id?}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Создать новый токен (tokens:create {title?} {day?} {user_id?})';
29
30
    /**
31
     * The console command signature.
32
     *
33
     * @var string
34
     */
35
    protected $signature = 'tokens:create {title?} {day?} {user_id?}';
36
37
    /**
38
     * @var Composer
39
     */
40
    public Composer $composer;
41
42
    /**
43
     * @var TokenManager
44
     */
45
    public TokenManager $TokenManager;
46
47
    /**
48
     * Create a new command instance.
49
     */
50
    public function __construct(TokenManager $TokenManager)
51
    {
52
        parent::__construct();
53
        $this->TokenManager = $TokenManager;
54
    }
55
56
    /**
57
     * Execute the command.
58
     *
59
     * @return mixed
60
     */
61
    public function handle()
62
    {
63
        $arguments = $this->arguments();
64
        $title = $arguments['title'] ?? date('Y-m-d H:i:s');
65
        $user_id = $arguments['user_id'] ? intval($arguments['user_id']) : null;
66
        $expiration = $arguments['day'] ? Carbon::now()->addDay(intval($arguments['day'])) : null;
0 ignored issues
show
The call to Carbon\Carbon::addDay() has too many arguments starting with intval($arguments['day']). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $expiration = $arguments['day'] ? Carbon::now()->/** @scrutinizer ignore-call */ addDay(intval($arguments['day'])) : null;

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
67
        try {
68
            $token = $this->TokenManager->create($title, $expiration, $user_id);
69
        } catch (UserNotExistsException $e) {
70
            $this->line('Пользователь ID ' . $user_id . ' не найден.');
71
            return 1;
72
        }
73
        $prependText = $token->user instanceof User
74
            ? 'Персональный токен ' . $token->token . ' для ' . $token->user->name
75
            : 'Глобальный токен ' . $token->token;
76
        $this->line($prependText . ' создан до ' . $token->expiration . '.');
77
        return 1;
78
    }
79
}
80