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

Application   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 15%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 19
c 2
b 1
f 0
dl 0
loc 44
ccs 3
cts 20
cp 0.15
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 27 4
A __construct() 0 5 1
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