Event::getResource()   A
last analyzed

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;
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\Like;
11
use Helix\Asana\Task\Story;
12
13
/**
14
 * An event obtained via sync token or delivered to you via webhook.
15
 *
16
 * > :warning:
17
 * > 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 string       getCreatedAt    () RFC3339x
36
 * @method null|User    getUser         () The initiator, if there was one.
37
 *
38
 * @method bool         hasChange       () False if the event was relational.
39
 * @method bool         hasParent       () True if the event was relational.
40
 * @method bool         hasUser         () When false, Asana initiated the event. ("system")
41
 */
42
class Event extends Data {
43
44
    const ACTION_CHANGED = 'changed';       // no parent
45
    const ACTION_ADDED = 'added';           // relational, no change
46
    const ACTION_REMOVED = 'removed';       // relational, no change
47
    const ACTION_DELETED = 'deleted';       // no parent or change
48
    const ACTION_UNDELETED = 'undeleted';   // no parent or change
49
50
    const GRAPH = [
51
        User::TYPE => User::class,
52
        Project::TYPE => Project::class,
53
        Section::TYPE => Section::class,
54
        Task::TYPE => Task::class,
55
        CustomField::TYPE => CustomField::class, // field!
56
        Attachment::TYPE => Attachment::class,
57
        Tag::TYPE => Tag::class,
58
        Story::TYPE => Story::class,
59
        Like::TYPE => Like::class
60
    ];
61
62
    protected const MAP = [
63
        'change' => Change::class,
64
        'user' => User::class
65
    ];
66
67
    protected function _setData (array $data): void {
68
        if (isset($data['parent'])) {
69
            $type = $data['parent']['resource_type'];
70
            $data['parent'] = $this->_hydrate(static::GRAPH[$type], $data['parent']);
71
        }
72
73
        $type = $data['resource']['resource_type'];
74
        $data['resource'] = $this->_hydrate(static::GRAPH[$type], $data['resource']);
75
76
        parent::_setData($data);
77
    }
78
79
    /**
80
     * The parent resource, if the event was relational.
81
     *
82
     * @return null|Project|Section|Task
83
     */
84
    public function getParent () {
85
        return $this->data['parent'] ?? null;
86
    }
87
88
    /**
89
     * The relational child, or the entity that was changed.
90
     *
91
     * @return User|Project|Section|Task|CustomField|Attachment|Story|Like
92
     */
93
    public function getResource () {
94
        return $this->data['resource'];
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    final public function wasAddition (): bool {
101
        return $this->getAction() === self::ACTION_ADDED;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    final public function wasChange (): bool {
108
        return $this->getAction() === self::ACTION_CHANGED;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    final public function wasDeletion (): bool {
115
        return $this->getAction() === self::ACTION_DELETED;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    final public function wasRemoval (): bool {
122
        return $this->getAction() === self::ACTION_REMOVED;
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    final public function wasUndeletion (): bool {
129
        return $this->getAction() === self::ACTION_UNDELETED;
130
    }
131
}