Completed
Push — master ( da62ab...2e0a22 )
by Thomas
09:51
created

ModelService::isModelAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 2
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
use keeko\core\schema\PackageSchema;
12
13
class ModelService extends AbstractService {
14
15
	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...
16
	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...
17
	private $namespace = null;
18
	
19
	/** @var Database */
20
	private $database = null;
21
22
	/**
23
	 * Returns the propel schema. The three locations, where the schema is looked up in:
24
	 *
25
	 * 1. --schema option (if available)
26
	 * 2. database/schema.xml
27
	 * 3. core/database/schema.xml
28
	 *
29
	 * @throws \RuntimeException
30
	 * @return string the path to the schema
31 12
	 */
32 12
	public function getSchema() {
33 12
		$input = $this->io->getInput();
34 12
		if ($this->schema === null) {
35 12
			$workDir = $this->service->getProject()->getRootPath();
36
			$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...
37 12
			$schemas = [
38 12
				$input->hasOption('schema') ? $input->getOption('schema') : '',
39
				$workDir . '/database/schema.xml',
40 12
				$workDir . '/core/database/schema.xml',
41 12
				$workDir . '/vendor/keeko/core/database/schema.xml'
42 12
			];
43 12
			foreach ($schemas as $path) {
44 12
				if (file_exists($path)) {
45
					$schema = $path;
46 12
					break;
47 12
				}
48
			}
49 12
			$this->schema = $schema;
50
				
51
			if ($schema === null) {
52
				$locations = implode(', ', $schemas);
53 12
				throw new \RuntimeException(sprintf('Can\'t find schema in these locations: %s', $locations));
54
			}
55 12
		}
56
57
		return $this->schema;
58 3
	}
59 3
	
60
	public function isCoreSchema() {
61
		return strpos($this->getSchema(), 'core') !== false;
62
	}
63
	
64
	public function hasSchema() {
65
		$vendorName = $this->packageService->getPackage()->getVendor();
66
		return $this->getSchema() !== null && ($this->isCoreSchema() ? $vendorName == 'keeko' : true);
67
	}
68
	
69
	/**
70
	 * Returns the propel database
71
	 *
72 12
	 * @return Database
73 12
	 */
74 12
	public function getDatabase() {
75 12
		if ($this->database === null) {
76 12
			$builder = new QuickBuilder();
77 12
			$builder->setSchema(file_get_contents($this->getSchema()));
78
			$this->database = $builder->getDatabase();
79 12
		}
80
	
81
		return $this->database;
82
	}
83
	
84
	/**
85
	 * Returns the tableName for a given name
86
	 *
87
	 * @param String $name tableName or modelName
88 11
	 * @return String tableName
89 11
	 */
90 11
	public function getTableName($name) {
91 11
		$db = $this->getDatabase();
92 11
		if (!Text::create($name)->startsWith($db->getTablePrefix())) {
93
			$name = $db->getTablePrefix() . $name;
94 11
		}
95
	
96
		return $name;
97
	}
98
	
99
	/**
100
	 * Returns all model names
101
	 *
102
	 * @return String[] an array of modelName
103
	 */
104
	public function getModelNames() {
105
		$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...
106
		$database = $this->getDatabase();
107
		foreach ($database->getTables() as $table) {
108
			$names[] = $table->getOriginCommonName();
109
		}
110
	
111
		return $names;
112
	}
113
	
114
	/**
115
	 * Returns the propel models from the database, where table namespace matches package namespace
116
	 *
117 1
	 * @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...
118 1
	 */
119 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...
120 1
		if ($this->models === null) {
121
			$namespace = str_replace('\\\\', '\\', $this->getRootNamespace() . '\\model');
122 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...
123
	
124 1
			$this->models = new ArrayList();
125 1
	
126 1
			foreach ($propel->getTables() as $table) {
127 1
				if (!$table->isCrossRef() && $table->getNamespace() == $namespace) {
128 1
					$this->models->add($table);
129 1
				}
130
			}
131 1
		}
132
	
133
		return $this->models;
134
	}
135
136
	/**
137
	 * Returns the model for the given name
138
	 *
139
	 * @param String $name modelName or tableName
140 7
	 * @return Table
141 7
	 */
142 7
	public function getModel($name) {
143
		$tableName = $this->getTableName($name);
144
		$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...
145
// 		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...
146
// 		foreach ($db->getTables() as $table) {
147 7
// 			echo $table->getName();
148
// 		}
149 7
		$table = $db->getTable($tableName);
150
	
151
		return $table;
152
	}
153
	
154
	/**
155
	 * Returns the model names for a given package
156
	 * 
157
	 * @param PackageSchema $package a package to search models for, if omitted global package is used
158 8
	 * @return array array with string of model names
159 8
	 */
160
	public function getPackageModelNames(PackageSchema $package = null) {
161
		if ($package === null) {
162
			$package = $this->packageService->getPackage();
163
		}
164
		
165
		$models = [];
166
		// if this is a core-module, find the related model
167 1
		if ($package->getVendor() == 'keeko' && $this->isCoreSchema()) {
168 1
			$model = $package->getName();
169 1
			if ($this->hasModel($model)) {
170 1
				$models []= $model;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

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

will have no issues, while

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

will report issues in lines 1 and 2.

Loading history...
171 1
			}
172 1
		}
173 1
		
174 1
		// anyway, generate all
175 1
		else {
176 1
			foreach ($this->getModels() as $model) {
177
				$models []= $model->getOriginCommonName();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

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

will have no issues, while

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

will report issues in lines 1 and 2.

Loading history...
178 1
			}
179 1
		}
180
		
181 1
		return $models;
182
	}
183
	
184
	/**
185
	 * Checks whether the given model exists
186
	 *
187
	 * @param String $name tableName or modelName
188
	 * @return boolean
189
	 */
190
	public function hasModel($name) {
191
		return $this->getDatabase()->hasTable($this->getTableName($name), true);
192
	}
193
194
	/**
195
	 * Returns the root namespace for this package
196
	 *
197
	 * @return string the namespace
198
	 */
199
	public function getRootNamespace() {
200
		if ($this->namespace === null) {
201
			$input = $this->io->getInput();
202
			$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...
203
				? $input->getOption('namespace')
204
				: null;
205
			if ($ns === null) {
206
				$package = $this->service->getPackageService()->getPackage();
207
				$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...
208
			}
209
				
210
			$this->namespace = $ns;
211
		}
212 10
	
213 10
		return $this->namespace;
214 10
	}
215 10
216 10
	/**
217 10
	 * Retrieves the model name for the given package in two steps:
218 10
	 * 
219
	 * 1. Check if it is passed as cli parameter
220
	 * 2. Retrieve it from the package name
221
	 *
222
	 * @return String
223
	 */
224
	public function getModelName() {
225
		$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...
226
		$modelName = $input->hasOption('model') ? $input->getOption('model') : null;
227 5
		if ($modelName === null && $this->isCoreSchema()) {
228 5
			$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...
229 5
			$packageName = $package->getName();
230 5
231 5
			if ($this->hasModel($packageName)) {
232
				$modelName = $packageName;
233 5
			}
234
		}
235
		return $modelName;
236
	}
237
	
238
	/**
239
	 * Parses the model name from a given action name
240
	 *
241
	 * @param ActionSchema $action
242
	 * @return String modelName
243
	 */
244 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...
245
		$actionName = $action->getName();
246
		$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...
247
		if (($pos = strpos($actionName, '-')) !== false) {
248
			$modelName = substr($actionName, 0, $pos);
249
		}
250
		return $modelName;
251
	}
252
253
	/**
254
	 * Returns the full model object name, including namespace
255
	 * 
256
	 * @param ActionSchema $action
257
	 * @return String fullModelObjectName
258
	 */
259
	public function getFullModelObjectName(ActionSchema $action) {
260
		$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...
261
		$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...
262 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...
263 5
		$modelObjectName = $model->getPhpName();
264 5
265
		return $database->getNamespace() . '\\' . $modelObjectName;
266
	}
267
	
268
	/**
269
	 * Returns the operation (verb) of the action (if existent)
270
	 * 
271
	 * @param ActionSchema $action
272
	 * @return string|null
273
	 */
274 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...
275
		$actionName = $action->getName();
276
		$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...
277
		if (($pos = strpos($actionName, '-')) !== false) {
278
			$operation = substr($actionName, $pos + 1);
279
		}
280
		return $operation;
281
	}
282
	
283
	/**
284
	 * Returns wether the given action refers to a model.
285
	 * 
286
	 * Examples:
287
	 * 
288
	 * Action: user-create => model: user
289
	 * Action: recover-password => no model
290
	 * 
291
	 * @param ActionSchema $action
292
	 * @return boolean
293
	 */
294
	public function isModelAction(ActionSchema $action) {
295
		$modelName = $this->getModelNameByAction($action);
296
		return $this->hasModel($modelName);
297
	}
298
	
299
	/**
300
	 * Returns whether this is a crud operation action
301
	 * (create, read, update, delete, list)
302
	 * 
303
	 * @param ActionSchema $action
304
	 * @return boolean
305
	 */
306
	public function isCrudAction(ActionSchema $action) {
307
		$operation = $this->getOperationByAction($action);
308
		
309
		return in_array($operation, ['create', 'read', 'update', 'delete', 'list']);
310
	}
311
}
312