Completed
Push — master ( c7b947...966759 )
by Thomas
08:09
created

ModelService::getModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

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 3
rs 10
ccs 0
cts 3
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace keeko\tools\services;
3
4
use keeko\framework\schema\ActionSchema;
5
use keeko\framework\schema\PackageSchema;
6
use keeko\tools\model\Project;
7
use keeko\tools\model\Relationship;
8
use keeko\tools\model\Relationships;
9
use phootwork\collection\Map;
10
use phootwork\collection\Set;
11
use phootwork\file\Path;
12
use Propel\Generator\Model\Database;
13
use Propel\Generator\Model\Table;
14
15
class ModelService extends AbstractService {
16
17
	private $models = null;
0 ignored issues
show
Unused Code introduced by
The property $models is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
	private $schema = null;
0 ignored issues
show
Unused Code introduced by
The property $schema is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
	
20
	/** @var Database */
21
	private $database = null;
0 ignored issues
show
Unused Code introduced by
The property $database is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
	
23
	private $relationships = null;
0 ignored issues
show
Unused Code introduced by
The property $relationships is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
	
25
	private $reader = null;
26
	
27
	public function read(Project $project = null) {
28
		if ($project === null) {
29
			if ($this->project->hasSchemaFile()) {
30
				$project = $this->project;
31 12
			} else {
32 12
				$project = new Project($this->project->getRootPath() . '/vendor/keeko/core');
33 12
			}
34 12
		}
35 12
		$this->reader = new ModelReader($project, $this->service);
36
	}
37 12
	
38 12
	/**
39
	 * @return ModelReader
40 12
	 */
41 12
	private function getReader() {
42 12
		if ($this->reader === null) {
43 12
			$this->read();
44 12
		}
45
		return $this->reader;
46 12
	}
47 12
48
	/**
49 12
	 * Returns the propel schema. The three locations, where the schema is looked up in:
50
	 *
51
	 * @return string|null the path to the schema
52
	 */
53 12
	public function getSchema() {
54
		if ($this->getReader()->getProject()->hasSchemaFile()) {
55 12
			return $this->getReader()->getProject()->getSchemaFileName();
56
		}
57
58 3
		return null;
59 3
	}
60
	
61
	public function isCoreSchema() {
62
		return $this->getReader()->getProject()->getPackage()->getFullName() == 'keeko/core';
63
	}
64
	
65
	public function hasSchema() {
66
		return $this->getReader()->getProject()->hasSchemaFile();
67
	}
68
69
	/**
70
	 * Returns the propel database
71
	 *
72 12
	 * @return Database
73 12
	 */
74 12
	public function getDatabase() {
75 12
		return $this->getReader()->getDatabase();
76 12
	}
77 12
	
78
	/**
79 12
	 * Returns the tableName for a given name
80
	 *
81
	 * @param String $name tableName or modelName
82
	 * @return String tableName
83
	 */
84
	public function getTableName($name) {
85
		return $this->getReader()->getTableName($name);
86
	}
87
	
88 11
	/**
89 11
	 * Returns all model names
90 11
	 *
91 11
	 * @return Set
92 11
	 */
93
	public function getModelNames() {
94 11
		return $this->getReader()->getModelNames();
95
	}
96
	
97
	/**
98
	 * Returns the propel models from the database, where table namespace matches package namespace
99
	 *
100
	 * @return Map
101
	 */
102
	public function getModels() {
103
		return $this->getReader()->getModels();
104
	}
105
106
	/**
107
	 * Returns the model for the given name
108
	 *
109
	 * @param String $name modelName or tableName
110
	 * @return Table
111
	 */
112
	public function getModel($name) {
113
		return $this->getReader()->getModel($name);
114
	}
115
116
// 	/**
117 1
// 	 * Returns the model names for a given package
118 1
// 	 * 
119 1
// 	 * @param PackageSchema $package a package to search models for, if omitted global package is used
120 1
// 	 * @return array array with string of model names
121
// 	 */
122 1
// 	public function getPackageModelNames(PackageSchema $package = null) {
123
// 		if ($package === null) {
124 1
// 			$package = $this->packageService->getPackage();
125 1
// 		}
126 1
		
127 1
// 		$models = [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
128 1
// 		// if this is a core-module, find the related model
129 1
// 		if ($package->getVendor() == 'keeko' && $this->isCoreSchema()) {
130
// 			$model = $package->getName();
131 1
// 			if ($this->hasModel($model)) {
132
// 				$models [] = $model;
133
// 			}
134
// 		}
135
		
136
// 		// anyway, generate all
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
137
// 		else {
138
// 			foreach ($this->getModels() as $model) {
139
// 				$models [] = $model->getOriginCommonName();
140 7
// 			}
141 7
// 		}
142 7
		
143
// 		return $models;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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
// 	}
145
	
146
	/**
147 7
	 * Checks whether the given model exists
148
	 *
149 7
	 * @param String $name tableName or modelName
150
	 * @return boolean
151
	 */
152
	public function hasModel($name) {
153
		return $this->getReader()->hasModel($name);
154
	}
155
	
156
	/**
157
	 * Parses the model name from a given action name
158 8
	 *
159 8
	 * @param ActionSchema $action
160
	 * @return String modelName
161
	 */
162
	public function getModelNameByAction(ActionSchema $action) {
163
		$actionName = $action->getName();
164
		$modelName = null;
165
		if (($pos = strpos($actionName, '-')) !== false) {
166
			$modelName = substr($actionName, 0, $pos);
167 1
		}
168 1
		return $modelName;
169 1
	}
170 1
171 1
	/**
172 1
	 * Returns the full model object name, including namespace
173 1
	 * 
174 1
	 * @param ActionSchema $action
175 1
	 * @return String fullModelObjectName
176 1
	 */
177
	public function getFullModelObjectName(ActionSchema $action) {
178 1
		$database = $this->getDatabase();
179 1
		$modelName = $this->getModelNameByAction($action);
180
		$model = $this->getModel($modelName);
181 1
		$modelObjectName = $model->getPhpName();
182
183
		return $database->getNamespace() . '\\' . $modelObjectName;
184
	}
185
	
186
	/**
187
	 * Returns wether the given action refers to a model.
188
	 * 
189
	 * Examples:
190
	 * 
191
	 * Action: user-create => model: user
192
	 * Action: recover-password => no model
193
	 * 
194
	 * @param ActionSchema $action
195
	 * @return boolean
196
	 */
197
	public function isModelAction(ActionSchema $action) {
198
		$modelName = $this->getModelNameByAction($action);
199
		return $this->hasModel($modelName);
200
	}
201
202
	/**
203
	 * Returns all model relationships.
204
	 * 
205
	 * @param Table $model
206
	 * @return Relationships
207
	 */
208
	public function getRelationships(Table $model) {
209
		return $this->getReader()->getRelationships($model);
210
	}
211
	
212 10
	/**
213 10
	 * Returns a relationship for a given related type name on a given model
214 10
	 * 
215 10
	 * @param Table $model
216 10
	 * @param string $relatedTypeName
217 10
	 * @return Relationship
218 10
	 */
219
	public function getRelationship(Table $model, $relatedTypeName) {
220
		$relationships = $this->getRelationships($model);
221
		return $relationships->get($relatedTypeName);
222
	}
223
}
224