Mode   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 97
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setMode() 0 8 2
A getMode() 0 4 1
A isReading() 0 4 1
A isWriting() 0 4 1
A isAppending() 0 4 1
1
<?php
2
3
4
namespace Pcelta\VirtualStream;
5
6
class Mode
7
{
8
    /**
9
     * @var string
10
     */
11
    private $mode;
12
13
    // Pointer is placed in the beginning
14
    const READING = 'r';
15
    const READING_PLUS_WRITING = 'r+';
16
    const WRITING = 'w';
17
    const WRITING_PLUS_READING = 'w+';
18
    const CREATE_FOR_WRITING = 'x';
19
    const CREATE_FOR_WRITING_PLUS_READING = 'x+';
20
    const TRUNCATE_FOR_WRITING = 'c'; // If the file exists it will be truncated otherwise it will be created
21
    const TRUNCATE_FOR_WRITING_PLUS_READING = 'c+';
22
23
    // Pointer is placed in the end
24
    const APPEND_WRITING = 'a';
25
    const APPEND_WRITING_PLUS_READING = 'a+';
26
27
    /**
28
     * @var array
29
     */
30
    protected static $allowedModes = [
31
        self::READING,
32
        self::READING_PLUS_WRITING,
33
        self::WRITING,
34
        self::WRITING_PLUS_READING,
35
        self::APPEND_WRITING,
36
        self::APPEND_WRITING_PLUS_READING,
37
        self::CREATE_FOR_WRITING,
38
        self::CREATE_FOR_WRITING_PLUS_READING,
39
        self::TRUNCATE_FOR_WRITING,
40
        self::TRUNCATE_FOR_WRITING_PLUS_READING
41
    ];
42
43
    protected static $readingModes = [
44
        self::READING,
45
        self::READING_PLUS_WRITING,
46
        self::WRITING_PLUS_READING,
47
        self::APPEND_WRITING_PLUS_READING,
48
        self::CREATE_FOR_WRITING_PLUS_READING,
49
        self::TRUNCATE_FOR_WRITING_PLUS_READING
50
    ];
51
52
    protected static $writingModes = [
53
        self::READING_PLUS_WRITING,
54
        self::WRITING,
55
        self::WRITING_PLUS_READING,
56
        self::APPEND_WRITING,
57
        self::APPEND_WRITING_PLUS_READING,
58
        self::CREATE_FOR_WRITING,
59
        self::CREATE_FOR_WRITING_PLUS_READING,
60
        self::TRUNCATE_FOR_WRITING,
61
        self::TRUNCATE_FOR_WRITING_PLUS_READING
62
    ];
63
64
    protected static $appendingMode = [
65
        self::APPEND_WRITING,
66
        self::APPEND_WRITING_PLUS_READING,
67
    ];
68
69 46
    public function __construct(string $mode)
70
    {
71 46
        $this->setMode($mode);
72 38
    }
73
74 46
    public function setMode(string $mode)
75
    {
76 46
        if (!in_array($mode, self::$allowedModes)) {
77 8
            throw new ModeNotAllowedException($mode);
78
        }
79
80 38
        $this->mode = $mode;
81 38
    }
82
83 2
    public function getMode(): string
84
    {
85 2
        return $this->mode;
86
    }
87
88 15
    public function isReading(): bool
89
    {
90 15
        return in_array($this->mode, self::$readingModes);
91
    }
92
93 17
    public function isWriting(): bool
94
    {
95 17
        return in_array($this->mode, self::$writingModes);
96
    }
97
98 15
    public function isAppending(): bool
99
    {
100 15
        return in_array($this->mode, self::$appendingMode);
101
    }
102
}
103