Completed
Push — master ( 26e625...88c602 )
by Timo
02:58
created

Router::connect()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 9
nc 4
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 69.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 * This file is part of the Tidal/WampWatch package.
5
 *   (c) 2016 Timo Michna <timomichna/yahoo.de>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 *
10
 */
11
12
namespace Tidal\WampWatch\Model;
13
14
use Guzzle\Common\Exception\RuntimeException;
15
use Tidal\WampWatch\Model\Property\Collection\HasRealmsTrait;
16
use Tidal\WampWatch\Model\Property\Scalar\HasUriTrait;
17
use Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait;
18
use Tidal\WampWatch\Model\Property\ObjectCollection;
19
use Exception;
20
21
class Router implements Contract\RouterInterface
22
{
23
    use HasCollectionsTrait;
24
    use HasRealmsTrait;
25
    use HasUriTrait;
26
27
    const DEFAULT_CONNECTION_CLS = '\Tidal\WampWatch\Model\Connection';
28
29
    private static $connectionFactory;
30
31
    public function __construct($uri)
32
    {
33
        $this->setUri((string) $uri);
34
        $this->setRealms(new ObjectCollection());
35
    }
36
37
    /**
38
     * @param callable $factory
39
     */
40
    public static function setConnectionFactory(callable $factory)
41
    {
42
        self::$connectionFactory = $factory;
43
    }
44
45
    /**
46
     * @param \Tidal\WampWatch\Model\Contract\RealmInterface $realm
47
     *
48
     * @return mixed
49
     *
50
     * @throws \Exception
51
     */
52
    public function connect(Contract\RealmInterface $realm)
53
    {
54
        try {
55
            $factory = self::$connectionFactory;
56
            /** @var Contract\ConnectionInterface $connection */
57
            $connection = $factory($this, $realm);
58
            if (!is_a($connection, Contract\ConnectionInterface::class)) {
59
                throw new RuntimeException('Callable registered as factory for Connection did not return Connection instance');
60
            }
61
        } catch (Exception $e) {
62
            throw $e;
63
        }
64
65
        return $connection;
66
    }
67
}
68
69
Router::setConnectionFactory(function (Contract\RouterInterface $router, Contract\RealmInterface $realm) {
70
    return new Connection($router, $realm);
71
});
72