Completed
Push — master ( b5fdaf...96e878 )
by Christopher
02:15
created

DoormanConnectorHandler::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
ccs 0
cts 41
cp 0
rs 9.411
cc 3
eloc 28
nc 4
nop 1
crap 12

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\ZeroMqClient;
8
use AsyncPHP\Remit\Location\InMemoryLocation;
9
use AsyncPHP\Remit\Server\ZeroMqServer;
10
use Aura\Sql\ExtendedPdo;
11
use Icicle\Loop;
12
use PDO;
13
14
final class DoormanConnectorHandler implements Handler
15
{
16
    /**
17
     * @inheritdoc
18
     *
19
     * @param Task $task
20
     */
21
    public function handle(Task $task)
22
    {
23
        $config = $task->getData();
24
25
        if ($config["driver"] === "mysql") {
26
            $config += [
27
                "host" => "127.0.0.1",
28
                "port" => 3306,
29
                "charset" => "utf8",
30
                "socket" => null,
31
            ];
32
        }
33
34
        if ($config["remit"]["driver"] === "zeromq") {
35
            $config["remit"]["server"] += [
36
                "host" => "127.0.0.1",
37
            ];
38
39
            $config["remit"]["client"] += [
40
                "host" => "127.0.0.1",
41
            ];
42
43
            $server = new ZeroMqServer(
44
                new InMemoryLocation(
45
                    $config["remit"]["client"]["host"],
46
                    $config["remit"]["client"]["port"]
47
                )
48
            );
49
50
            $client = new ZeroMqClient(
51
                new InMemoryLocation(
52
                    $config["remit"]["server"]["host"],
53
                    $config["remit"]["server"]["port"]
54
                )
55
            );
56
        }
57
58
        $connection = new ExtendedPdo(
59
            new PDO($this->newConnectionString($config), $config["username"], $config["password"])
60
        );
61
62
        $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...
63
            $client->emit("r", [$connection->fetchAll($query, $values), $id]);
64
        });
65
66
        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...
67
            $server->tick();
68
        });
69
70
        Loop\run();
71
    }
72
73
    /**
74
     * Returns a new dsn string, for PDO to connect to the database with.
75
     *
76
     * @param array $config
77
     *
78
     * @return string
79
     */
80
    private function newConnectionString(array $config)
81
    {
82
        if ($config["driver"] === "mysql") {
83
            return "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']};unix_socket={$config['socket']};charset={$config['charset']}";
84
        }
85
86
        if ($config["driver"] === "pgsql") {
87
            return "pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
88
        }
89
90
        if ($config["driver"] === "sqlite") {
91
            return "sqlite:{$config['file']}";
92
        }
93
94
        if ($config["driver"] === "sqlsrv") {
95
            return "sqlsrv:Server={$config['host']},{$config['port']};Database={$config['database']}";
96
        }
97
    }
98
}
99