OrganizationExport::isActive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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