Passed
Push — master ( 2428ce...06a092 )
by y
02:04
created

Story::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helix\Asana\Task;
4
5
use Helix\Asana\Base\AbstractEntity;
6
use Helix\Asana\Base\AbstractEntity\CrudTrait;
7
use Helix\Asana\Task;
8
use Helix\Asana\User;
9
10
/**
11
 * A task story.
12
 *
13
 * @see https://developers.asana.com/docs/asana-stories
14
 * @see https://developers.asana.com/docs/story
15
 *
16
 * @method string       getCreatedAt        () RFC3339x
17
 * @method null|User    getCreatedBy        () This will be `null` if Asana produced the story.
18
 * @method bool         isLiked             () Whether the story is liked by the API user.
19
 * @method Like[]       getLikes            () Other people's likes.
20
 * @method int          getNumLikes         ()
21
 * @method string       getResourceSubtype  () See the subtype constants.
22
 * @method string       getSource           () `web|api`
23
 * @method Task         getTarget           ()
24
 * @method string       getText             ()
25
 *
26
 * @method $this        setLiked            (bool $liked)
27
 * @method $this        setText             (string $text) Comments only.
28
 *
29
 * @method Like[]       selectLikes         (callable $filter) `fn( Like $like ): bool`
30
 */
31
class Story extends AbstractEntity {
32
33
    use CrudTrait;
34
35
    const DIR = 'stories';
36
    const TYPE = 'story';
37
38
    const TYPE_ASSIGNED = 'assigned';
39
    const TYPE_COMMENT_ADDED = 'comment_added';
40
    const TYPE_COMMENT_LIKED = 'comment_liked';
41
    const TYPE_DUE_DATE_CHANGED = 'due_date_changed';
42
    const TYPE_LIKED = 'liked';
43
    const TYPE_TAGGED = 'added_to_tag';
44
45
    protected const MAP = [
46
        'created_by' => User::class,
47
        'likes' => [Like::class],
48
        'target' => Task::class
49
    ];
50
51
    public function __construct ($caller, array $data = []) {
52
        parent::__construct($caller, $data);
53
        $this->parent = $this->getTarget();
54
    }
55
56
    protected function _setData (array $data): void {
57
        // hearts were deprecated for likes
58
        unset($data['hearted'], $data['hearts'], $data['num_hearts']);
59
60
        parent::_setData($data);
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    final public function isAssignment (): bool {
67
        return $this->getResourceSubtype() === self::TYPE_ASSIGNED;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    final public function isComment (): bool {
74
        return $this->getResourceSubtype() === self::TYPE_COMMENT_ADDED;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    final public function isDueDate (): bool {
81
        return $this->getResourceSubtype() === self::TYPE_DUE_DATE_CHANGED;
82
    }
83
84
    /**
85
     * @return bool
86
     */
87
    final public function isEdited (): bool {
88
        return $this->_is('is_edited');
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    final public function isFromApi (): bool {
95
        return $this->getSource() === 'api';
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    final public function isFromWeb (): bool {
102
        return $this->getSource() === 'web';
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    final public function isPinned (): bool {
109
        return $this->_is('is_pinned');
110
    }
111
112
    /**
113
     * @param bool $pinned
114
     * @return $this
115
     */
116
    final public function setPinned (bool $pinned) {
117
        return $this->_set('is_pinned', $pinned);
118
    }
119
120
}