TopicRouter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadTopic() 0 21 3
1
<?php
2
3
namespace Eole\Sandstone\Websocket\Routing;
4
5
use LogicException;
6
use Symfony\Component\Routing\Matcher\UrlMatcher;
7
use Eole\Sandstone\Websocket\Topic;
8
9
class TopicRouter
10
{
11
    /**
12
     * @var UrlMatcher
13
     */
14
    private $urlMatcher;
15
16
    /**
17
     * @param UrlMatcher $urlMatcher
18
     */
19
    public function __construct(UrlMatcher $urlMatcher)
20
    {
21
        $this->urlMatcher = $urlMatcher;
22
    }
23
24
    /**
25
     * @param string $topicPath
26
     *
27
     * @return Topic
28
     *
29
     * @throws LogicException when cannot load topic.
30
     */
31
    public function loadTopic($topicPath)
32
    {
33
        $arguments = $this->urlMatcher->match('/'.$topicPath);
34
        $topicFactory = $arguments['_topic_factory'];
35
36
        if (!is_callable($topicFactory)) {
37
            throw new LogicException("Topic $topicPath is not a callback.");
38
        }
39
40
        $topicInstance = $topicFactory($topicPath, $arguments);
41
42
        if (!$topicInstance instanceof Topic) {
43
            throw new LogicException(sprintf(
44
                'Topic callback for %s must return an instance of %s.',
45
                $topicPath,
46
                Topic::class
47
            ));
48
        }
49
50
        return $topicInstance;
51
    }
52
}
53