CreateTrait::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Helix\Asana\Base\AbstractEntity;
4
5
use Helix\Asana\Base\AbstractEntity;
6
7
/**
8
 * Adds `create()` to entities.
9
 *
10
 * @mixin AbstractEntity
11
 */
12
trait CreateTrait {
13
14
    /**
15
     * The parent entity, if any, needed for creation.
16
     *
17
     * @var null|AbstractEntity
18
     */
19
    protected $parent;
20
21
    /**
22
     * Creates the new entity in Asana.
23
     *
24
     * @return $this
25
     */
26
    public function create () {
27
        assert(!$this->hasGid());
28
        $path = static::DIR;
29
        if (isset($this->parent)) {
30
            assert($this->parent->hasGid());
31
            $path = "{$this->parent}/{$path}";
32
        }
33
        $remote = $this->api->post($path, $this->toArray(true), ['expand' => 'this']);
34
        $this->_setData($remote);
35
        /** @var AbstractEntity $that */
36
        $that = $this;
37
        $this->api->getPool()->add($that);
38
        return $this;
39
    }
40
}