Completed
Push — master ( d972fa...aafada )
by Dmytro
13:29
created

AbstractStrategy::unSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Webhook\Domain\Infrastructure\Strategy;
6
7
8
/**
9
 * Class AbstractStrategy
10
 *
11
 * @package Webhook\Domain\Infrastructure\Strategy
12
 */
13
abstract class AbstractStrategy implements \Serializable, \JsonSerializable, StrategyInterface
14
{
15
    /**
16
     * @return array
17
     */
18
    public function jsonSerialize()
19
    {
20
        return $this->getOptions() + ['name' => StrategyRegistry::getName($this)];
21
    }
22
23
    /**
24
     * @return string
25
     */
26 1
    public function serialize(): string
27
    {
28 1
        return json_encode($this->getOptions());
29
    }
30
31
    /**
32
     * @return array
33
     */
34 1
    public function getOptions(): array
35
    {
36 1
        return get_object_vars($this);
37
    }
38
39
    /**
40
     * @param string $serialized
41
     */
42 1
    public function unSerialize($serialized)
43
    {
44 1
        $data = json_decode($serialized, true);
45
46 1
        $this->setOptions($data);
47 1
    }
48
49
    /**
50
     * @param array $options
51
     */
52 1
    public function setOptions(array $options)
53
    {
54 1
        foreach ($options as $key => $val) {
55 1
            $method = 'set' . ucfirst($key);
56 1
            if ($method !== __FUNCTION__ && method_exists($this, $method)) {
57 1
                $this->{$method}($val);
58
            }
59
        }
60
    }
61
}