Http::process()   B
last analyzed

Complexity

Conditions 11
Paths 24

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.6363

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 19
cts 23
cp 0.8261
rs 7.3166
c 0
b 0
f 0
cc 11
nc 24
nop 1
crap 11.6363

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 3
    public function __construct(Application $app)
13
    {
14 3
        $this->app = $app;
15 3
    }
16
17 3
    public function process(string $uri): ?string
18
    {
19 3
        list($controller, $method) = $this->getChain($uri);
20 3
        $className = "Controller\\".ucfirst($controller);
21 3
        $class = $this->app->get(Filesystem::class)->completeClassName($className);
22 3
        if (!class_exists($class)) {
23 1
            $frameworkClass = $this->app->get(Framework::class)->completeClassName($className);
24
        }
25
26 3
        if (!class_exists($class)) {
27 1
            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 1
            $class = $frameworkClass;
34
        }
35
36 3
        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 3
            $url = substr($uri, (strlen($controller) + strlen($method) + 3));
44
        }
45
46 3
        $container = $this->app->get(Container::class);
47 3
        $result = $container->call([$container->get($class), $method], ['url' => $url]);
48
49 3
        if (is_array($result) || is_object($result)) {
50 1
            return json_encode($result);
51
        } else {
52 2
            return $result;
53
        }
54
    }
55
56 3
    public function getChain(string $uri) : array
57
    {
58 3
        list($clean) = explode('?', $uri);
59 3
        $chain = [];
60 3
        foreach (explode('/', $clean) as $k => $v) {
61 3
            if ($v) {
62 3
                $chain[] = $v;
63
            }
64
        }
65
66 3
        if (!count($chain)) {
67 1
            $chain[] = 'index';
68
        }
69
70 3
        if (count($chain) == 1) {
71 2
            $chain[] = 'index';
72
        }
73
74 3
        return $chain;
75
    }
76
}
77