Completed
Push — master ( cac1be...eadaa3 )
by Thomas
04:35
created

ModelService::getFullModelObjectName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4286
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
namespace keeko\tools\services;
3
4
use phootwork\collection\ArrayList;
5
use Propel\Generator\Model\Table;
6
use Propel\Generator\Model\Database;
7
use Propel\Generator\Util\QuickBuilder;
8
use phootwork\lang\Text;
9
use keeko\core\schema\ActionSchema;
10
use keeko\tools\utils\NamespaceResolver;
11
12
class ModelService extends AbstractService {
13
14
	private $models = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
15
	private $schema = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
16
	private $namespace = null;
17
	
18
	/** @var Database */
19
	private $database = null;
20
21
	/**
22
	 * Returns the propel schema. The three locations, where the schema is looked up in:
23
	 *
24
	 * 1. --schema option (if available)
25
	 * 2. database/schema.xml
26
	 * 3. core/database/schema.xml
27
	 *
28
	 * @throws \RuntimeException
29
	 * @return string the path to the schema
30
	 */
31 12
	public function getSchema() {
32 12
		$input = $this->io->getInput();
33 12
		if ($this->schema === null) {
34 12
			$workDir = $this->service->getProject()->getRootPath();
35 12
			$schema = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
36
			$schemas = [
37 12
				$input->hasOption('schema') ? $input->getOption('schema') : '',
38 12
				$workDir . '/database/schema.xml',
39
				$workDir . '/core/database/schema.xml'
40 12
			];
41 12
			foreach ($schemas as $path) {
42 12
				if (file_exists($path)) {
43 12
					$schema = $path;
44 12
					break;
45
				}
46 12
			}
47 12
			$this->schema = $schema;
48
				
49 12
			if ($schema === null) {
50
				$locations = implode(', ', $schemas);
51
				throw new \RuntimeException(sprintf('Can\'t find schema in these locations: %s', $locations));
52
			}
53 12
		}
54
55 12
		return $this->schema;
56
	}
57
	
58 3
	public function isCoreSchema() {
59 3
		return strpos($this->getSchema(), 'core') !== false;
60
	}
61
	
62
	public function hasSchema() {
63
		$vendorName = $this->packageService->getPackage()->getVendor();
64
		return $this->getSchema() !== null && ($this->isCoreSchema() ? $vendorName == 'keeko' : true);
65
	}
66
	
67
	/**
68
	 * Returns the propel database
69
	 *
70
	 * @return Database
71
	 */
72 12
	public function getDatabase() {
73 12
		if ($this->database === null) {
74 12
			$builder = new QuickBuilder();
75 12
			$builder->setSchema(file_get_contents($this->getSchema()));
76 12
			$this->database = $builder->getDatabase();
77 12
		}
78
	
79 12
		return $this->database;
80
	}
81
	
82
	/**
83
	 * Returns the tableName for a given name
84
	 *
85
	 * @param String $name tableName or modelName
86
	 * @return String tableName
87
	 */
88 11
	public function getTableName($name) {
89 11
		$db = $this->getDatabase();
90 11
		if (!Text::create($name)->startsWith($db->getTablePrefix())) {
91 11
			$name = $db->getTablePrefix() . $name;
92 11
		}
93
	
94 11
		return $name;
95
	}
96
	
97
	/**
98
	 * Returns all model names
99
	 *
100
	 * @return String[] an array of modelName
101
	 */
102
	public function getModelNames() {
103
		$names = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
104
		$database = $this->getDatabase();
105
		foreach ($database->getTables() as $table) {
106
			$names[] = $table->getOriginCommonName();
107
		}
108
	
109
		return $names;
110
	}
111
	
112
	/**
113
	 * Returns the propel models from the database, where table namespace matches package namespace
114
	 *
115
	 * @return ArrayList<Table>
1 ignored issue
show
Documentation introduced by
The doc-type ArrayList<Table> could not be parsed: Expected "|" or "end of type", but got "<" at position 9. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
116
	 */
117 1
	public function getModels() {
1 ignored issue
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
118 1
		if ($this->models === null) {
119 1
			$namespace = str_replace('\\\\', '\\', $this->getRootNamespace() . '\\model');
120 1
			$propel = $this->getDatabase();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
121
	
122 1
			$this->models = new ArrayList();
123
	
124 1
			foreach ($propel->getTables() as $table) {
125 1
				if (!$table->isCrossRef() && $table->getNamespace() == $namespace) {
126 1
					$this->models->add($table);
127 1
				}
128 1
			}
129 1
		}
130
	
131 1
		return $this->models;
132
	}
133
134
	/**
135
	 * Returns the model for the given name
136
	 *
137
	 * @param String $name modelName or tableName
138
	 * @return Table
139
	 */
140 7
	public function getModel($name) {
141 7
		$tableName = $this->getTableName($name);
142 7
		$db = $this->getDatabase();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
143
// 		echo $db->getNamespace();
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
144
// 		foreach ($db->getTables() as $table) {
145
// 			echo $table->getName();
146
// 		}
147 7
		$table = $db->getTable($tableName);
148
	
149 7
		return $table;
150
	}
151
	
152
	/**
153
	 * Checks whether the given model exists
154
	 *
155
	 * @param String $name tableName or modelName
156
	 * @return boolean
157
	 */
158 8
	public function hasModel($name) {
159 8
		return $this->getDatabase()->hasTable($this->getTableName($name), true);
160
	}
161
162
	/**
163
	 * Returns the root namespace for this package
164
	 *
165
	 * @return string the namespace
166
	 */
167 1
	public function getRootNamespace() {
168 1
		if ($this->namespace === null) {
169 1
			$input = $this->io->getInput();
170 1
			$ns = $input->hasOption('namespace')
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
171 1
				? $input->getOption('namespace')
172 1
				: null;
173 1
			if ($ns === null) {
174 1
				$package = $this->service->getPackageService()->getPackage();
175 1
				$ns = NamespaceResolver::getNamespace('src', $package);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
176 1
			}
177
				
178 1
			$this->namespace = $ns;
179 1
		}
180
	
181 1
		return $this->namespace;
182
	}
183
184
	/**
185
	 * Retrieves the model name for the given package in two steps:
186
	 * 
187
	 * 1. Check if it is passed as cli parameter
188
	 * 2. Retrieve it from the package name
189
	 *
190
	 * @return String
191
	 */
192
	public function getModelName() {
193
		$input = $this->io->getInput();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
194
		$modelName = $input->hasOption('model') ? $input->getOption('model') : null;
195
		if ($modelName === null && $this->isCoreSchema()) {
196
			$package = $this->service->getPackageService()->getPackage();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
197
			$packageName = $package->getName();
198
199
			if ($this->hasModel($packageName)) {
200
				$modelName = $packageName;
201
			}
202
		}
203
		return $modelName;
204
	}
205
	
206
	/**
207
	 * Parses the model name from a given action name
208
	 *
209
	 * @param ActionSchema $action
210
	 * @return String modelName
211
	 */
212 10 View Code Duplication
	public function getModelNameByAction(ActionSchema $action) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213 10
		$actionName = $action->getName();
214 10
		$modelName = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
215 10
		if (($pos = strpos($actionName, '-')) !== false) {
216 10
			$modelName = substr($actionName, 0, $pos);
217 10
		}
218 10
		return $modelName;
219
	}
220
221
	/**
222
	 * Returns the full model object name, including namespace
223
	 * 
224
	 * @param ActionSchema $action
225
	 * @return String fullModelObjectName
226
	 */
227 5
	public function getFullModelObjectName(ActionSchema $action) {
228 5
		$database = $this->getDatabase();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
229 5
		$modelName = $this->getModelNameByAction($action);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
230 5
		$model = $this->getModel($modelName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
231 5
		$modelObjectName = $model->getPhpName();
232
233 5
		return $database->getNamespace() . '\\' . $modelObjectName;
234
	}
235
	
236
	/**
237
	 * Returns the operation (verb) of the action (if existent)
238
	 * 
239
	 * @param ActionSchema $action
240
	 * @return string|null
241
	 */
242 View Code Duplication
	public function getOperationByAction(ActionSchema $action) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
243
		$actionName = $action->getName();
244
		$operation = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
245
		if (($pos = strpos($actionName, '-')) !== false) {
246
			$operation = substr($actionName, $pos + 1);
247
		}
248
		return $operation;
249
	}
250
	
251
	/**
252
	 * Returns wether the given action refers to a model.
253
	 * 
254
	 * Examples:
255
	 * 
256
	 * Action: user-create => model: user
257
	 * Action: recover-password => no model
258
	 * 
259
	 * @param ActionSchema $action
260
	 * @return boolean
261
	 */
262 5
	public function isModelAction(ActionSchema $action) {
263 5
		$modelName = $this->getModelNameByAction($action);
264 5
		return $this->hasModel($modelName);
265
	}
266
	
267
	/**
268
	 * Returns whether this is a crud operation action
269
	 * (create, read, update, delete, list)
270
	 * 
271
	 * @param ActionSchema $action
272
	 * @return boolean
273
	 */
274
	public function isCrudAction(ActionSchema $action) {
275
		$operation = $this->getOperationByAction($action);
276
		
277
		return in_array($operation, ['create', 'read', 'update', 'delete', 'list']);
278
	}
279
}
280