CreateSubscriber::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
4
namespace SirSova\Webhooks\Console;
5
6
use Illuminate\Console\Command;
7
use Illuminate\Validation\ValidationException;
8
use SirSova\Webhooks\Contracts\SubscriberRepository;
9
10
class CreateSubscriber extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'webhooks:subscriber
18
                            {event : Subscriber name}
19
                            {url : Webhook target url}
20
                            {--disabled : Set status `enabled` false}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create a new webhook subscriber';
28
    
29
    /**
30
     * @var SubscriberRepository
31
     */
32
    protected $subscriberRepository;
33
34
    public function __construct(SubscriberRepository $subscriberRepository)
35
    {
36
        parent::__construct();
37
        
38
        $this->subscriberRepository = $subscriberRepository;
39
    }
40
41
    /**
42
     * @return int
43
     */
44
    public function handle(): int
45
    {
46
        $data = [
47
            'event'   => $this->argument('event'),
48
            'url'     => $this->argument('url'),
49
            'enabled' => !(bool)$this->option('disabled')
50
        ];
51
        
52
        try {
53
            $this->subscriberRepository->create($data);
54
        } catch (ValidationException $e) {
55
            foreach ($e->errors() as $message) {
56
                $this->output->error($message);
57
            }
58
            
59
            return 1;
60
        }
61
        
62
        return 0;
63
    }
64
}
65