Completed
Push — master ( 0d3592...0bb29a )
by Peter
06:28
created

StreamState::close()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Component\Sitemap\Stream\State;
11
12
use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;
13
14
/**
15
 * Service for monitoring the status of the stream.
16
 */
17
class StreamState
18
{
19
    const STATE_CREATED = 0;
20
    const STATE_READY = 1;
21
    const STATE_CLOSED = 2;
22
23
    /**
24
     * @var int
25
     */
26
    public $state = self::STATE_CREATED;
27
28
    public function open()
29
    {
30
        if ($this->state == self::STATE_READY) {
31
            throw StreamStateException::alreadyOpened();
32
        }
33
34
        $this->state = self::STATE_READY;
35
    }
36
37
    public function close()
38
    {
39
        if ($this->state == self::STATE_CLOSED) {
40
            throw StreamStateException::alreadyClosed();
41
        }
42
43
        if ($this->state != self::STATE_READY) {
44
            throw StreamStateException::notOpened();
45
        }
46
47
        $this->state = self::STATE_CLOSED;
48
    }
49
50
    /**
51
     * Stream is ready to receive data.
52
     *
53
     * @return bool
54
     */
55
    public function isReady()
56
    {
57
        return $this->state == self::STATE_READY;
58
    }
59
60
    /**
61
     * Did you not forget to close the stream?
62
     */
63
    public function __destruct()
64
    {
65
        if ($this->state == self::STATE_READY) {
66
            throw StreamStateException::notClosed();
67
        }
68
    }
69
}
70