GetModeController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 47
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getModeAction() 0 15 3
A getPublicPath() 0 9 4
A loadHtaccess() 0 3 1
A getMode() 0 12 4
1
<?php
2
3
namespace ExpressApi\V1\Rpc\GetMode;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use ZF\ApiProblem\ApiProblem;
7
use ZF\ApiProblem\ApiProblemResponse;
8
9
class GetModeController extends AbstractActionController {
10
11
    private $htaccessName = '.htaccess';
12
13
    public function getModeAction() {
14
        $response = array(
15
            'success' => false,
16
            'mode' => ''
17
        );
18
        $htaccess = $this->getPublicPath().$this->htaccessName;
19
        if(file_exists($htaccess) && is_file($htaccess)) {
20
            $response['mode'] = $this->getMode($this->loadHtaccess($htaccess));
21
            $response['success'] = !empty($response['mode']);
22
        }
23
        else {
24
            return new ApiProblemResponse(new ApiProblem(400, 'Htaccess file not exist.'));
25
        }
26
        return $response;
27
    }
28
29
    private function getPublicPath() {
30
        $config = $this->getServiceLocator()->get('Config');
31
        if(is_array($config) &&
32
                isset($config['template_assets_resolver']) &&
33
                isset($config['template_assets_resolver']['public_path'])) {
34
            return $config['template_assets_resolver']['public_path'];
35
        }
36
        return '';
37
    }
38
39
    private function loadHtaccess($path) {
40
        return file_get_contents($path);
41
    }
42
43
    private function getMode($content) {
44
        $envLinePart = 'SetEnv APP_ENV ';
45
        $matches = array();
46
        preg_match_all("~\\".$envLinePart."\\w+~i", $content, $matches);
47
        if(is_array($matches) && is_array($matches[0])) {
48
            $envLine = array_pop($matches[0]);
49
            if(!empty($envLine)) {
50
                return str_replace($envLinePart, '', $envLine);
51
            }
52
        }
53
        return '';
54
    }
55
}
56