Passed
Push — master ( a40ea7...ddbf02 )
by Anthony
03:04
created

RouterController::getBaseLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
	namespace core;
3
	
4
	
5
	class RouterController {
6
		private $page;
7
		private $controller;
8
		private $part;
9
		private $erreur;
10
		
11
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
12
		public function __construct($page, $admin = null) {
13
			if (strpos($page, "controller") !== false) {
14
				$this->part = "app";
15
				if ($admin !== null) {
16
					$this->part = "admin";
17
				}
18
				
19
				$this->page = $page;
20
				
21
				if (($this->getTestCoreController() === false) && ($this->getTestModuleController()  === false)) {
22
					$this->controller = $this->part."/".$this->page.".php";
23
				}
24
				
25
				$this->getTestControllerExist();
26
				$this->erreur = false;
27
			}
28
		}
29
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
30
		
31
		
32
		//-------------------------- GETTER ----------------------------------------------------------------------------//
33
		public function getController(){
34
		    return $this->controller;
35
		}
36
		public function getErreur(){
37
		    return $this->erreur;
38
		}
39
		
40
		/**
41
		 * @return bool
42
		 * this function set if is a controller core the link to the controller
43
		 */
44
		private function getTestCoreController() {
45
			if (strpos($this->page, "controller/core") !== false) {
46
				$this->controller = $this->getBaseLink().".php";
47
				return true;
48
			}
49
			
50
			return false;
51
		}
52
		
53
		/**
54
		 * @return bool
55
		 * this function set if is a controller module the link to the controller
56
		 */
57
		private function getTestModuleController() {
58
			if (strpos($this->page, "controller/modules") !== false) {
59
				$explode = explode("/", $this->getBaseLink(), 3);
60
				$this->controller = $explode[0]."/".$explode[1]."/".$this->part."/controller/".$explode[2].".php";
61
				return true;
62
			}
63
			
64
			return false;
65
		}
66
		
67
		/**
68
		 * @return mixed
69
		 * this function return link and delete controller at the begining of the link
70
		 */
71
		private function getBaseLink() {
72
			$explode = explode("/", $this->page, 2);
73
			return end($explode);
74
		}
75
		
76
		/**
77
		 * redirect on 404 if controller doesn't exist
78
		 */
79
		private function getTestControllerExist() {
80
			if (!file_exists($this->controller)) {
81
				RedirectError::Redirect(404);
82
			}
83
		}
84
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
85
		
86
		
87
		//-------------------------- SETTER ----------------------------------------------------------------------------//
88
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
89
	}