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.

RegisterServerService::register()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 26
rs 8.8571
cc 1
eloc 13
nc 1
nop 2
1
<?php
2
namespace SeleniumSetup\Service;
3
4
5
use SeleniumSetup\Config\ConfigFactory;
6
use SeleniumSetup\Locker\ServerItemFactory;
7
8
class RegisterServerService extends AbstractService
9
{
10
    const LOG_TYPE_INFO = 'info';
11
    const LOG_TYPE_ERROR = 'error';
12
13
    const VALID_INSTANCE = 1;
14
    const INVALID_NAME = 3;
15
    const INVALID_PORT = 5;
16
    // const INVALID_FILENAME = 7; @todo check filename pattern and sanitize or display error
17
18
    const SUCCESS_MESSAGE_ADDED = 'Instance %s was added.';
19
    const ERROR_MESSAGE_NAME = 'Name %s is reserved for another instance.';
20
    const ERROR_MESSAGE_PORT = 'Port %d is reserved for another instance.';
21
    // const ERROR_MESSAGE_FILENAME = 'The name %s is not a valid filename.';
22
23
    public function test()
24
    {
25
        return $this->env->test();
26
    }
27
28
    public function handle()
29
    {
30
        $instanceName = $this->input->getArgument('name');
31
        $instancePort = $this->input->getArgument('port');
32
33
        $this->locker->openLockFile();
34
        $status = $this->validate($instanceName, $instancePort);
35
36
        $this->logStatus($status, $instanceName, $instancePort);
37
38
        if ($status === self::VALID_INSTANCE) {
39
            $this->register($instanceName, $instancePort);
40
            $this->log(self::SUCCESS_MESSAGE_ADDED, $instanceName);
41
        }
42
    }
43
44
    protected function register($name, $port)
45
    {
46
        // Find filename by instance name.
47
        $filename = $this->getInstanceConfigFilename($name);
48
49
        // Update configuration to new instance clone.
50
        $this->config->setName($name);
51
        $this->config->setPort($port);
52
        $this->config->setFilePath($filename);
53
54
        // Store new filename using the default instance template.
55
        $this->fileSystem->createFile($filename, $this->config->toJson());
56
57
        // Append instance entry in lock-file.
58
        $this->locker->addServer(
59
            ServerItemFactory::createFromProperties(
60
                $name,
61
                0,
62
                $port,
63
                $filename
64
            )
65
        );
66
67
        // Write lock-file settings.
68
        $this->locker->writeToLockFile();
69
    }
70
71
    protected function getInstanceConfigFilename($name)
72
    {
73
        return dirname($this->config->getFilePath()) . DIRECTORY_SEPARATOR . $name . '.json';
74
    }
75
76
    /**
77
     * - initialize OK status.
78
     *
79
     * - for each registered instances
80
     *     - if equal name as the new instance, append (multiply) the value of INVALID_NAME
81
     *     - if equal port as the new instance, append (multiply) the value of INVALID_PORT
82
     *
83
     * - return status final value
84
     *
85
     * @return int
86
     */
87
    protected function validate($testName, $testPort)
88
    {
89
        $status = self::VALID_INSTANCE;
90
91
        $instances = $this->locker->getServers();
92
93
        foreach ($instances as $instance) {
94
            if ($instance->getName() == $testName) {
95
                $status *= self::INVALID_NAME;
96
            }
97
98
            if ($instance->getPort() == $testPort) {
99
                $status *= self::INVALID_PORT;
100
            }
101
        }
102
103
        return $status;
104
    }
105
106
    protected function logStatus($status, $name, $port)
107
    {
108
        if ($this->isStatusContaining($status, self::INVALID_NAME)) {
109
            $this->log(self::ERROR_MESSAGE_NAME, $name, self::LOG_TYPE_ERROR);
110
        }
111
112
        if ($this->isStatusContaining($status, self::INVALID_PORT)) {
113
            $this->log(self::ERROR_MESSAGE_PORT, $port, self::LOG_TYPE_ERROR);
114
        }
115
    }
116
117
    protected function isStatusContaining($status, $checkedStatus)
118
    {
119
        return $status % $checkedStatus === 0;
120
    }
121
122
    protected function log($message, $value = null, $type = self::LOG_TYPE_INFO) {
123
        $this->output->writeln(sprintf('<' . $type . '>' . $message . '</' . $type . '>', $value));
124
    }
125
}