Completed
Push — master ( 47ba96...4fd14e )
by Gerrit
02:18
created

createArgumentFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Arguments\ArgumentFactory;
14
15
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
16
use Addiks\SymfonyGenerics\Arguments\Argument;
17
use Addiks\SymfonyGenerics\Arguments\AdditionalDataArgument;
18
use Webmozart\Assert\Assert;
19
use Addiks\SymfonyGenerics\Arguments\ArgumentContextInterface;
20
21
final class AdditionalDataArgumentFactory implements ArgumentFactory
22
{
23
24
    /**
25
     * @var ArgumentContextInterface
26
     */
27
    private $context;
28
29 30
    public function __construct(ArgumentContextInterface $context)
30
    {
31 30
        $this->context = $context;
32 30
    }
33
34 15
    public function understandsString(string $source): bool
35
    {
36 15
        return 1 === preg_match("/^\%[a-zA-Z0-9_-]+\%$/is", $source);
37
    }
38
39 15
    public function understandsArray(array $source): bool
40
    {
41 15
        return isset($source['key']) && isset($source['type']) && $source['type'] === 'additional-data';
42
    }
43
44 7
    public function createArgumentFromString(string $source): Argument
45
    {
46 7
        Assert::true($this->understandsString($source));
47
48
        /** @var string $key */
49 3
        $key = substr($source, 1, strlen($source) - 2);
50
51 3
        return new AdditionalDataArgument($key, $this->context);
52
    }
53
54 6
    public function createArgumentFromArray(array $source): Argument
55
    {
56 6
        Assert::true($this->understandsArray($source));
57
58 3
        return new AdditionalDataArgument($source['key'], $this->context);
59
    }
60
61
}
62