CallbackOptionHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 64
ccs 15
cts 15
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 4 2
A __construct() 0 6 1
A setOption() 0 5 1
A check() 0 11 4
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 1
    public function __construct(
27
        string $option,
28
        callable $callable,
29
    ) {
30 1
        $this->option   = $option;
31 1
        $this->callable = $callable;
32
    }
33
34 1
    public function setOption($option): OptionHandler
35
    {
36 1
        $this->option = $option;
37
38 1
        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 1
    public function check(InterceptedCommand $intercepted): bool
49
    {
50
        if (
51 1
            $intercepted->getInput()->hasOption($this->option)
52
            // If the option has a value or does not accept a value
53 1
            && ($intercepted->getInput()->getOption($this->option)
54 1
                || !$intercepted->getArtisan()->getDefinition()->getOption($this->option)->acceptValue())
55
        ) {
56 1
            return true;
57
        }
58 1
        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 1
    public function handle(InterceptedCommand $intercepted): void
69
    {
70 1
        if ($this->callable) {
71 1
            call_user_func($this->callable, $intercepted);
72
        }
73
    }
74
}
75