Change   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
eloc 28
c 3
b 0
f 1
dl 0
loc 76
rs 10

6 Methods

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