Passed
Push — main ( 4ee39f...33155d )
by Justin
03:54
created

CallbackOptionHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 2
dl 0
loc 6
rs 10
1
<?php
2
declare(strict_types=1);
3
namespace Modulate\Artisan\Interceptor\Handlers;
4
5
use Modulate\Artisan\Interceptor\Contracts\OptionHandler;
6
use Modulate\Artisan\Interceptor\InterceptedCommand;
7
8
class CallbackOptionHandler implements OptionHandler
9
{
10
11
    /**
12
     * @var string
13
     */
14
    protected $option;
15
16
    /**
17
     * @var callable
18
     */
19
    protected $callable;
20
21
    /**
22
     *
23
     * @param string $option
24
     * @param callable $callable
25
     */
26
    public function __construct(
27
        string $option,
28
        callable $callable,
29
    ) {
30
        $this->option   = $option;
31
        $this->callable = $callable;
32
    }
33
34
    public function setOption($option): OptionHandler
35
    {
36
        $this->option = $option;
37
38
        return $this;
39
    }
40
41
    /**
42
     * Check to see if the required option is present and has a value
43
     * If false is returned then the callback is not executed
44
     *
45
     * @param InterceptedCommand $intercepted
46
     * @return boolean
47
     */
48
    public function check(InterceptedCommand $intercepted): bool
49
    {
50
        if (
51
            $intercepted->getInput()->hasOption($this->option)
52
            // If the option has a value or does not accept a value
53
            && ($intercepted->getInput()->getOption($this->option)
54
                || !$intercepted->getArtisan()->getDefinition()->getOption($this->option)->acceptValue())
55
        ) {
56
            return true;
57
        }
58
        return false;
59
60
    }
61
62
    /**
63
     * The handle function will execute the stored closure
64
     *
65
     * @param InterceptedCommand $intercepted
66
     * @return void
67
     */
68
    public function handle(InterceptedCommand $intercepted): void
69
    {
70
        if ($this->callable) {
71
            call_user_func($this->callable, $intercepted);
72
        }
73
    }
74
}
75