Completed
Pull Request — master (#29)
by Tim
13:43
created

ServerStateKeys::getServerStates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * AppserverIo\Server\Dictionaries\ServerStateKeys
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/server
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Server\Dictionaries;
22
23
use AppserverIo\Server\Exceptions\InvalidServerStateException;
24
25
/**
26
 * Utility class that contains the server state keys.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/server
32
 * @link      http://www.appserver.io
33
 */
34
class ServerStateKeys
35
{
36
37
    /**
38
     * Server has to be stopped.
39
     *
40
     * @var integer
41
     */
42
    const HALT = 0;
43
44
    /**
45
     * Server is waiting for initialization.
46
     *
47
     * @var integer
48
     */
49
    const WAITING_FOR_INITIALIZATION = 1;
50
51
    /**
52
     * Server has been successfully initialized.
53
     *
54
     * @var integer
55
     */
56
    const INITIALIZATION_SUCCESSFUL = 2;
57
58
    /**
59
     * Servers socket has been started successful.
60
     *
61
     * @var integer
62
     */
63
    const SERVER_SOCKET_STARTED = 3;
64
65
    /**
66
     * Servers modules has successfully been initialized.
67
     *
68
     * @var integer
69
     */
70
    const MODULES_INITIALIZED = 4;
71
72
    /**
73
     * Servers connection handlers has successfully been initialized.
74
     *
75
     * @var integer
76
     */
77
    const CONNECTION_HANDLERS_INITIALIZED = 5;
78
79
    /**
80
     * Servers workers has successfully been initialized.
81
     *
82
     * @var integer
83
     */
84
    const WORKERS_INITIALIZED = 6;
85
86
    /**
87
     * Server has successfully been shutdown.
88
     *
89
     * @var integer
90
     */
91
    const SHUTDOWN = 7;
92
93
    /**
94
     * The actual server state.
95
     *
96
     * @var integer
97
     */
98
    private $serverState;
99
100
    /**
101
     * This is a utility class, so protect it against direct
102
     * instantiation.
103
     *
104
     * @param integer $serverState The server state to initialize the instance with
105
     */
106
    private function __construct($serverState)
107
    {
108
        $this->serverState = $serverState;
109
    }
110
111
    /**
112
     * This is a utility class, so protect it against cloning.
113
     *
114
     * @return void
115
     */
116
    private function __clone()
117
    {
118
    }
119
120
    /**
121
     * Returns the server state representation as integer.
122
     *
123
     * @return integer The integer representation of the server state
124
     */
125
    public function getServerState()
126
    {
127
        return $this->serverState;
128
    }
129
130
    /**
131
     * Returns the server state representation as string.
132
     *
133
     * @return string The string representation of the server state
134
     * @see \TechDivision\ApplicationServer\Dictionaries\ServerStateKeys::getServerState()
135
     */
136
    public function __toString()
137
    {
138
        return sprintf('%d', $this->getServerState());
139
    }
140
141
    /**
142
     * Returns the server states.
143
     *
144
     * @return array The server states
145
     */
146
    public static function getServerStates()
147
    {
148
        return array(
149
            ServerStateKeys::HALT,
150
            ServerStateKeys::WAITING_FOR_INITIALIZATION,
151
            ServerStateKeys::INITIALIZATION_SUCCESSFUL,
152
            ServerStateKeys::SERVER_SOCKET_STARTED,
153
            ServerStateKeys::MODULES_INITIALIZED,
154
            ServerStateKeys::CONNECTION_HANDLERS_INITIALIZED,
155
            ServerStateKeys::WORKERS_INITIALIZED,
156
            ServerStateKeys::SHUTDOWN,
157
        );
158
    }
159
160
    /**
161
     * Returns TRUE if the server state is greater than the passed one, else FALSE.
162
     *
163
     * @param \AppserverIo\Server\Dictionaries\ServerStateKeys $serverState The server state to be greater than
164
     *
165
     * @return boolean TRUE if equal, else FALSE
166
     */
167
    public function greaterOrEqualThan(ServerStateKeys $serverState)
168
    {
169
        return $this->serverState >= $serverState->getServerState();
170
    }
171
172
    /**
173
     * Returns TRUE if the passed server state equals the actual one, else FALSE.
174
     *
175
     * @param \AppserverIo\Server\Dictionaries\ServerStateKeys $serverState The server state to check
176
     *
177
     * @return boolean TRUE if equal, else FALSE
178
     */
179
    public function equals(ServerStateKeys $serverState)
180
    {
181
        return $this->serverState === $serverState->getServerState();
182
    }
183
184
    /**
185
     * Factory method to create a new server state instance.
186
     *
187
     * @param integer $serverState The server state to create an instance for
188
     *
189
     * @return \AppserverIo\Server\Dictionaries\ServerStateKeys The server state key instance
190
     * @throws \AppserverIo\Server\Exceptions\InvalidServerStateException
191
     *      Is thrown if the server state is not available
192
     */
193
    public static function get($serverState)
194
    {
195
196
        // check if the requested server state is available and create a new instance
197
        if (in_array($serverState, ServerStateKeys::getServerStates())) {
198
            return new ServerStateKeys($serverState);
199
        }
200
201
        // throw a exception if the requested server state is not available
202
        throw new InvalidServerStateException(
203
            sprintf(
204
                'Requested server state %s is not available (choose on of: %s)',
205
                $serverState,
206
                implode(',', ServerStateKeys::getServerStates())
207
            )
208
        );
209
    }
210
}
211