Completed
Push — master ( 006cbd...4884ca )
by Dmitry
03:44 queued 22s
created

Http::process()   C

Complexity

Conditions 11
Paths 24

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 16.0905

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 15
cts 23
cp 0.6522
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 25
nc 24
nop 1
crap 16.0905

How to fix   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;
4
5
use League\Container\Container;
6
use LogicException;
7
8
class Http
9
{
10
    private $app;
11
12 1
    public function __construct(Application $app)
13
    {
14 1
        $this->app = $app;
15 1
    }
16
17 1
    public function process(string $uri): ?string
18
    {
19 1
        list($controller, $method) = $this->getChain($uri);
20 1
        $className = "Controller\\".ucfirst($controller);
21 1
        $class = $this->app->get(Filesystem::class)->completeClassName($className);
22 1
        if (!class_exists($class)) {
23
            $frameworkClass = $this->app->get(Framework::class)->completeClassName($className);
24
        }
25
26 1
        if (!class_exists($class)) {
27
            if (!isset($frameworkClass) || !class_exists($frameworkClass)) {
28
                if (isset($frameworkClass) && !class_exists($frameworkClass)) {
29
                    throw new LogicException("No class for $controller $controller, [$class, $frameworkClass]");
30
                }
31
                throw new LogicException("No class for $controller $controller - $class");
32
            }
33
            $class = $frameworkClass;
34
        }
35
36 1
        if (!method_exists($class, $method)) {
37 1
            if (!method_exists($class, '__process')) {
38
                return "$controller/$method not found";
39
            }
40 1
            $url = substr($uri, strlen($controller)+2);
41 1
            $method = '__process';
42
        } else {
43 1
            $url = substr($uri, (strlen($controller) + strlen($method) + 3));
44
        }
45
46 1
        $container = $this->app->get(Container::class);
47 1
        $result = $container->call([$container->get($class), $method], ['url' => $url]);
48
49 1
        if (is_array($result) || is_object($result)) {
50
            return json_encode($result);
51
        } else {
52 1
            return $result;
53
        }
54
    }
55
56 1
    public function getChain(string $uri) : array
57
    {
58 1
        list($clean) = explode('?', $uri);
59 1
        $chain = [];
60 1
        foreach (explode('/', $clean) as $k => $v) {
61 1
            if ($v) {
62 1
                $chain[] = $v;
63
            }
64
        }
65
66 1
        if (!count($chain)) {
67 1
            $chain[] = 'index';
68
        }
69
70 1
        if (count($chain) == 1) {
71 1
            $chain[] = 'index';
72
        }
73
74 1
        return $chain;
75
    }
76
}
77