GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 0e985d...08b4ed )
by Stuart
05:49
created

Host   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 271
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 0
cbo 14
dl 0
loc 271
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanupHosts() 0 4 1
A expectsFirstHostWithRole() 0 4 1
A expectsHost() 0 4 1
A expectsHostsTable() 0 4 1
A foreachHostWithRole() 0 4 1
A fromFirstHostWithRole() 0 4 1
A fromHost() 0 4 1
A fromHostsTable() 0 4 1
B getHostsWithRole() 0 22 4
A usingFirstHostWithRole() 0 4 1
A usingHost() 0 4 1
A usingHostsTable() 0 4 1
1
<?php
2
3
/**
4
 * Copyright (c) 2011-present Mediasift Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   Storyplayer
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://datasift.github.io/storyplayer
42
 */
43
44
namespace Storyplayer\SPv2\Modules;
45
46
use DataSift\Storyplayer\PlayerLib\StoryTeller;
47
use Storyplayer\SPv2\Modules\Host\CleanupHosts;
48
use Storyplayer\SPv2\Modules\Host\ExpectsFirstHostWithRole;
49
use Storyplayer\SPv2\Modules\Host\ExpectsHost;
50
use Storyplayer\SPv2\Modules\Host\FromFirstHostWithRole;
51
use Storyplayer\SPv2\Modules\Host\FromHost;
52
use Storyplayer\SPv2\Modules\Host\FromHostsTable;
53
use Storyplayer\SPv2\Modules\Host\UsingFirstHostWithRole;
54
use Storyplayer\SPv2\Modules\Host\UsingHost;
55
use Storyplayer\SPv2\Modules\Host\UsingHostsTable;
56
57
class Host
58
{
59
    /**
60
     * returns the CleanupHosts module
61
     *
62
     * This module is used internally when Storyplayer shuts down to cleanup any
63
     * remaining entries in the internal 'hosts' table.
64
     *
65
     * You will never need to call this module from your stories.
66
     *
67
     * @param  string $key
68
     *         the name of the hosts table in the runtime table
69
     * @return CleanupHosts
70
     */
71
    public static function cleanupHosts($key)
72
    {
73
        return new CleanupHosts(StoryTeller::instance(), [$key]);
74
    }
75
76
    /**
77
     * returns the ExpectsHost module, with its hostId already set to the first
78
     * host in your test environment config file that has the given role.
79
     *
80
     * This module provides support for making your tests work on multiple
81
     * test environments. Instead of hard-coding hostIds into your stories,
82
     * use this module to find a host by its assigned role. That way, it doesn't
83
     * matter how many hosts are in different environments, or if their hostIds
84
     * are different.
85
     *
86
     * This module is normally used for testing the status of a host, such as if
87
     * a given process is currently running. If the check fails, this module
88
     * will throw an exception.
89
     *
90
     * @param  string $roleName
91
     *         the assigned role you're looking for
92
     * @return ExpectsFirstHostWithRole
93
     */
94
    public static function expectsFirstHostWithRole($roleName)
95
    {
96
        return new ExpectsFirstHostWithRole(StoryTeller::instance(), [$roleName]);
97
    }
98
99
    /**
100
     * returns the ExpectsHost module
101
     *
102
     * This module provides support for checking on something on a computer in
103
     * your test environment. If the check fails, an exception is thrown for you.
104
     *
105
     * In SPv1, it was common to call this module directly from your own stories.
106
     * In SPv2, you're much more likely to use one of our multi-host modules or
107
     * helpers (such as usingFirstHostWithRole) so that your stories are as
108
     * test-environment-independent as possible.
109
     *
110
     * @param  string $hostId
111
     *         the ID of the host to use
112
     * @return ExpectsHost
113
     */
114
    public static function expectsHost($hostId)
115
    {
116
        return new ExpectsHost(StoryTeller::instance(), [$hostId]);
117
    }
118
119
    /**
120
     * returns the ExpectsHostsTable module
121
     *
122
     * This module provides access to Storyplayer's internal list of computers
123
     * that are running in your test environment.
124
     *
125
     * This module is intended for internal use by Storyplayer. You should not
126
     * need to call this module from your own stories.
127
     *
128
     * @return ExpectsHostsTable
129
     */
130
    public static function expectsHostsTable()
131
    {
132
        return new ExpectsHostsTable(StoryTeller::instance());
133
    }
134
135
    /**
136
     * iterates over each host in your test environment that has been assigned
137
     * the given role
138
     *
139
     * This helper gives you a way to run any host-aware module across every
140
     * computer in your test environment that has the role you ask for.
141
     *
142
     * For example, if you want to get a table of IP addresses, you can do this:
143
     *
144
     *     $ipAddresses = foreachHostWithRole('web-server')->fromHost()->getIpAddress();
145
     *
146
     * or, if you wanted to reboot machines, you could do this:
147
     *
148
     *     foreachHostWithRole('web-server')->usingHost()->restartHost();
149
     *
150
     * This iterator will automatically provide the $hostId parameter to the module
151
     * you tell it to call, and will collate all return values into an array.
152
     *
153
     * Alternatively, you can use a normal PHP foreach() loop:
154
     *
155
     *     foreach(hostWithRole('web-server') as $hostId) {
156
     *         usingHost($hostId)->restartHost();
157
     *     }
158
     *
159
     * @param  string $roleName
160
     *         the role that you want to work with
161
     * @return ForeachHostWithRole
162
     */
163
    public static function foreachHostWithRole($roleName)
164
    {
165
        return new ForeachHostWithRole(StoryTeller::instance(), [$roleName]);
166
    }
167
168
    /**
169
     * returns the FromHost module, with its hostId already set to the first
170
     * host in your test environment config file that has the given role.
171
     *
172
     * This module provides support for making your tests work on multiple
173
     * test environments. Instead of hard-coding hostIds into your stories,
174
     * use this module to find a host by its assigned role. That way, it doesn't
175
     * matter how many hosts are in different environments, or if their hostIds
176
     * are different.
177
     *
178
     * This module is normally used for testing read requests via APIs. You
179
     * should be able to write once, and then read from all hosts to prove
180
     * that the data was correctly written (and that there are no caching errors).
181
     *
182
     * To read from all hosts, you would use:
183
     *
184
     *     foreach(hostWithRole($roleName) as $hostId) { ... }
185
     *
186
     * @param  string $roleName
187
     *         the assigned role you're looking for
188
     * @return FromFirstHostWithRole
189
     */
190
    public static function fromFirstHostWithRole($roleName)
191
    {
192
        return new FromFirstHostWithRole(StoryTeller::instance(), [$roleName]);
193
    }
194
195
    /**
196
     * returns the FromHost module
197
     *
198
     * This module provides support for running commands on a computer in your
199
     * test environment - basically for doing anything that's likely to change
200
     * the state of that computer.
201
     *
202
     * In SPv1, it was common to call this module directly from your own stories.
203
     * In SPv2, you're much more likely to use one of our multi-host modules or
204
     * helpers (such as usingFirstHostWithRole) so that your stories are as
205
     * test-environment-independent as possible.
206
     *
207
     * @param  string $hostId
208
     *         the ID of the host to use
209
     * @return FromHost
210
     */
211
    public static function fromHost($hostId)
212
    {
213
        return new FromHost(StoryTeller::instance(), [$hostId]);
214
    }
215
216
    /**
217
     * returns the FromHostsTable module
218
     *
219
     * This module provides access to Storyplayer's internal list of computers
220
     * that are running in your test environment.
221
     *
222
     * This module is intended for internal use by Storyplayer. You should not
223
     * need to call this module from your own stories.
224
     *
225
     * @return FromHostsTable
226
     */
227
    public static function fromHostsTable()
228
    {
229
        return new FromHostsTable(StoryTeller::instance());
230
    }
231
232
    /**
233
     * get all host IDs that match the given role
234
     *
235
     * @param  string $roleName
236
     *         The role that we want
237
     *
238
     * @return Iterator
0 ignored issues
show
Documentation introduced by
Should the return type not be \Generator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
239
     *         a hostId that matches the role
240
     */
241
    public static function getHostsWithRole($roleName)
242
    {
243
        // special case
244
        if ($roleName instanceof StoryTeller) {
245
            throw Exceptions::newActionFailedException(__METHOD__, "first param to hostWithRole() is no longer \$st");
246
        }
247
248
        $listOfHosts = fromRolesTable()->getDetailsForRole($roleName);
249
        if (!count($listOfHosts)) {
250
            throw Exceptions::newActionFailedException(__METHOD__, "unknown role '{$roleName}' or no hosts for that role");
251
        }
252
253
        // what are we doing?
254
        $log = usingLog()->startAction("for each host with role '{$roleName}' ... ");
255
256
        foreach ($listOfHosts as $hostId) {
257
            yield($hostId);
258
        }
259
260
        // all done
261
        $log->endAction();
262
    }
263
264
    /**
265
     * returns the UsingHost module, with its hostId already set to the first
266
     * host in your test environment config file that has the given role.
267
     *
268
     * This module provides support for making your tests work on multiple
269
     * test environments. Instead of hard-coding hostIds into your stories,
270
     * use this module to find a host by its assigned role. That way, it doesn't
271
     * matter how many hosts are in different environments, or if their hostIds
272
     * are different.
273
     *
274
     * This module is normally used for testing write requests via APIs. You
275
     * should be able to write once, and then read from all hosts to prove
276
     * that the data was correctly written (and that there are no caching errors).
277
     *
278
     * To read from all hosts, you would use:
279
     *
280
     *     foreach(hostWithRole($roleName) as $hostId) { ... }
281
     *
282
     * @param  string $roleName
283
     *         the assigned role you're looking for
284
     * @return UsingHost
285
     */
286
    public static function usingFirstHostWithRole($roleName)
287
    {
288
        return new UsingFirstHostWithRole(StoryTeller::instance(), [$roleName]);
289
    }
290
291
    /**
292
     * returns the UsingHost module
293
     *
294
     * This module provides support for running commands on a computer in your
295
     * test environment - basically for doing anything that's likely to change
296
     * the state of that computer.
297
     *
298
     * In SPv1, it was common to call this module directly from your own stories.
299
     * In SPv2, you're much more likely to use one of our multi-host modules or
300
     * helpers (such as usingFirstHostWithRole) so that your stories are as
301
     * test-environment-independent as possible.
302
     *
303
     * @param  string $hostId
304
     *         the ID of the host to use
305
     * @return UsingHost
306
     */
307
    public static function usingHost($hostId)
308
    {
309
        return new UsingHost(StoryTeller::instance(), [$hostId]);
310
    }
311
312
    /**
313
     * returns the UsingHostsTable module
314
     *
315
     * This module provides access to Storyplayer's internal list of computers
316
     * that are running in your test environment.
317
     *
318
     * This module is intended for internal use by Storyplayer. You should not
319
     * need to call this module from your own stories.
320
     *
321
     * @return UsingHostsTable
322
     */
323
    public static function usingHostsTable()
324
    {
325
        return new UsingHostsTable(StoryTeller::instance());
326
    }
327
}
328