Completed
Push — master ( dc8a1c...bb44ed )
by Dmitry
03:57
created

Event::getRequestContext()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 0
crap 20
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
            $context = $this->getRequestContext();
16
            $subscription = $event->getSubscription();
17
18
            if (!array_key_exists($_REQUEST['event'], $subscription)) {
19
                $service->unsubscribe($_REQUEST['event']);
20
                throw new Exception("No subscription on event ".$_REQUEST['event'], 1);
21
            }
22
23
            $result = [];
24
25
            foreach ($subscription[$_REQUEST['event']] as $listener) {
26
                $instance = $app->get('Listener\\'.$listener);
27
                $result[$listener] = $this->handleEvent($instance, $context);
28
            }
29
30
            return [
31
                'success' => true,
32
                'data' => $result,
33
            ];
34
35
        } catch (Exception $e) {
36
            return ['success' => false, 'message' => $e->getMessage()];
37
        }
38
    }
39
40
    private function getRequestContext()
0 ignored issues
show
Coding Style introduced by
getRequestContext 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...
41
    {
42
        if (!array_key_exists('event', $_REQUEST)) {
43
            throw new Exception('No event defined');
44
        }
45
46
        if (!array_key_exists('context', $_REQUEST)) {
47
            throw new Exception('No context defined');
48
        }
49
50
        $context = json_decode($_REQUEST['context']);
51
52
        if (!$context) {
53
            throw new Exception('Invalid context');
54
        }
55
56
        return $context;
57
    }
58
59
    private function handleEvent($instance, $context)
60
    {
61
        foreach ($context as $k => $v) {
62
            $instance->$k = $v;
63
        }
64
        if (!method_exists($instance, 'run')) {
65
            throw new Exception('No run method for '.$class);
66
        }
67
68
        return $app->call([$instance, 'run']);
0 ignored issues
show
Bug introduced by
The variable $app does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
69
    }
70
}
71