• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / Module / 4245494566

pending completion
4245494566

push

github

Olivier Laviale
Resolve templates using Config instead of Collection

115 of 432 relevant lines covered (26.62%)

0.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/Module/ModuleOperationDispatcher.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\Module;
13

14
use ICanBoogie\HTTP\Request;
15
use ICanBoogie\Module;
16
use ICanBoogie\Operation;
17
use ICanBoogie\Routing\Route;
18
use ICanBoogie\Routing\RouteCollection;
19

20
use function ICanBoogie\camelize;
21

22
/**
23
 * A request dispatcher for module operations.
24
 */
25
final class ModuleOperationDispatcher extends Operation\OperationRouteDispatcher
26
{
27
    /**
28
     * @var ModuleCollection
29
     */
30
    private $modules;
31

32
    public function __construct(RouteCollection $routes, ModuleCollection $modules)
33
    {
34
        $this->modules = $modules;
×
35

36
        parent::__construct($routes);
×
37
    }
38

39
    /**
40
     * @inheritdoc
41
     */
42
    protected function resolve_route(Request $request, $normalized_path, array &$captured)
43
    {
44
        return parent::resolve_route($request, $normalized_path, $captured)
×
45
            ?: $this->resolve_module_route($request, $normalized_path, $captured);
×
46
    }
47

48
    /**
49
     * Resolves module route.
50
     *
51
     * @return Route|null A made up {@link Route} instance or `null` if the route
52
     * cannot be resolved.
53
     */
54
    private function resolve_module_route(Request $request, string $normalized_path, array &$captured): ?Route
55
    {
56
        $parsed_path = $this->parse_path($normalized_path);
×
57

58
        if ($parsed_path === false) {
×
59
            return null;
×
60
        }
61

62
        [ $module, $operation_name, $operation_key ] = $parsed_path;
×
63

64
        $operation_class = $this->resolve_operation_class($operation_name, $module);
×
65

66
        if (!$operation_class) {
×
67
            return null;
×
68
        }
69

70
        $captured[Operation::KEY] = $operation_key;
×
71

72
        $pattern = $operation_key
×
73
            ? sprintf('/api/:%s/:%s/:%s', Operation::DESTINATION, Operation::KEY, Operation::NAME)
×
74
            : sprintf('/api/:%s/:%s', Operation::DESTINATION, Operation::NAME);
×
75

76
        return Route::from([
×
77

78
            ModuleRouteDefinition::PATTERN => $pattern,
×
79
            ModuleRouteDefinition::CONTROLLER => $operation_class,
×
80
            ModuleRouteDefinition::MODULE => $module->id,
×
81
            ModuleRouteDefinition::VIA => $request->method
×
82

83
        ]);
×
84
    }
85

86
    /**
87
     * Parse path to extract operation information.
88
     *
89
     * @return array|bool The operation information or `false` if the path is not suitable.
90
     */
91
    private function parse_path(string $path)
92
    {
93
        if (strpos($path, Operation::RESTFUL_BASE) !== 0) {
×
94
            return false;
×
95
        }
96

97
        $parts = explode('/', trim($path, '/'));
×
98
        array_shift($parts);
×
99
        $n = count($parts);
×
100

101
        if ($n < 2 || $n > 4) {
×
102
            return false;
×
103
        }
104

105
        $operation_key = null;
×
106

107
        if ($n === 2) {
×
108
            list($module_id, $operation_name) = $parts;
×
109
        } else {
110
            list($module_id, $operation_key, $operation_name) = $parts;
×
111
        }
112

113
        $modules = $this->modules;
×
114

115
        if (!isset($modules[$module_id])) {
×
116
            return false;
×
117
        }
118

119
        return [ $modules[$module_id], $operation_name, $operation_key ];
×
120
    }
121

122
    /**
123
     * Resolves the operation class.
124
     *
125
     * @return false|string
126
     */
127
    private function resolve_operation_class(string $operation_name, Module $module)
128
    {
129
        $unqualified_class_name = 'Operation\\' . camelize(strtr($operation_name, '-', '_')) . 'Operation';
×
130

131
        return $this->modules->resolve_classname($unqualified_class_name, $module);
×
132
    }
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc