Topic   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 66
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B cacheSet() 0 9 5
A show() 0 10 3
A subscribe() 0 10 4
B publish() 0 18 8
1
<?php
2
/**
3
 * Opine\Topic
4
 *
5
 * Copyright (c)2013, 2014 Ryan Mahoney, https://github.com/Opine-Org <[email protected]>
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
namespace Opine\PubSub;
26
27
use ArrayObject;
28
use Exception;
29
use Opine\Interfaces\Topic as TopicInterface;
30
use Opine\Interfaces\Route as RouteInterface;
31
32
class Topic implements TopicInterface
33
{
34
    private $topics = [];
35
    private $route;
36
    private $model;
37
38
    public function __construct(RouteInterface $route, $model)
39
    {
40
        $this->route = $route;
41
        $this->model = $model;
42
    }
43
44
    public function cacheSet($cache)
45
    {
46
        if ($cache === false || !is_array($cache) || !isset($cache['topics']) || !is_array($cache['topics'])) {
47
            $this->topics = $this->model->readDiskCache();
48
49
            return;
50
        }
51
        $this->topics = (array) $cache['topics'];
52
    }
53
54
    /**
55
     * @codeCoverageIgnore
56
     */
57
    public function show()
58
    {
59
        foreach ($this->topics as $key => $value) {
60
            echo $key, "\n";
61
            foreach ($value as $call) {
62
                echo ' - ', $call, "\n";
63
            }
64
            echo "\n";
65
        }
66
    }
67
68
    public function subscribe($topic, $callback)
69
    {
70
        if (!is_string($callback) || substr_count($callback, '@') != 1) {
71
            throw new Exception('Callback must be a string expressed in the format: service@method');
72
        }
73
        if (!isset($this->topics[$topic])) {
74
            $this->topics[$topic] = [];
75
        }
76
        $this->topics[$topic][] = $callback;
77
    }
78
79
    public function publish($topic, ArrayObject $context)
80
    {
81
        if (!isset($this->topics[$topic]) || !is_array($this->topics[$topic]) || empty($this->topics[$topic])) {
82
            return;
83
        }
84
        foreach ($this->topics[$topic] as $subscriber) {
85
            if (!is_string($subscriber)) {
86
                continue;
87
            }
88
            if (substr_count($subscriber, '@') != 1) {
89
                continue;
90
            }
91
            $response = $this->route->serviceMethod($subscriber, $context);
0 ignored issues
show
Unused Code introduced by
The call to Route::serviceMethod() has too many arguments starting with $context.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
92
            if ($response === false) {
93
                break;
94
            }
95
        }
96
    }
97
}
98