TemplatesController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A templatesAction() 0 23 5
A getTemplatesPath() 0 10 5
A validTemplate() 0 4 2
A validCommonTemplate() 0 4 2
1
<?php
2
3
namespace ExpressApi\V1\Rpc\Templates;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
7
class TemplatesController extends AbstractActionController {
8
9
    private static $templateInfoFile = 'def.json';
10
    private static $commonInfoFile = 'common.json';
11
12
    public function templatesAction() {
13
        $templatesPath = $this->getTemplatesPath();
14
        $templateData = array(
15
            'templates' => array(),
16
            'common' => array()
17
        );
18
        if(is_dir($templatesPath)) {
19
            $dirContent = scandir($templatesPath);
20
            foreach($dirContent as $templateDir) {
21
                $contentFile = $templatesPath.'/'.$templateDir;
22
                if($this->validTemplate($contentFile)) {
23
                    $templateInfo = json_decode(file_get_contents($contentFile.'/'.self::$templateInfoFile));
24
                    $templateInfo->id = $templateDir;
25
                    $templateInfo->name = ucfirst(str_replace('_', ' ', $templateDir));
26
                    $templateData['templates'][] = $templateInfo;
27
                }
28
            }
29
            if($this->validCommonTemplate($templatesPath)) {
30
                $templateData['common'] = json_decode(file_get_contents($templatesPath.'/'.self::$commonInfoFile));
31
            }
32
        }
33
        return $templateData;
34
    }
35
36
    private function getTemplatesPath() {
37
        $config = $this->getServiceLocator()->get('Config');
38
        if(is_array($config) &&
39
                isset($config['view_manager']) &&
40
                is_array($config['view_manager']['template_path_stack']) &&
41
                isset($config['view_manager']['template_path_stack'])) {
42
            return array_pop($config['view_manager']['template_path_stack']);
43
        }
44
        return '';
45
    }
46
47
    private function validTemplate($completeName) {
48
        $def = $completeName.DIRECTORY_SEPARATOR.self::$templateInfoFile;
49
        return is_dir($completeName) && file_exists($def);
50
    }
51
52
    private function validCommonTemplate($completeName) {
53
        $def = $completeName.DIRECTORY_SEPARATOR.self::$commonInfoFile;
54
        return is_dir($completeName) && file_exists($def);
55
    }
56
}
57