DataOptionWrapper   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 120
c 0
b 0
f 0
wmc 20
lcom 1
cbo 0
ccs 34
cts 34
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 4
A getName() 0 4 1
A getShortcut() 0 4 1
A getDescription() 0 4 1
A getDefault() 0 8 2
A isNone() 0 4 1
A isRequired() 0 4 1
A isOptional() 0 4 1
A isArray() 0 4 1
A getMode() 0 11 5
A getModeValue() 0 4 1
A getData() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Commands\Wrappers;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Contracts\Commands\CommandInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use function assert;
24
use function is_string;
25
26
/**
27
 * @package Limoncello\Commands
28
 */
29
class DataOptionWrapper
30
{
31
    /**
32
     * @var array
33
     */
34
    private $data;
35 3
36
    /**
37 3
     * @param array $data
38
     */
39
    public function __construct(array $data)
40 3
    {
41 3
        $this->data = $data;
42 3
43
        // only one true or all false
44
        assert(
45
            ($this->isNone() xor $this->isRequired() xor $this->isOptional()) ||
46
            (!$this->isNone() && !$this->isRequired() && !$this->isOptional())
47
        );
48
    }
49 3
50
    /**
51 3
     * @return string
52
     */
53
    public function getName(): string
54
    {
55
        return $this->getData()[CommandInterface::OPTION_NAME];
56
    }
57 3
58
    /**
59 3
     * @return string|null
60
     */
61
    public function getShortcut(): ?string
62
    {
63
        return $this->getData()[CommandInterface::OPTION_SHORTCUT] ?? null;
64
    }
65 3
66
    /**
67 3
     * @return string
68
     */
69
    public function getDescription(): string
70
    {
71
        return $this->getData()[CommandInterface::OPTION_DESCRIPTION] ?? '';
72
    }
73 3
74
    /**
75 3
     * @return string|null
76
     */
77 3
    public function getDefault(): ?string
78
    {
79 3
        $value = $this->getData()[CommandInterface::OPTION_DEFAULT] ?? null;
80
81
        assert(is_string($value) === true || $value === null);
82
83
        return $value;
84
    }
85 3
86
    /**
87 3
     * @return bool
88
     */
89
    public function isNone(): bool
90
    {
91
        return ($this->getModeValue() & CommandInterface::OPTION_MODE__NONE) > 0;
92
    }
93 3
94
    /**
95 3
     * @return bool
96
     */
97
    public function isRequired(): bool
98
    {
99
        return ($this->getModeValue() & CommandInterface::OPTION_MODE__REQUIRED) > 0;
100
    }
101 3
102
    /**
103 3
     * @return bool
104
     */
105
    public function isOptional(): bool
106
    {
107
        return ($this->getModeValue() & CommandInterface::OPTION_MODE__OPTIONAL) > 0;
108
    }
109 3
110
    /**
111 3
     * @return bool
112
     */
113
    public function isArray(): bool
114
    {
115
        return ($this->getModeValue() & CommandInterface::OPTION_MODE__IS_ARRAY) > 0;
116
    }
117 3
118
    /**
119 3
     * @return int|null
120
     */
121 3
    public function getMode(): ?int
122 3
    {
123 3
        $mode = null;
124 3
125
        $this->isNone() === false     ?: $mode = (int)$mode | InputOption::VALUE_NONE;
126 3
        $this->isRequired() === false ?: $mode = (int)$mode | InputOption::VALUE_REQUIRED;
127
        $this->isOptional() === false ?: $mode = (int)$mode | InputOption::VALUE_OPTIONAL;
128
        $this->isArray() === false    ?: $mode = (int)$mode | InputOption::VALUE_IS_ARRAY;
129
130
        return $mode;
131
    }
132 3
133
    /**
134 3
     * @return int
135
     */
136
    protected function getModeValue(): int
137
    {
138
        return $this->getData()[CommandInterface::OPTION_MODE] ?? 0;
139
    }
140 3
141
    /**
142 3
     * @return array
143
     */
144
    protected function getData(): array
145
    {
146
        return $this->data;
147
    }
148
}
149