Passed
Push — master ( 2428ce...06a092 )
by y
02:04
created

OrganizationExport::wait()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 9
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
     * Whether the export is in progress.
37
     *
38
     * @return bool
39
     */
40
    final public function isActive (): bool {
41
        return $this->getState() === self::STATE_ACTIVE;
42
    }
43
44
    /**
45
     * Whether the export has completed successfully or failed.
46
     *
47
     * @return bool
48
     */
49
    final public function isDone (): bool {
50
        return $this->isSuccessful() or $this->isFailed();
51
    }
52
53
    /**
54
     * Whether the export failed.
55
     *
56
     * @return bool
57
     */
58
    final public function isFailed (): bool {
59
        return $this->getState() === self::STATE_FAIL;
60
    }
61
62
    /**
63
     * Whether the export has yet to be started.
64
     *
65
     * @return bool
66
     */
67
    final public function isQueued (): bool {
68
        return $this->getState() === self::STATE_QUEUED;
69
    }
70
71
    /**
72
     * Whether the export completed successfully.
73
     *
74
     * @return bool
75
     */
76
    final public function isSuccessful (): bool {
77
        return $this->getState() === self::STATE_SUCCESS;
78
    }
79
80
    /**
81
     * Sleeps a minute between reloads until the export completes successfully or fails.
82
     *
83
     * A spinner can be called every sleep cycle to indicate progress.
84
     *
85
     * @param callable $spinner `fn( OrganizationExport $this ): void`
86
     * @return $this
87
     */
88
    public function wait (callable $spinner = null) {
89
        while (!$this->isDone()) {
90
            if ($spinner) {
91
                call_user_func($spinner, $this);
92
            }
93
            sleep(60);
94
            $this->reload();
95
        }
96
        return $this;
97
    }
98
}