CreateTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 13 2
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
}