Attachment   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

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

1 Method

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