Passed
Push — master ( 1b00af...da5542 )
by Jean-Christophe
05:51
created

RestApiControllerCreator::create()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 31
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 5.0016

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 31
ccs 24
cts 25
cp 0.96
rs 9.2088
c 0
b 0
f 0
cc 5
nc 9
nop 2
crap 5.0016
1
<?php
2
3
namespace Ubiquity\scaffolding\creators;
4
5
use Ubiquity\scaffolding\ScaffoldController;
6
use Ubiquity\controllers\rest\RestServer;
7
use Ubiquity\utils\base\UFileSystem;
8
9
/**
10
 * Creates a Rest controller for JsonRestApi
11
 * Ubiquity\scaffolding\creators$RestControllerCreator
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.2
16
 *
17
 */
18
class RestApiControllerCreator extends BaseControllerCreator {
19
20 1
	public function __construct($restControllerName, $routePath = '') {
21 1
		$this->controllerName = $restControllerName;
22 1
		$this->routePath = $routePath;
23 1
		$this->controllerNS = RestServer::getRestNamespace ();
24 1
		$this->templateName = 'restApiController.tpl';
25 1
	}
26
27 1
	public function create(ScaffoldController $scaffoldController, $reInit = null) {
28 1
		$this->scaffoldController = $scaffoldController;
29 1
		$controllerName = $this->controllerName;
30 1
		$controllerNS = $this->controllerNS;
31 1
		$messages = [ ];
32
33 1
		$restControllersDir = \ROOT . \DS . str_replace ( "\\", \DS, $controllerNS );
34 1
		UFileSystem::safeMkdir ( $restControllersDir );
35
36 1
		$filename = $restControllersDir . \DS . $controllerName . ".php";
37 1
		if (! \file_exists ( $filename )) {
38 1
			$routeName = "";
39 1
			$templateDir = $scaffoldController->getTemplateDir ();
40 1
			$namespace = '';
41 1
			if ($controllerNS != null)
42 1
				$namespace = "namespace " . $controllerNS . ";";
43 1
			if ($this->routePath != null) {
44 1
				$routeName = $this->addRoute ( $this->routePath );
45
			}
46 1
			$variables = [ "%route%" => $this->routePath,"%controllerName%" => $controllerName,"%namespace%" => $namespace,"%routeName%" => $routeName ];
47 1
			$this->addVariablesForReplacement ( $variables );
48 1
			UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . $this->templateName, $filename, $variables );
49 1
			$messages [] = $scaffoldController->showSimpleMessage ( "The <b>" . $controllerName . "</b> Rest controller has been created in <b>" . UFileSystem::cleanPathname ( $filename ) . "</b>.", "success", "Rest creation", "checkmark circle", 30000, "msgGlobal" );
50 1
			if (isset ( $reInit )) {
51 1
				$this->scaffoldController->initRestCache ( false );
52
			}
53
		} else {
54
			$messages [] = $scaffoldController->showSimpleMessage ( "The file <b>" . $filename . "</b> already exists.<br>Can not create the <b>" . $controllerName . "</b> Rest controller!", "warning", "Rest error", "warning circle", 30000, "msgGlobal" );
55
		}
56 1
		$this->scaffoldController->_refreshRest ( true );
57 1
		echo implode ( "\n", $messages );
58 1
	}
59
60
	protected function addVariablesForReplacement(&$variables) {
1 ignored issue
show
Unused Code introduced by
The parameter $variables is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
	protected function addVariablesForReplacement(/** @scrutinizer ignore-unused */ &$variables) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
	}
62
63
	protected function addViews(&$uses, &$messages, &$classContent) {
64
	}
65
}
66
67