Test Failed
Push — master ( 6773ab...a1c0a8 )
by Julien
05:51
created

Application::request()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 16
c 2
b 1
f 0
nc 6
nop 1
dl 0
loc 27
ccs 0
cts 17
cp 0
crap 20
rs 9.7333
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc;
13
14
use Phalcon\Di\DiInterface;
15
use Phalcon\Http\ResponseInterface;
16
use Zemit\Dispatcher\AbstractDispatcher;
17
use Zemit\Mvc\Dispatcher as MvcDispatcher;
18
use Zemit\Cli\Dispatcher as CliDispatcher;
19
20
/**
21
 * Simple HMVC - allow requests with namespaces and modules
22
 * {@inheritdoc}
23
 */
24
class Application extends \Phalcon\Mvc\Application
25
{
26
    /**
27
     * HMVC Application
28
     * {@inheritdoc}
29
     */
30 15
    public function __construct(DiInterface $di)
31
    {
32
        // Registering app itself as a service
33 15
        $di->setShared('application', $this);
34 15
        parent::__construct($di);
35
    }
36
37
    /**
38
     * HMVC request
39
     * You can request call any module/namespace
40
     */
41
    public function request(array $location = []): string
42
    {
43
        // Get a unique dispatcher
44
        $dispatcher = clone $this->getDI()->get('dispatcher');
45
        assert($dispatcher instanceof AbstractDispatcher);
46
        
47
        // Route dispatcher
48
        $dispatcher->setDefaultNamespace($location['namespace'] ?? $dispatcher->getNamespaceName());
49
        $dispatcher->setNamespaceName($location['namespace'] ?? $dispatcher->getNamespaceName());
50
        $dispatcher->setModuleName($location['module'] ?? $dispatcher->getModuleName());
51
        if ($dispatcher instanceof MvcDispatcher) {
0 ignored issues
show
introduced by
$dispatcher is never a sub-type of Zemit\Mvc\Dispatcher.
Loading history...
52
            $dispatcher->setControllerName($location['controller'] ?? 'index');
53
        }
54
        elseif ($dispatcher instanceof CliDispatcher) {
0 ignored issues
show
introduced by
$dispatcher is never a sub-type of Zemit\Cli\Dispatcher.
Loading history...
55
            $dispatcher->setTaskName($location['task'] ?? 'index');
56
        }
57
        $dispatcher->setActionName($location['action'] ?? 'index');
58
        $dispatcher->setParams($location['params'] ?? []);
59
        $dispatcher->dispatch();
60
        
61
        // Get and return value
62
        $response = $dispatcher->getReturnedValue();
63
        if ($response instanceof ResponseInterface) {
64
            return $response->getContent();
65
        }
66
        
67
        return $response;
68
    }
69
}
70