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

Job   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 21
c 4
b 1
f 0
dl 0
loc 76
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isFailed() 0 2 1
A isQueued() 0 2 1
A __toString() 0 2 1
A isSuccessful() 0 2 1
A isActive() 0 2 1
A wait() 0 6 2
A isDone() 0 2 2
1
<?php
2
3
namespace Helix\Asana;
4
5
use Helix\Asana\Base\AbstractEntity;
6
7
/**
8
 * An asynchronous job.
9
 *
10
 * @see https://developers.asana.com/docs/asana-jobs
11
 * @see https://developers.asana.com/docs/job
12
 *
13
 * @immutable Jobs may only be polled.
14
 *
15
 * @method null|Project getNewProject       ()
16
 * @method null|Task    getNewTask          ()
17
 * @method string       getResourceSubtype  ()
18
 * @method string       getStatus           ()
19
 */
20
class Job extends AbstractEntity {
21
22
    const TYPE = 'job';
23
    const TYPE_DUPLICATE_PROJECT = 'duplicate_project';
24
    const TYPE_DUPLICATE_TASK = 'duplicate_task';
25
26
    const STATUS_QUEUED = 'not_started';
27
    const STATUS_ACTIVE = 'in_progress';
28
    const STATUS_SUCCESS = 'succeeded'; // api docs say "completed" but that's wrong.
29
    const STATUS_FAIL = 'failed';
30
31
    protected static $map = [
32
        'new_project' => Project::class,
33
        'new_task' => Task::class
34
    ];
35
36
    final public function __toString (): string {
37
        return "jobs/{$this->getGid()}";
38
    }
39
40
    /**
41
     * Whether the job is in progress.
42
     *
43
     * @return bool
44
     */
45
    public function isActive (): bool {
46
        return $this->getStatus() === self::STATUS_ACTIVE;
47
    }
48
49
    /**
50
     * Whether the job has completed successfully or failed.
51
     *
52
     * @return bool
53
     */
54
    public function isDone (): bool {
55
        return $this->isSuccessful() or $this->isFailed();
56
    }
57
58
    /**
59
     * Whether the job failed.
60
     *
61
     * @return bool
62
     */
63
    public function isFailed (): bool {
64
        return $this->getStatus() === self::STATUS_FAIL;
65
    }
66
67
    /**
68
     * Whether the job has yet to be started.
69
     *
70
     * @return bool
71
     */
72
    public function isQueued (): bool {
73
        return $this->getStatus() === self::STATUS_QUEUED;
74
    }
75
76
    /**
77
     * Whether the job completed successfully.
78
     *
79
     * @return bool
80
     */
81
    public function isSuccessful (): bool {
82
        return $this->getStatus() === self::STATUS_SUCCESS;
83
    }
84
85
    /**
86
     * Sleeps a few seconds between reloads until the job completes successfully or fails.
87
     *
88
     * @return $this
89
     */
90
    public function wait () {
91
        while (!$this->isDone()) {
92
            sleep(3);
93
            $this->reload();
94
        }
95
        return $this;
96
    }
97
}