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

OrganizationExport::_getMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Helix\Asana\Workspace;
4
5
use Helix\Asana\Base\AbstractEntity;
6
use Helix\Asana\Base\AbstractEntity\CreateTrait;
7
use Helix\Asana\Workspace;
8
9
/**
10
 * An organization export.
11
 *
12
 * @see https://developers.asana.com/docs/asana-organization-exports
13
 *
14
 * @method string       getCreatedAt    ()
15
 * @method null|string  getDownloadUrl  ()
16
 * @method string       getState        ()
17
 * @method Workspace    getOrganization ()
18
 */
19
class OrganizationExport extends AbstractEntity {
20
21
    use CreateTrait {
22
        create as private _create;
23
    }
24
25
    const TYPE = 'organization_export';
26
27
    const STATE_QUEUED = 'pending';
28
    const STATE_ACTIVE = 'started';
29
    const STATE_SUCCESS = 'finished';
30
    const STATE_FAIL = 'error';
31
32
    protected static $map = [
33
        'organization' => Workspace::class
34
    ];
35
36
    final public function __toString (): string {
37
        return "organization_exports/{$this->getGid()}";
38
    }
39
40
    final protected function _getDir (): string {
41
        return "organization_exports";
42
    }
43
44
    /**
45
     * @param Workspace $organization
46
     * @return $this
47
     */
48
    public function create (Workspace $organization) {
49
        $this->_set('organization', $organization);
50
        return $this->_create();
51
    }
52
53
    /**
54
     * Whether the export is in progress.
55
     *
56
     * @return bool
57
     */
58
    public function isActive (): bool {
59
        return $this->getState() === self::STATE_ACTIVE;
60
    }
61
62
    /**
63
     * Whether the export has completed successfully or failed.
64
     *
65
     * @return bool
66
     */
67
    public function isDone (): bool {
68
        return $this->isSuccessful() or $this->isFailed();
69
    }
70
71
    /**
72
     * Whether the export failed.
73
     *
74
     * @return bool
75
     */
76
    public function isFailed (): bool {
77
        return $this->getState() === self::STATE_FAIL;
78
    }
79
80
    /**
81
     * Whether the export has yet to be started.
82
     *
83
     * @return bool
84
     */
85
    public function isQueued (): bool {
86
        return $this->getState() === self::STATE_QUEUED;
87
    }
88
89
    /**
90
     * Whether the export completed successfully.
91
     *
92
     * @return bool
93
     */
94
    public function isSuccessful (): bool {
95
        return $this->getState() === self::STATE_SUCCESS;
96
    }
97
98
    /**
99
     * Sleeps and reloads "every few minutes" until the export completes successfully or fails.
100
     */
101
    public function wait () {
102
        while (!$this->isDone()) {
103
            sleep(240);
104
            $this->reload();
105
        }
106
    }
107
}