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.

FromHost   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 465
Duplicated Lines 16.34 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 38
c 1
b 0
f 0
lcom 1
cbo 13
dl 76
loc 465
rs 8.3999

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getDetails() 0 19 3
A getHostIsRunning() 0 23 2
A getHostname() 0 17 2
A getIpAddress() 0 12 1
A getInstalledPackageDetails() 0 18 1
A getPidIsRunning() 23 23 2
A getProcessIsRunning() 23 23 2
A getPid() 0 18 1
A getSshUsername() 15 15 1
A getSshKeyFile() 15 15 1
A getIsScreenRunning() 0 4 1
A getScreenIsRunning() 0 5 1
A getScreenSessionDetails() 0 5 1
A getAllScreenSessions() 0 5 1
B getAppSettings() 0 25 2
B getAppSetting() 0 37 5
B getLegacyAppSetting() 0 33 4
B getStorySetting() 0 28 3
A downloadFile() 0 5 1
A getFileDetails() 0 5 1
A getLocalFolder() 0 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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/Modules/Host
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\SPv3\Modules\Host;
45
46
use DataSift\Storyplayer\HostLib;
47
use DataSift\Storyplayer\OsLib;
48
49
use DataSift\Stone\DataLib\DataPrinter;
50
use DataSift\Stone\ObjectLib\BaseObject;
51
52
use GanbaroDigital\TextTools\Filters\FilterColumns;
53
use GanbaroDigital\TextTools\Filters\FilterForMatchingRegex;
54
use GanbaroDigital\TextTools\Filters\FilterForMatchingString;
55
56
use Storyplayer\SPv3\Modules\Exceptions;
57
use Storyplayer\SPv3\Modules\Filesystem;
58
use Storyplayer\SPv3\Modules\Log;
59
use Storyplayer\SPv3\Modules\Screen;
60
use StoryplayerInternals\SPv3\Modules\Deprecated;
61
use StoryplayerInternals\SPv3\Helpers\ManualUrls;
62
63
/**
64
 * get information about a given host
65
 *
66
 * @category  Libraries
67
 * @package   Storyplayer/Modules/Host
68
 * @author    Stuart Herbert <[email protected]>
69
 * @copyright 2011-present Mediasift Ltd www.datasift.com
70
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
71
 * @link      http://datasift.github.io/storyplayer
72
 */
73
class FromHost extends HostAwareModule
74
{
75
    /**
76
     * @return object
77
     */
78
    public function getDetails()
79
    {
80
        // what are we doing?
81
        $log = Log::usingLog()->startAction("retrieve details for host '{$this->args[0]}'");
82
83
        // get the host details
84
        $hostDetails = $this->getHostDetails();
85
86
        // we already have details - are they valid?
87
        if (isset($hostDetails->invalidHost) && $hostDetails->invalidHost) {
88
            $msg = "there are no details about host '{$hostDetails->hostId}'";
89
            $log->endAction($msg);
90
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
91
        }
92
93
        // return the details
94
        $log->endAction();
95
        return $hostDetails;
96
    }
97
98
    /**
99
     * is a host up and running?
100
     *
101
     * @return boolean
102
     */
103
    public function getHostIsRunning()
104
    {
105
        // what are we doing?
106
        $log = Log::usingLog()->startAction("is host '{$this->args[0]}' running?");
107
108
        // make sure we have valid host details
109
        $hostDetails = $this->getHostDetails();
110
111
        // get an object to talk to this host
112
        $host = HostLib::getHostAdapter($this->st, $hostDetails->type);
113
114
        // if the box is running, it should have a status of 'running'
115
        $result = $host->isRunning($hostDetails);
116
117
        if (!$result) {
118
            $log->endAction("host is not running");
119
            return false;
120
        }
121
122
        // all done
123
        $log->endAction("host is running");
124
        return true;
125
    }
126
127
    /**
128
     * get the hostname for a host
129
     *
130
     * the returned hostname is suitable for use in HTTP/HTTPS URLs
131
     *
132
     * if we have been unable to determine the hostname for the host,
133
     * this will return the host's IP address instead
134
     *
135
     * @return string
136
     */
137
    public function getHostname()
138
    {
139
        // what are we doing?
140
        $log = Log::usingLog()->startAction("get the hostname of host ID '{$this->args[0]}'");
141
142
        // make sure we have valid host details
143
        $hostDetails = $this->getHostDetails();
144
145
        // do we have a hostname?
146
        if (!isset($hostDetails->hostname)) {
147
            throw Exceptions::newActionFailedException(__METHOD__, "no hostname found for host ID '{$this->args[0]}'");
148
        }
149
150
        // all done
151
        $log->endAction("hostname is '{$hostDetails->hostname}'");
152
        return $hostDetails->hostname;
153
    }
154
155
    /**
156
     * get the IP address for a host
157
     *
158
     * @return string
159
     */
160
    public function getIpAddress()
161
    {
162
        // what are we doing?
163
        $log = Log::usingLog()->startAction("get IP address of host '{$this->args[0]}'");
164
165
        // make sure we have valid host details
166
        $hostDetails = $this->getHostDetails();
167
168
        // all done
169
        $log->endAction("IP address is '{$hostDetails->ipAddress}'");
170
        return $hostDetails->ipAddress;
171
    }
172
173
    /**
174
     * @param  string $packageName
175
     * @return object
176
     */
177
    public function getInstalledPackageDetails($packageName)
178
    {
179
        // what are we doing?
180
        $log = Log::usingLog()->startAction("get details for package '{$packageName}' installed on host '{$this->args[0]}'");
181
182
        // make sure we have valid host details
183
        $hostDetails = $this->getHostDetails();
184
185
        // get an object to talk to this host
186
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
187
188
        // get the information
189
        $return = $host->getInstalledPackageDetails($hostDetails, $packageName);
190
191
        // all done
192
        $log->endAction();
193
        return $return;
194
    }
195
196
    /**
197
     * @param  int $pid
198
     * @return bool
199
     */
200 View Code Duplication
    public function getPidIsRunning($pid)
201
    {
202
        // what are we doing?
203
        $log = Log::usingLog()->startAction("is process PID '{$pid}' running on host '{$this->args[0]}'?");
204
205
        // make sure we have valid host details
206
        $hostDetails = $this->getHostDetails();
207
208
        // get an object to talk to this host
209
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
210
211
        // get the information
212
        $return = $host->getPidIsRunning($hostDetails, $pid);
213
214
        // did it work?
215
        if ($return) {
216
            $log->endAction("'{$pid}' is running");
217
            return true;
218
        }
219
220
        $log->endAction("'{$pid}' is not running");
221
        return false;
222
    }
223
224
    /**
225
     * @param  string $processName
226
     * @return bool
227
     */
228 View Code Duplication
    public function getProcessIsRunning($processName)
229
    {
230
        // what are we doing?
231
        $log = Log::usingLog()->startAction("is process '{$processName}' running on host '{$this->args[0]}'?");
232
233
        // make sure we have valid host details
234
        $hostDetails = $this->getHostDetails();
235
236
        // get an object to talk to this host
237
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
238
239
        // get the information
240
        $return = $host->getProcessIsRunning($hostDetails, $processName);
241
242
        // did it work?
243
        if ($return) {
244
            $log->endAction("'{$processName}' is running");
245
            return true;
246
        }
247
248
        $log->endAction("'{$processName}' is not running");
249
        return false;
250
    }
251
252
    /**
253
     * @param  string $processName
254
     * @return int
255
     */
256
    public function getPid($processName)
257
    {
258
        // log some info to the user
259
        $log = Log::usingLog()->startAction("get id of process '{$processName}' running on VM '{$this->args[0]}'");
260
261
        // make sure we have valid host details
262
        $hostDetails = $this->getHostDetails();
263
264
        // get an object to talk to this host
265
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
266
267
        // get the information
268
        $return = (int)$host->getPid($hostDetails, $processName);
269
270
        // success
271
        $log->endAction("pid is '{$return}'");
272
        return $return;
273
    }
274
275
    /**
276
     * @return string
277
     */
278 View Code Duplication
    public function getSshUsername()
279
    {
280
        // what are we doing?
281
        $log = Log::usingLog()->startAction("get username to use with SSH to host '{$this->args[0]}'");
282
283
        // make sure we have valid host details
284
        $hostDetails = $this->getHostDetails();
285
286
        // get the information
287
        $return = $hostDetails->sshUsername;
288
289
        // all done
290
        $log->endAction("username is '{$return}'");
291
        return $return;
292
    }
293
294
    /**
295
     * @return string
296
     */
297 View Code Duplication
    public function getSshKeyFile()
298
    {
299
        // what are we doing?
300
        $log = Log::usingLog()->startAction("get key file to use with SSH to host '{$this->args[0]}'");
301
302
        // make sure we have valid host details
303
        $hostDetails = $this->getHostDetails();
304
305
        // get the information
306
        $return = $hostDetails->sshKeyFile;
307
308
        // all done
309
        $log->endAction("key file is '{$return}'");
310
        return $return;
311
    }
312
313
    /**
314
     * @param  string $sessionName
315
     * @return bool
316
     */
317
    public function getIsScreenRunning($sessionName)
318
    {
319
        return $this->getScreenIsRunning($sessionName);
0 ignored issues
show
Deprecated Code introduced by
The method Storyplayer\SPv3\Modules...t::getScreenIsRunning() has been deprecated with message: since v2.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
320
    }
321
322
    /**
323
     * @deprecated since v2.4.0
324
     */
325
    public function getScreenIsRunning($sessionName)
326
    {
327
        Deprecated::fireDeprecated(__METHOD__, "v2.4.0", ManualUrls::HOST_MODULE_BREAKUP);
328
        return Screen::fromHost($this->args[0])->getScreenIsRunning($sessionName);
329
    }
330
331
    /**
332
     * @deprecated since v2.4.0
333
     */
334
    public function getScreenSessionDetails($sessionName)
335
    {
336
        Deprecated::fireDeprecated(__METHOD__, "v2.4.0", ManualUrls::HOST_MODULE_BREAKUP);
337
        return Screen::fromHost($this->args[0])->getScreenSessionDetails($sessionName);
338
    }
339
340
    /**
341
     * @deprecated since v2.4.0
342
     */
343
    public function getAllScreenSessions()
344
    {
345
        Deprecated::fireDeprecated(__METHOD__, "v2.4.0", ManualUrls::HOST_MODULE_BREAKUP);
346
        return Screen::fromHost($this->args[0])->getAllScreenSessions();
347
    }
348
349
    /**
350
     * @param  string $appName
351
     * @return mixed
352
     */
353
    public function getAppSettings($appName)
354
    {
355
        // what are we doing?
356
        $log = Log::usingLog()->startAction("get settings for '{$appName}' from host '{$this->args[0]}'");
357
358
        // make sure we have valid host details
359
        $hostDetails = $this->getHostDetails();
360
361
        // do we have any app settings?
362
        if (!isset($hostDetails->appSettings, $hostDetails->appSettings->$appName)) {
363
            $log->endAction("setting does not exist :(");
364
            throw Exceptions::newActionFailedException(__METHOD__);
365
        }
366
367
        // yes we do
368
        $value = $hostDetails->appSettings->$appName;
369
370
        // log the settings
371
        $printer  = new DataPrinter();
372
        $logValue = $printer->convertToString($value);
373
        $log->endAction("settings for '{$appName}' are '{$logValue}'");
374
375
        // all done
376
        return $value;
377
    }
378
379
    /**
380
     * @param  string $path
381
     * @param  string|null $settingName
382
     * @return mixed
383
     */
384
    public function getAppSetting($path, $settingName = null)
385
    {
386
        // are we operating in legacy mode (for DataSift), or are we using
387
        // the new dot.notation.support that we want everywhere in v2?
388
        $parts = explode(".", $path);
389
        if (count($parts) === 1 && $settingName !== null) {
390
            return $this->getLegacyAppSetting($path, $settingName);
391
        }
392
393
        // if we get here, then we are using the new dot.notation.support
394
395
        // what are we doing?
396
        $log = Log::usingLog()->startAction("get appSetting '{$path}' from host '{$this->args[0]}'");
397
398
        // make sure we have valid host details
399
        $hostDetails = $this->getHostDetails();
400
401
        // do we have any app settings?
402
        if (!isset($hostDetails->appSettings)) {
403
            $msg = "host has no appSettings at all";
404
            $log->endAction($msg);
405
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
406
        }
407
408
        // do we have the setting that we want?
409
        if (!$hostDetails->appSettings->hasData($path)) {
410
            $msg = "host does not have appSetting '{$path}'";
411
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
412
        }
413
414
        // success
415
        $data = $hostDetails->appSettings->getData($path);
416
417
        // all done
418
        $log->endAction([ "setting is", $data ]);
419
        return $data;
420
    }
421
422
    /**
423
     * @param  string $appName
424
     * @param  string $settingName
425
     * @return mixed
426
     */
427
    protected function getLegacyAppSetting($appName, $settingName)
428
    {
429
        // what are we doing?
430
        $log = Log::usingLog()->startAction("get $settingName for '{$appName}' from host '{$this->args[0]}'");
431
432
        // make sure we have valid host details
433
        $hostDetails = $this->getHostDetails();
434
435
        // do we have any app settings?
436
        if (!isset($hostDetails->appSettings)) {
437
            $log->endAction("host has no appSettings at all");
438
            throw Exceptions::newActionFailedException(__METHOD__);
439
        }
440
        if (!isset($hostDetails->appSettings->$appName)) {
441
            $log->endAction("host has no appSettings for {$appName}");
442
            throw Exceptions::newActionFailedException(__METHOD__);
443
        }
444
        if (!isset($hostDetails->appSettings->$appName->$settingName)) {
445
            $log->endAction("host has no appSetting '{$settingName}' for {$appName}");
446
            throw Exceptions::newActionFailedException(__METHOD__);
447
        }
448
449
        // yes we do
450
        $value = $hostDetails->appSettings->$appName->$settingName;
451
452
        // log the settings
453
        $printer  = new DataPrinter();
454
        $logValue = $printer->convertToString($value);
455
        $log->endAction("setting for '{$appName}' is '{$logValue}'");
456
457
        // all done
458
        return $value;
459
    }
460
461
    /**
462
     * @param  string $path
463
     * @return mixed
464
     */
465
    public function getStorySetting($path)
466
    {
467
        // what are we doing?
468
        $log = Log::usingLog()->startAction("get storySetting '{$path}' from host '{$this->args[0]}'");
469
470
        // make sure we have valid host details
471
        $hostDetails = $this->getHostDetails();
472
473
        // do we have any app settings?
474
        if (!isset($hostDetails->storySettings)) {
475
            $msg = "host has no storySettings at all";
476
            $log->endAction($msg);
477
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
478
        }
479
480
        // do we have the setting that we want?
481
        if (!$hostDetails->storySettings->hasData($path)) {
482
            $msg = "host does not have storySetting '{$path}'";
483
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
484
        }
485
486
        // success
487
        $data = $hostDetails->storySettings->getData($path);
488
489
        // all done
490
        $log->endAction([ "setting is", $data ]);
491
        return $data;
492
    }
493
494
    /**
495
     * @deprecated since v2.4.0
496
     */
497
    public function downloadFile($sourceFilename, $destFilename)
498
    {
499
        Deprecated::fireDeprecated(__METHOD__, "v2.4.0", ManualUrls::HOST_MODULE_BREAKUP);
500
        return Filesystem::fromHost($this->args[0])->downloadFile($sourceFilename, $destFilename);
501
    }
502
503
    /**
504
     * @deprecated since v2.4.0
505
     */
506
    public function getFileDetails($filename)
507
    {
508
        Deprecated::fireDeprecated(__METHOD__, "v2.4.0", ManualUrls::HOST_MODULE_BREAKUP);
509
        return Filesystem::fromHost($this->args[0])->getFileDetails($filename);
510
    }
511
512
    /**
513
     * which local folder do we need to be in when working with this host?
514
     *
515
     * @return string
516
     */
517
    public function getLocalFolder()
518
    {
519
        // what are we doing?
520
        $log = Log::usingLog()->startAction("get local folder for host '{$this->args[0]}'");
521
522
        // make sure we have valid host details
523
        $hostDetails = $this->getHostDetails();
524
525
        // does it have a folder defined?
526
        if (isset($hostDetails->dir)) {
527
            $retval = $hostDetails->dir;
528
        }
529
        else {
530
            $retval = getcwd();
531
        }
532
533
        // all done
534
        $log->endAction($retval);
535
        return $retval;
536
    }
537
}
538