Passed
Push — master ( 1a00b5...984447 )
by y
03:02
created

Attachment   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 2
Metric Value
eloc 10
c 8
b 1
f 2
dl 0
loc 26
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 2 1
A upload() 0 6 1
1
<?php
2
3
namespace Helix\Asana\Task;
4
5
use Helix\Asana\Base\AbstractEntity;
6
use Helix\Asana\Base\AbstractEntity\DeleteTrait;
7
use Helix\Asana\Task;
8
9
/**
10
 * A file attachment.
11
 *
12
 * @see https://developers.asana.com/docs/asana-attachments
13
 * @see https://developers.asana.com/docs/attachment
14
 *
15
 * @immutable Attachments may only be deleted after creation.
16
 *
17
 * @method string   getCreatedAt    ()
18
 * @method string   getDownloadUrl  ()
19
 * @method string   getHost         ()
20
 * @method string   getName         ()
21
 * @method Task     getParent       ()
22
 * @method string   getPermanentUrl () Short, human-friendly.
23
 * @method string   getViewUrl      ()
24
 */
25
class Attachment extends AbstractEntity {
26
27
    use DeleteTrait;
28
29
    const TYPE = 'attachment';
30
31
    protected static $map = [
32
        'parent' => Task::class
33
    ];
34
35
    final public function __toString (): string {
36
        return "attachments/{$this->getGid()}";
37
    }
38
39
    /**
40
     * Creates the attachment by uploading a file.
41
     *
42
     * @param string $file
43
     * @return $this
44
     */
45
    public function upload (string $file) {
46
        // api returns compact version. reload.
47
        $remote = $this->api->upload("{$this->getParent()}/attachments", $file);
48
        $this->data['gid'] = $remote['gid'];
49
        $this->reload();
50
        return $this;
51
    }
52
}