Data   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 0
dl 0
loc 148
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getData() 0 4 1
A getMessage() 0 4 1
A setMessage() 0 4 1
A merge() 0 6 1
A update() 0 11 2
A mergeData() 0 6 1
A hasExecutionTime() 0 4 1
A getExecutionTime() 0 7 2
A accumulateExecutionTime() 0 9 3
A accumulateMessage() 0 8 2
1
<?php
2
3
namespace Robo\State;
4
5
/**
6
 * A State\Data object contains a "message" (the primary result) and a
7
 * data array (the persistent state). The message is transient, and does
8
 * not move into the persistent state unless explicitly copied there.
9
 */
10
class Data extends \ArrayObject
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $message;
16
17
    /**
18
     * @param string $message
19
     * @param array $data
20
     */
21
    public function __construct($message = '', $data = [])
22
    {
23
        $this->message = $message;
24
        parent::__construct($data);
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    public function getData()
31
    {
32
        return $this->getArrayCopy();
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function getMessage()
39
    {
40
        return $this->message;
41
    }
42
43
    /**
44
     * @param string $message
45
     */
46
    public function setMessage($message)
47
    {
48
        $this->message = $message;
49
    }
50
51
    /**
52
     * Merge another result into this result.  Data already
53
     * existing in this result takes precedence over the
54
     * data in the Result being merged.
55
     *
56
     * @param \Robo\State\Data $result
57
     *
58
     * @return $this
59
     */
60
    public function merge(Data $result)
61
    {
62
        $mergedData = $this->getArrayCopy() + $result->getArrayCopy();
63
        $this->exchangeArray($mergedData);
64
        return $this;
65
    }
66
67
    /**
68
     * Update the current data with the data provided in the parameter.
69
     * Provided data takes precedence.
70
     *
71
     * @param \ArrayObject $update
72
     *
73
     * @return $this
74
     */
75
    public function update(\ArrayObject $update)
76
    {
77
        $iterator = $update->getIterator();
78
79
        while ($iterator->valid()) {
80
            $this[$iterator->key()] = $iterator->current();
81
            $iterator->next();
82
        }
83
84
        return $this;
85
    }
86
87
    /**
88
     * Merge another result into this result.  Data already
89
     * existing in this result takes precedence over the
90
     * data in the Result being merged.
91
     *
92
     * $data['message'] is handled specially, and is appended
93
     * to $this->message if set.
94
     *
95
     * @param array $data
96
     *
97
     * @return array
98
     */
99
    public function mergeData(array $data)
100
    {
101
        $mergedData = $this->getArrayCopy() + $data;
102
        $this->exchangeArray($mergedData);
103
        return $mergedData;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function hasExecutionTime()
110
    {
111
        return isset($this['time']);
112
    }
113
114
    /**
115
     * @return null|float
116
     */
117
    public function getExecutionTime()
118
    {
119
        if (!$this->hasExecutionTime()) {
120
            return null;
121
        }
122
        return $this['time'];
123
    }
124
125
    /**
126
     * Accumulate execution time
127
     *
128
     * @param array|float $duration
129
     *
130
     * @return null|float
131
     */
132
    public function accumulateExecutionTime($duration)
133
    {
134
        // Convert data arrays to scalar
135
        if (is_array($duration)) {
136
            $duration = isset($duration['time']) ? $duration['time'] : 0;
137
        }
138
        $this['time'] = $this->getExecutionTime() + $duration;
139
        return $this->getExecutionTime();
140
    }
141
142
    /**
143
     * Accumulate the message.
144
     *
145
     * @param string $message
146
     *
147
     * @return string
148
     */
149
    public function accumulateMessage($message)
150
    {
151
        if (!empty($this->message)) {
152
            $this->message .= "\n";
153
        }
154
        $this->message .= $message;
155
        return $this->getMessage();
156
    }
157
}
158