Completed
Push — 3.0 ( d53eed )
by Vermeulen
02:03
created

BfwController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace BfwController;
4
5
/**
6
 * Controller system class
7
 */
8
class BfwController implements \SplObserver
9
{
10
    /**
11
     * @var \BFW\Module $module The bfw module instance for this module
12
     */
13
    protected $module;
14
    
15
    /**
16
     * @var \BFW\Config $config The bfw config instance for this module
17
     */
18
    protected $config;
19
    
20
    /**
21
     * @var \BFW\ControllerRouterLink Linker between controller and router instance
22
     */
23
    protected $routerLinker;
24
    
25
    /**
26
     * Constructor
27
     * Get config and linker instance
28
     * 
29
     * @param \BFW\Module $module
30
     */
31
    public function __construct(\BFW\Module $module)
32
    {
33
        $this->module = $module;
34
        $this->config = $module->getConfig();
35
        
36
        $this->routerLinker = \BFW\ControllerRouterLink::getInstance();
37
    }
38
    
39
    /**
40
     * Observer update method
41
     * Call run method on action "bfw_run_finish".
42
     * 
43
     * @param \SplSubject $subject
44
     * 
45
     * @return void
46
     */
47
    public function update(\SplSubject $subject)
48
    {
49
        if ($subject->getAction() === 'bfw_run_finish') {
50
            $this->run();
51
        }
52
    }
53
    
54
    /**
55
     * Run controller system if application is not run in cli mode
56
     * 
57
     * @return void
58
     */
59
    protected function run()
60
    {
61
        if (PHP_SAPI === 'cli') {
62
            return;
63
        }
64
        
65
        $useClass = $this->config->getConfig('useClass');
66
        
67
        if ($useClass === true) {
68
            $this->runObject();
69
        } else {
70
            $this->runProcedural();
71
        }
72
        
73
        $app = \BFW\Application::getInstance();
74
        $app->notifyAction('BfwController_run_finish');
75
    }
76
    
77
    /**
78
     * Call controller when is an object.
79
     * 
80
     * @return void
81
     */
82
    protected function runObject()
83
    {
84
        $targetInfos = (object) $this->routerLinker->getTarget();
85
        $class       = $targetInfos->class;
86
        $method      = $targetInfos->method;
87
        
88
        if (!class_exists($class)) {
89
            throw new \Exception('Class '.$class.' not found');
90
        }
91
        
92
        $classInstance = new $class;
93
        if (!method_exists($classInstance, $method)) {
94
            throw new \Exception(
95
                'Method '.$method.' not found in class '.$class
96
            );
97
        }
98
        
99
        $classInstance->{$method}();
100
    }
101
    
102
    /**
103
     * Call controler when is a procedural file
104
     * 
105
     * @return void
106
     */
107
    protected function runProcedural()
108
    {
109
        $routerLinker = $this->routerLinker;
110
        
111
        $runFct = function() use ($routerLinker) {
112
            $controllerFile = $routerLinker->getTarget();
113
            
114
            if (!file_exists(CTRL_DIR.$controllerFile)) {
115
                throw new \Exception(
116
                    'Controller file '.$controllerFile.' not found.'
117
                );
118
            }
119
            
120
            include(CTRL_DIR.$controllerFile);
121
        };
122
        
123
        $runFct();
124
    }
125
}
126