Completed
Push — master ( b893df...d009f8 )
by Park Jong-Hun
03:04
created

RouterMapBuilder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 68
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 1
dl 16
loc 68
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setRouterConfig() 0 4 1
A build() 0 11 2
A getRoutePathMap() 0 7 1
A addRouterMap() 0 10 3
A resolveHttpGetHandler() 8 8 4
A resolveHttpPostHandler() 8 8 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Core\ControllerDispatcher;
4
5
use Prob\Router\Map;
6
use Core\ControllerDispatcher;
7
8
class RouterMapBuilder
9
{
10
11
    private $routerConfig = [];
12
13
    public function setRouterConfig(array $config)
14
    {
15
        $this->routerConfig = $config;
16
    }
17
18
    public function build()
19
    {
20
        $map = new Map();
21
        $map->setNamespace($this->routerConfig['namespace']);
22
23
        foreach ($this->getRoutePathMap() as $k => $v) {
24
            $this->addRouterMap($map, $k, $v);
25
        }
26
27
        return $map;
28
    }
29
30
    private function getRoutePathMap()
31
    {
32
        $paths = $this->routerConfig;
33
        unset($paths['namespace']);
34
35
        return $paths;
36
    }
37
38
    /**
39
     * $handlers schema
40
     *     (string) $handlers method or function name | {closure}
41
     *     (array) $handlers['GET' | 'POST'] => method or function name | {closure}
42
     *
43
     * @param Map    $routerMap
44
     * @param string $path  url path
45
     * @param string|array|closure $handlers
46
     */
47
    private function addRouterMap(Map $routerMap, $path, $handlers)
48
    {
49
        if ($this->resolveHttpGetHandler($handlers)) {
50
            $routerMap->get($path, $this->resolveHttpGetHandler($handlers));
51
        }
52
53
        if ($this->resolveHttpPostHandler($handlers)) {
54
            $routerMap->post($path, $this->resolveHttpPostHandler($handlers));
55
        }
56
    }
57
58 View Code Duplication
    private function resolveHttpGetHandler($handlers)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
59
    {
60
        if (gettype($handlers) === 'string' || is_callable($handlers)) {
61
            return $handlers;
62
        }
63
64
        return isset($handlers['GET']) ? $handlers['GET'] : null;
65
    }
66
67 View Code Duplication
    private function resolveHttpPostHandler($handlers)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
68
    {
69
        if (gettype($handlers) === 'string' || is_callable($handlers)) {
70
            return;
71
        }
72
73
        return isset($handlers['POST']) ? $handlers['POST'] : null;
74
    }
75
}
76