Passed
Pull Request — master (#18)
by Harry
04:35 queued 23s
created

RunningStateTrait::setState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of graze/parallel-process.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/parallel-process/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/parallel-process
12
 */
13
14
namespace Graze\ParallelProcess;
15
16
trait RunningStateTrait
17
{
18
    /** @var float */
19
    protected $started = 0.0;
20
    /** @var float */
21
    protected $finished = 0.0;
22
    /** @var int */
23
    private $state = RunInterface::STATE_NOT_STARTED;
24
25
    /**
26
     * @return float
27
     */
28 33
    public function getDuration()
29
    {
30 33
        if ($this->finished > 0) {
31 27
            return $this->finished - $this->started;
32
        }
33 32
        return $this->started > 0 ? microtime(true) - $this->started : 0;
34
    }
35
36
    /**
37
     * Starts this thing
38
     *
39
     * @param int|null $time Optional time to say that we started at
40
     */
41 56
    protected function setStarted($time = null)
42
    {
43 56
        $this->started = $time ?: microtime(true);
44 56
        $this->setState(RunInterface::STATE_RUNNING);
45 56
    }
46
47
    /**
48
     * this thing has finished
49
     *
50
     * @param int|null $time Optional time to say that we finished at
51
     */
52 53
    protected function setFinished($time = null)
53
    {
54 53
        $this->finished = $time ?: microtime(true);
55 53
        $this->setState(RunInterface::STATE_NOT_RUNNING);
56 53
    }
57
58
    /**
59
     * @return int RunInterface::STATE_*
60
     */
61 72
    protected function getState()
62
    {
63 72
        return $this->state;
64
    }
65
66
    /**
67
     * @param int $state RunInterface::STATE_*
68
     *
69
     * @return $this
70
     */
71 56
    protected function setState($state)
72
    {
73 56
        $this->state = $state;
74 56
        return $this;
75
    }
76
}
77