TopicRouter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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