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

Event::_setData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helix\Asana;
4
5
use Helix\Asana\Base\AbstractEntity;
6
use Helix\Asana\Base\Data;
7
use Helix\Asana\Event\Change;
8
use Helix\Asana\Project\Section;
9
use Helix\Asana\Task\Attachment;
10
use Helix\Asana\Task\FieldEntry;
11
use Helix\Asana\Task\Like;
12
use Helix\Asana\Task\Story;
13
14
/**
15
 * An event obtained via sync token or delivered to you via webhook.
16
 *
17
 * > :warning: Asana has a "feature" that squashes "duplicate" events into the oldest date,
18
 * > as well as removing events inbetween, effectively rewriting history.
19
 * >
20
 * > As such, any particular sequence of events should not be greatly relied upon for continuity.
21
 * > Cached or already-pooled entities will be stale as well.
22
 * >
23
 * > You should reload the resource and go off of that data instead.
24
 *
25
 * @see https://developers.asana.com/docs/get-events-on-a-resource
26
 * @see https://developers.asana.com/docs/event
27
 *
28
 * @see Project::getEvents()
29
 * @see Task::getEvents()
30
 * @see Api::getWebhookEvent()
31
 * @see AbstractEntity::reload()
32
 *
33
 * @method string       getAction       () The action-verb for the event.
34
 * @method null|Change  getChange       () The change made on the resource.
35
 * @method bool         hasChange       () False if the event was relational.
36
 * @method string       getCreatedAt    () When the event took place.
37
 * @method bool         hasParent       () True if the event was relational.
38
 * @method null|User    getUser         () The initiator, if there was one.
39
 * @method bool         hasUser         () When false, Asana initiated the event. ("system")
40
 */
41
class Event extends Data {
42
43
    const ACTION_CHANGED = 'changed';       // no parent
44
    const ACTION_ADDED = 'added';           // relational, no change
45
    const ACTION_REMOVED = 'removed';       // relational, no change
46
    const ACTION_DELETED = 'deleted';       // no parent or change
47
    const ACTION_UNDELETED = 'undeleted';   // no parent or change
48
49
    const GRAPH = [
50
        User::TYPE => User::class,
51
        Project::TYPE => Project::class,
52
        Section::TYPE => Section::class,
53
        Task::TYPE => Task::class,
54
        CustomField::TYPE => FieldEntry::class,
55
        Attachment::TYPE => Attachment::class,
56
        Story::TYPE => Story::class,
57
        Like::TYPE => Like::class
58
    ];
59
60
    protected const MAP = [
61
        'change' => Change::class,
62
        'user' => User::class
63
    ];
64
65
    protected function _setData (array $data): void {
66
        if (isset($data['parent'])) {
67
            $type = $data['parent']['resource_type'];
68
            $data['parent'] = $this->_hydrate(static::GRAPH[$type], $data['parent']);
69
        }
70
71
        $type = $data['resource']['resource_type'];
72
        $data['resource'] = $this->_hydrate(static::GRAPH[$type], $data['resource']);
73
74
        parent::_setData($data);
75
    }
76
77
    /**
78
     * The parent resource, if the event was relational.
79
     *
80
     * @return null|Project|Section|Task
81
     */
82
    public function getParent () {
83
        return $this->data['parent'] ?? null;
84
    }
85
86
    /**
87
     * The relational child, or the entity that was changed.
88
     *
89
     * @return User|Project|Section|Task|CustomField|Attachment|Story|Like
90
     */
91
    public function getResource () {
92
        return $this->data['resource'];
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    final public function wasAddition (): bool {
99
        return $this->getAction() === self::ACTION_ADDED;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    final public function wasChange (): bool {
106
        return $this->getAction() === self::ACTION_CHANGED;
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    final public function wasDeletion (): bool {
113
        return $this->getAction() === self::ACTION_DELETED;
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    final public function wasRemoval (): bool {
120
        return $this->getAction() === self::ACTION_REMOVED;
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    final public function wasUndeletion (): bool {
127
        return $this->getAction() === self::ACTION_UNDELETED;
128
    }
129
130
}