Passed
Push — master ( abee1f...edc958 )
by Kirill
03:08
created

Set::patch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Config\Patch;
13
14
use Spiral\Config\Exception\DotNotFoundException;
15
use Spiral\Config\Exception\PatchException;
16
use Spiral\Config\Patch\Traits\DotTrait;
17
use Spiral\Config\PatchInterface;
18
19
/**
20
 * Set the value.
21
 */
22
final class Set implements PatchInterface
23
{
24
    use DotTrait;
0 ignored issues
show
Bug introduced by
The trait Spiral\Config\Patch\Traits\DotTrait requires the property $prefix which is not provided by Spiral\Config\Patch\Set.
Loading history...
25
26
    /** @var string */
27
    private $key;
28
29
    /** @var mixed */
30
    private $value;
31
32
    /**
33
     * @param string $key
34
     * @param mixed  $value
35
     */
36
    public function __construct(string $key, $value)
37
    {
38
        $this->key = $key;
39
        $this->value = $value;
40
    }
41
42
    /**
43
     * @param array $config
44
     * @return array
45
     */
46
    public function patch(array $config): array
47
    {
48
        try {
49
            $target = &$this->dotGet($config, $this->key);
50
            $target = $this->value;
51
        } catch (DotNotFoundException $e) {
52
            throw new PatchException($e->getMessage(), $e->getCode(), $e);
53
        }
54
55
        return $config;
56
    }
57
}
58