Passed
Push — master ( 262356...2c0ae5 )
by y
01:43
created

Attachment::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
 * @see https://developers.asana.com/docs/asana-attachments
15
 * @see https://developers.asana.com/docs/attachment
16
 *
17
 * @immutable Attachments may only be deleted after creation.
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 TYPE = 'attachment';
32
33
    protected const MAP = [
34
        'parent' => Task::class
35
    ];
36
37
    /**
38
     * `attachments/{gid}`
39
     *
40
     * @return string
41
     */
42
    final public function __toString (): string {
43
        return "attachments/{$this->getGid()}";
44
    }
45
46
    /**
47
     * Creates the attachment by uploading a file.
48
     *
49
     * @see https://developers.asana.com/docs/upload-an-attachment
50
     *
51
     * @param string $file
52
     * @return $this
53
     */
54
    public function create (string $file) {
55
        // api returns compact version. reload.
56
        $remote = $this->api->call('POST', "{$this->getParent()}/attachments", [
57
            CURLOPT_POSTFIELDS => ['file' => new CURLFile(realpath($file))] // multipart/form-data
58
        ])['data'];
59
        $this->data['gid'] = $remote['gid'];
60
        $this->reload();
61
        return $this;
62
    }
63
}