Completed
Push — master ( 62549b...430ab9 )
by Nazar
04:10
created

Files::files_router_handler_internal()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 6
eloc 12
nc 5
nop 4
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\App\Router;
9
10
/**
11
 * @property string[] $controller_path Path that will be used by controller to render page
12
 */
13
trait Files {
14
	/**
15
	 * Include files necessary for module page rendering
16
	 *
17
	 * @param \cs\Request $Request
18
	 *
19
	 * @throws \cs\ExitException
20
	 */
21
	protected function files_router ($Request) {
22
		foreach ($this->controller_path as $index => $path) {
23
			/**
24
			 * Starting from index 2 we need to maintain slash-separated string that includes all paths from index 1 and till current
25
			 */
26
			if ($index > 1) {
27
				$path = implode('/', array_slice($this->controller_path, 1, $index));
28
			}
29
			$next_exists = isset($this->controller_path[$index + 1]);
30
			$this->files_router_handler($Request, $this->working_directory, $path, !$next_exists);
31
		}
32
	}
33
	/**
34
	 * Include files that corresponds for specific paths in URL
35
	 *
36
	 * @param \cs\Request $Request
37
	 * @param string      $dir
38
	 * @param string      $basename
39
	 * @param bool        $required
40
	 *
41
	 * @throws \cs\ExitException
42
	 */
43
	protected function files_router_handler ($Request, $dir, $basename, $required = true) {
44
		$this->files_router_handler_internal($Request, $dir, $basename, $required);
45
	}
46
	/**
47
	 * @param \cs\Request $Request
48
	 * @param string      $dir
49
	 * @param string      $basename
50
	 * @param bool        $required
51
	 *
52
	 * @throws \cs\ExitException
53
	 */
54
	protected function files_router_handler_internal ($Request, $dir, $basename, $required) {
55
		$included = _include("$dir/$basename.php", false, false) !== false;
56
		if (!$Request->cli_path && !$Request->api_path) {
57
			return;
58
		}
59
		$request_method = strtolower($Request->method);
60
		$included       = _include("$dir/$basename.$request_method.php", false, false) !== false || $included;
61
		if ($included || !$required) {
62
			return;
63
		}
64
		$this->handler_not_found(
1 ignored issue
show
Bug introduced by
It seems like handler_not_found() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
65
			$this->files_router_available_methods($dir, $basename),
66
			$request_method,
67
			$Request
68
		);
69
	}
70
	/**
71
	 * @param string $dir
72
	 * @param string $basename
73
	 *
74
	 * @return string[]
75
	 */
76
	protected function files_router_available_methods ($dir, $basename) {
77
		$methods = get_files_list($dir, "/^$basename\\.[a-z]+\\.php$/");
78
		$methods = _strtoupper(_substr($methods, strlen($basename) + 1, -4));
79
		natcasesort($methods);
80
		return array_values($methods);
81
	}
82
}
83