Passed
Push — master ( 02e6b9...8f2f39 )
by y
01:57
created

Change::wasValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
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\CustomField;
7
use Helix\Asana\Event;
8
use Helix\Asana\Project;
9
use Helix\Asana\Project\Section;
10
use Helix\Asana\Task;
11
use Helix\Asana\Task\Attachment;
12
use Helix\Asana\Task\FieldEntry;
13
use Helix\Asana\Task\Like;
14
use Helix\Asana\Task\Story;
15
use Helix\Asana\User;
16
17
/**
18
 * The change on the event's resource.
19
 *
20
 * @see https://developers.asana.com/docs/event
21
 *
22
 * @method string getAction () The change's action-verb.
23
 * @method string getField  ()
24
 */
25
class Change extends Data {
26
27
    const GRAPH = [
28
        User::TYPE => User::class,
29
        Project::TYPE => Project::class,
30
        Section::TYPE => Section::class,
31
        Task::TYPE => Task::class,
32
        CustomField::TYPE => FieldEntry::class, // entry!
33
        Attachment::TYPE => Attachment::class,
34
        Story::TYPE => Story::class,
35
        Like::TYPE => Like::class
36
    ];
37
38
    /**
39
     * @var string
40
     */
41
    protected $key;
42
43
    protected function _setData (array $data): void {
44
        $this->key = [
45
            Event::ACTION_ADDED => 'added_value',
46
            Event::ACTION_REMOVED => 'removed_value',
47
            Event::ACTION_CHANGED => 'new_value'
48
        ][$data['action']];
49
50
        if ($payload = $data[$this->key] ?? null) {
51
            $payload = $this->_hydrate(self::GRAPH[$payload['resource_type']], $payload);
52
        }
53
54
        $this->data = [
55
            'action' => $data['action'],
56
            'field' => $data['field'],
57
            $this->key => $payload
58
        ];
59
    }
60
61
    /**
62
     * The contents of the change.
63
     *
64
     * > :warning:
65
     * > This is `null` for changes to scalar fields.
66
     * > You should reload the event's resource and check it.
67
     *
68
     * @return null|User|Project|Section|Task|FieldEntry|Attachment|Story|Like
69
     */
70
    public function getPayload () {
71
        return $this->data[$this->key];
72
    }
73
74
    /**
75
     * @return bool
76
     */
77
    final public function hasPayload (): bool {
78
        return isset($this->data[$this->key]);
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    final public function wasAddition (): bool {
85
        return $this->getAction() === Event::ACTION_ADDED;
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    final public function wasRemoval (): bool {
92
        return $this->getAction() === Event::ACTION_REMOVED;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    final public function wasValue (): bool {
99
        return $this->getAction() === Event::ACTION_CHANGED;
100
    }
101
102
}