Passed
Push — master ( 3c0c31...b6d585 )
by y
02:02
created

Change::_setData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Helix\Asana\Event;
4
5
use Helix\Asana\Base\Data;
6
use Helix\Asana\Event;
7
use Helix\Asana\Project;
8
use Helix\Asana\Project\Section;
9
use Helix\Asana\Task;
10
use Helix\Asana\Task\Attachment;
11
use Helix\Asana\Task\FieldEntry;
12
use Helix\Asana\Task\Like;
13
use Helix\Asana\Task\Story;
14
use Helix\Asana\User;
15
16
/**
17
 * The change on the event's resource.
18
 *
19
 * @see https://developers.asana.com/docs/event
20
 *
21
 * @method string getAction () The change's action-verb.
22
 * @method string getField  ()
23
 */
24
class Change extends Data {
25
26
    /**
27
     * @var string
28
     */
29
    protected $valueKey;
30
31
    protected function _setData (array $data): void {
32
        $this->valueKey = [
33
            'added' => 'added_value',
34
            'removed' => 'removed_value',
35
            'changed' => 'new_value'
36
        ][$data['action']];
37
38
        $value = $data[$this->valueKey] ?? null;
39
        if (is_array($value)) {
40
            $value = $this->_hydrate(Event::GRAPH[$value['resource_type']], $value);
41
        }
42
43
        $this->data = [
44
            'action' => $data['action'],
45
            'field' => $data['field'],
46
            $this->valueKey => $value
47
        ];
48
    }
49
50
    /**
51
     * The contents of the change.
52
     *
53
     * > :warning: This is `null` for changes to scalar fields.
54
     *
55
     * @return null|User|Project|Section|Task|FieldEntry|Attachment|Story|Like
56
     */
57
    public function getPayload () {
58
        return $this->data[$this->valueKey];
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    final public function hasPayload (): bool {
65
        return isset($this->data[$this->valueKey]);
66
    }
67
68
}