ServiceProvider::register()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 1
nop 1
1
<?php
2
3
namespace Eole\Sandstone\Push\Bridge\ZMQ;
4
5
use ZMQ;
6
use ZMQContext;
7
use Pimple\ServiceProviderInterface;
8
use Pimple\Container;
9
use Eole\Sandstone\Push\Debug\TraceablePushServer;
10
11
class ServiceProvider implements ServiceProviderInterface
12
{
13
    /**
14
     * {@InheritDoc}
15
     */
16
    public function register(Container $app)
17
    {
18
        $app['sandstone.push'] = function () use ($app) {
19
            $pushServerHost = $app['sandstone.push.server']['host'];
20
            $pushServerPort = $app['sandstone.push.server']['port'];
21
22
            $context = new ZMQContext();
23
            $socket = $context->getSocket(ZMQ::SOCKET_PUSH);
24
            $socket->connect("tcp://$pushServerHost:$pushServerPort");
25
26
            $pushServer = new ZMQPushServer($socket);
0 ignored issues
show
Bug introduced by
It seems like $socket defined by $context->getSocket(\ZMQ::SOCKET_PUSH) on line 23 can be null; however, Eole\Sandstone\Push\Brid...shServer::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
27
28
            if ($app['debug']) {
29
                $pushServer = new TraceablePushServer($pushServer);
30
            }
31
32
            return $pushServer;
33
        };
34
    }
35
}
36