Completed
Push — master ( 429f1a...51eb75 )
by Christopher
02:26
created

DoormanConnectorHandler::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 66
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 66
ccs 0
cts 50
cp 0
rs 8.8291
cc 4
eloc 36
nc 4
nop 1
crap 20

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AsyncPHP\Icicle\Database\Connector;
4
5
use AsyncPHP\Doorman\Handler;
6
use AsyncPHP\Doorman\Task;
7
use AsyncPHP\Remit\Client;
8
use AsyncPHP\Remit\Client\ZeroMqClient;
9
use AsyncPHP\Remit\Location\InMemoryLocation;
10
use AsyncPHP\Remit\Server;
11
use AsyncPHP\Remit\Server\ZeroMqServer;
12
use Aura\Sql\ExtendedPdo;
13
use Icicle\Loop;
14
use PDO;
15
16
final class DoormanConnectorHandler implements Handler
17
{
18
    /**
19
     * @inheritdoc
20
     *
21
     * @param Task $task
22
     */
23
    public function handle(Task $task)
24
    {
25
        $config = $task->getData();
26
27
        if ($config["driver"] === "mysql") {
28
            $config += [
29
                "host" => "127.0.0.1",
30
                "port" => 3306,
31
                "charset" => "utf8",
32
                "socket" => null,
33
            ];
34
        }
35
36
        if ($config["remit"]["driver"] === "zeromq") {
37
            $config["remit"]["server"] += [
38
                "host" => "127.0.0.1",
39
            ];
40
41
            $config["remit"]["client"] += [
42
                "host" => "127.0.0.1",
43
            ];
44
45
            $server = new ZeroMqServer(
46
                new InMemoryLocation(
47
                    $config["remit"]["client"]["host"],
48
                    $config["remit"]["client"]["port"]
49
                )
50
            );
51
52
            $client = new ZeroMqClient(
53
                new InMemoryLocation(
54
                    $config["remit"]["server"]["host"],
55
                    $config["remit"]["server"]["port"]
56
                )
57
            );
58
        }
59
60
        $connection = new ExtendedPdo(
61
            new PDO($this->newConnectionString($config), $config["username"], $config["password"])
62
        );
63
64
        $server->addListener("q", function ($query, $values, $id) use ($client, $connection) {
0 ignored issues
show
Bug introduced by
The variable $server does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $client does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
65
            $client->emit("r", [$connection->fetchAll($query, $values), $id]);
66
        });
67
68
        $server->addListener("d", function () use ($connection, $server, $client) {
0 ignored issues
show
Bug introduced by
The variable $server does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $client does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
69
            $client->emit("dd");
70
71
            try {
72
                $connection->disconnect();
73
            } catch (Exception $exception) {
0 ignored issues
show
Bug introduced by
The class AsyncPHP\Icicle\Database\Connector\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
74
                // TODO: find an elegant way to deal with this
75
            }
76
77
            $server->disconnect();
78
            $client->disconnect();
79
80
            Loop\stop();
81
        });
82
83
        Loop\periodic(0, function () use ($server) {
0 ignored issues
show
Bug introduced by
The variable $server does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
84
            $server->tick();
85
        });
86
87
        Loop\run();
88
    }
89
90
    /**
91
     * Returns a new dsn string, for PDO to connect to the database with.
92
     *
93
     * @param array $config
94
     *
95
     * @return string
96
     */
97
    private function newConnectionString(array $config)
98
    {
99
        if ($config["driver"] === "mysql") {
100
            return "mysql:host={$config['host']};port={$config['port']};dbname={$config['schema']};unix_socket={$config['socket']};charset={$config['charset']}";
101
        }
102
103
        if ($config["driver"] === "pgsql") {
104
            return "pgsql:host={$config['host']};port={$config['port']};dbname={$config['schema']}";
105
        }
106
107
        if ($config["driver"] === "sqlite") {
108
            return "sqlite:{$config['file']}";
109
        }
110
111
        if ($config["driver"] === "sqlsrv") {
112
            return "sqlsrv:Server={$config['host']},{$config['port']};Database={$config['schema']}";
113
        }
114
    }
115
}
116