Passed
Push — main ( 0bfc82...b73123 )
by Garbuz
02:45
created

CreateGlobalTokenCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 1
b 0
f 0
dl 0
loc 57
ccs 0
cts 13
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Garbuzivan\Laraveltokens\Commands;
6
7
use Carbon\Carbon;
8
use Garbuzivan\Laraveltokens\Exceptions\UserNotExistsException;
9
use Garbuzivan\Laraveltokens\TokenManager;
10
use Illuminate\Console\Command;
11
use Illuminate\Support\Composer;
12
13
class CreateGlobalTokenCommand extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'tokens:global-create {title} {day}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Создать новый токен (tokens:create {title} {количество дней действия или 0 - бессрочно})';
28
29
    /**
30
     * The console command signature.
31
     *
32
     * @var string
33
     */
34
    protected $signature = 'tokens:global-create {title} {day}';
35
36
    /**
37
     * @var Composer
38
     */
39
    public Composer $composer;
40
41
    /**
42
     * @var TokenManager
43
     */
44
    public TokenManager $TokenManager;
45
46
    /**
47
     * Create a new command instance.
48
     */
49
    public function __construct(TokenManager $TokenManager)
50
    {
51
        parent::__construct();
52
        $this->TokenManager = $TokenManager;
53
    }
54
55
    /**
56
     * Execute the command.
57
     *
58
     * @return mixed
59
     */
60
    public function handle()
61
    {
62
        $arguments = $this->arguments();
63
        $title = $arguments['title'] ?? date('Y-m-d H:i:s');
64
        $day = intval($arguments['day']);
65
        $expiration = $day > 0 ? Carbon::now()->addDays($day) : null;
66
        $token = $this->TokenManager->createGlobalToken($title, $expiration);
67
        $date = is_null($expiration) ? 'навсегда' : 'до ' . $expiration->format('Y-m-d H:i:s');
68
        $this->line('Глобальный токен ' . $token->token . ' создан '. $date . '.');
69
        return 1;
70
    }
71
}
72