Completed
Push — master ( 3f30e7...37b632 )
by Dmitry
08:21
created

Event::index()   C

Complexity

Conditions 10
Paths 223

Size

Total Lines 52

Duplication

Lines 8
Ratio 15.38 %

Code Coverage

Tests 25
CRAP Score 10.2622

Importance

Changes 0
Metric Value
dl 8
loc 52
ccs 25
cts 29
cp 0.8621
rs 6.2839
c 0
b 0
f 0
cc 10
nc 223
nop 3
crap 10.2622

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 __process(Application $app, BasisEvent $event, Service $service)
13
    {
14
        return $this->index($app, $event, $service);
15
    }
16
17 1
    public function index(Application $app, BasisEvent $event, Service $service)
18
    {
19 1
        $start = microtime(1);
20
21
        try {
22 1
            $info = $this->getEventInfo();
23 1
            $subscription = $event->getSubscription();
24
25 1
            $patterns = [];
26 1
            foreach (array_keys($subscription) as $pattern) {
27 1
                if ($service->eventMatch($info->event, $pattern)) {
28 1
                    $patterns[] = $pattern;
29
                }
30
            }
31
32 1
            if (!count($patterns)) {
33
                $service->unsubscribe($info->event);
34
                throw new Exception("No subscription on event ".$info->event);
35
            }
36
37 1
            $listeners = [];
38 1
            foreach ($patterns as $pattern) {
39 1
                foreach ($subscription[$pattern] as $listener) {
40 1
                    if (!array_key_exists($listener, $listeners)) {
41 1
                        $listeners[$listener] = $app->get('Listener\\'.$listener);
42
                    }
43
                }
44
            }
45
46 1
            $result = [];
47 1
            $issues = [];
48 1 View Code Duplication
            foreach ($listeners as $nick => $listener) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49 1
                $result[$nick] = $this->handleEvent($app, $listener, $info);
50
                try {
51 1
                    $event->fireChanges($nick);
52 1
                } catch (Exception $e) {
53 1
                    $issues[$nick] =  $e->getMessage();
54
                }
55
            }
56
57
58
            return [
59 1
                'success' => true,
60 1
                'data' => $result,
61 1
                'issues' => $issues,
62 1
                'time' => microtime(1) - $start,
63
            ];
64
65
        } catch (Exception $e) {
66
            return ['success' => false, 'message' => $e->getMessage()];
67
        }
68
    }
69
70 1
    private function getEventInfo()
71
    {
72 1
        if (!array_key_exists('event', $_REQUEST)) {
73
            throw new Exception('No event defined');
74
        }
75
76 1
        if (!array_key_exists('context', $_REQUEST)) {
77
            throw new Exception('No context defined');
78
        }
79
80 1
        $context = json_decode($_REQUEST['context']);
81
82 1
        if (!$context) {
83
            throw new Exception('Invalid context');
84
        }
85
86 1
        $parts = explode('.', $_REQUEST['event']);
87 1
        $action = array_pop($parts);
88
89
        return (object) [
90 1
            'event' => $_REQUEST['event'],
91 1
            'space' => implode('.', $parts),
92 1
            'action' => $action,
93 1
            'context' => $context,
94
        ];
95
    }
96
97 1
    private function handleEvent($app, $instance, $info)
98
    {
99 1
        foreach ($info as $k => $v) {
100 1
            $instance->$k = $v;
101
        }
102 1
        if (!method_exists($instance, 'run')) {
103
            throw new Exception('No run method for '.$class);
104
        }
105
106 1
        return $app->call([$instance, 'run']);
107
    }
108
}
109