StreamState   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 55
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 8 2
A close() 0 12 3
A isReady() 0 4 1
A __destruct() 0 6 2
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
21
    const STATE_READY = 1;
22
23
    const STATE_CLOSED = 2;
24
25
    /**
26
     * @var int
27
     */
28
    private $state = self::STATE_CREATED;
29
30 45
    public function open()
31
    {
32 45
        if ($this->state == self::STATE_READY) {
33 6
            throw StreamStateException::alreadyOpened();
34
        }
35
36 45
        $this->state = self::STATE_READY;
37 45
    }
38
39 62
    public function close()
40
    {
41 62
        if ($this->state == self::STATE_CLOSED) {
42 31
            throw StreamStateException::alreadyClosed();
43
        }
44
45 62
        if ($this->state != self::STATE_READY) {
46 18
            throw StreamStateException::notOpened();
47
        }
48
49 44
        $this->state = self::STATE_CLOSED;
50 44
    }
51
52
    /**
53
     * Stream is ready to receive data.
54
     *
55
     * @return bool
56
     */
57 36
    public function isReady()
58
    {
59 36
        return $this->state == self::STATE_READY;
60
    }
61
62
    /**
63
     * Did you not forget to close the stream?
64
     */
65 39
    public function __destruct()
66
    {
67 39
        if ($this->state == self::STATE_READY) {
68 1
            throw StreamStateException::notClosed();
69
        }
70 38
    }
71
}
72