ModelService::hasModel()   A
last analyzed

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