Completed
Push — master ( 5a6cbe...c8e6dd )
by Dmitry
05:03
created

Event   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 49
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C index() 0 46 9
1
<?php
2
3
namespace Basis\Controller;
4
5
use Basis\Application;
6
use Basis\Event as BasisEvent;
7
use Basis\Service;
8
use Exception;
9
10
class Event
11
{
12
    public function index(Application $app, BasisEvent $event, Service $service)
0 ignored issues
show
Coding Style introduced by
index uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
13
    {
14
        try {
15
            if (!array_key_exists('event', $_REQUEST)) {
16
                throw new Exception('No event defined');
17
            }
18
19
            if (!array_key_exists('context', $_REQUEST)) {
20
                throw new Exception('No context defined');
21
            }
22
23
            $context = json_decode($_REQUEST['context']);
24
25
            if (!$context) {
26
                throw new Exception('Invalid context');
27
            }
28
29
            $subscription = $event->getSubscription();
30
31
            if (!array_key_exists($_REQUEST['event'], $subscription)) {
32
                $service->unsubscribe($_REQUEST['event']);
0 ignored issues
show
Bug introduced by
The method unsubscribe() does not exist on Basis\Service. Did you maybe mean subscribe()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
33
                throw new Exception("No subscription on event ".$_REQUEST['event'], 1);
34
            }
35
36
            $result = [];
37
38
            foreach ($subscription[$_REQUEST['event']] as $listener) {
39
                $instance = $app->get('Listener\\'.$listener);
40
                foreach ($context as $k => $v) {
41
                    $instance->$k = $v;
42
                }
43
                if (!method_exists($instance, 'run')) {
44
                    throw new Exception('No run method for '.$class);
45
                }
46
47
                $result[$listener] = $app->call([$instance, 'run']);
48
            }
49
50
            return [
51
                'success' => true,
52
                'data' => $result,
53
            ];
54
        } catch (Exception $e) {
55
            return ['success' => false, 'message' => $e->getMessage()];
56
        }
57
    }
58
}
59