Test Failed
Push — master ( ffc597...4abc3b )
by Julien
12:58 queued 09:01
created

Application::request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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