Data   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 78
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommand() 0 3 1
A scenarios() 0 3 1
A getClient() 0 3 1
A ensureAttributes() 0 4 1
A __construct() 0 9 1
A get() 0 6 3
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-gateway-clients
4
 * @copyright Copyright (c) 2018 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\gatewayclients;
9
10
use GatewayClients\DataInterface;
11
12
use yii\base\DynamicModel;
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * Data is the base class that implements the DataInterface provide data for send or get from gateway server api.
17
 *
18
 * @property BaseClient $client The client of data.
19
 * @property string $command The command of data useful when detect attributes and scenario.
20
 *
21
 * @author Vuong Minh <[email protected]>
22
 * @since 1.0
23
 */
24
class Data extends DynamicModel implements DataInterface
25
{
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param string|int $command of this data
31
     * @param array $attributes use for parse to [[DynamicModel::construct()]]
32
     * @param BaseClient $client use for make data it may use for add fixed data.
33
     * @param array $config configurations to be applied to the newly created data object.
34
     */
35 6
    public function __construct($command, array $attributes = [], BaseClient $client, array $config = [])
36
    {
37 6
        $this->_command = $command;
38 6
        $this->_client = $client;
39
40 6
        $this->setScenario($this->getCommand());
41 6
        $this->ensureAttributes($attributes);
42
43 6
        parent::__construct($attributes, $config);
44 6
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 6
    public function scenarios()
50
    {
51 6
        return array_merge([$this->getCommand() => []], parent::scenarios());
52
    }
53
54
    /**
55
     * @param array $attributes for ensure value or add more attribute from client.
56
     */
57 6
    protected function ensureAttributes(array &$attributes)
58
    {
59 6
        $activeAttributes = array_fill_keys($this->activeAttributes(), null);
60 6
        $attributes = array_merge($activeAttributes, $attributes);
61 6
    }
62
63
    /**
64
     * @var string command of this data
65
     */
66
    private $_command;
67
68
    /**
69
     * Return command of this data. This method may called by [[ensureAttributes()]].
70
     * @return string
71
     */
72 6
    public function getCommand(): string
73
    {
74 6
        return $this->_command;
75
    }
76
77
    /**
78
     * @var BaseClient of this data
79
     */
80
    private $_client;
81
82
    /**
83
     * Return client of this data. This method may called by [[ensureAttributes()]] for add fixed data from client properties.
84
     *
85
     * @return BaseClient
86
     */
87 2
    public function getClient(): BaseClient
88
    {
89 2
        return $this->_client;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     * @throws InvalidConfigException
95
     */
96 6
    public function get(bool $validate = true): array
97
    {
98 6
        if (!$validate || $this->validate()) {
99 5
            return $this->toArray();
100
        } else {
101 1
            throw new InvalidConfigException(current($this->getFirstErrors()));
102
        }
103
    }
104
105
106
}
107