Completed
Push — master ( 5d27f2...d6c5c0 )
by Thomas
08:45
created

ActionNameGenerator::generate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 6
eloc 13
nc 6
nop 2
1
<?php
2
namespace keeko\tools\generator;
3
4
use Propel\Generator\Model\Table;
5
use keeko\tools\model\Relationship;
6
use keeko\framework\utils\NameUtils;
7
8
/**
9
 * Generates action names
10
 * 
11
 * @author gossi
12
 */
13
class ActionNameGenerator {
14
	
15
	/**
16
	 * Generates an action name for a given model and a type
17
	 * 
18
	 * @param string $type
19
	 * @param Table $model
20
	 * @return string|null Returns the name or null if the type is unknown
21
	 */
22
	public static function generate($type, Table $model) {
23
		switch ($type) {
24
			case 'create':
25
				return self::generateCreate($model);
26
			
27
			case 'read':
28
				return self::generateRead($model);
29
				
30
			case 'list':
31
				return self::generateList($model);
32
				
33
			case 'update':
34
				return self::generateUpdate($model);
35
				
36
			case 'delete':
37
				return self::generateDelete($model);
38
		}
39
		
40
		return null;
41
	}
42
43
	/**
44
	 * Generates the name for a create action
45
	 * 
46
	 * @param Table $model
47
	 * @return string
48
	 */
49
	public static function generateCreate(Table $model) {
50
		return $model->getOriginCommonName() . '-create';
51
	}
52
53
	/**
54
	 * Generates the name for a read action
55
	 * 
56
	 * @param Table $model
57
	 * @return string
58
	 */
59
	public static function generateRead(Table $model) {
60
		return $model->getOriginCommonName() . '-read';
61
	}
62
63
	/**
64
	 * Generates the name for a list action
65
	 * 
66
	 * @param Table $model
67
	 * @return string
68
	 */
69
	public static function generateList(Table $model) {
70
		return $model->getOriginCommonName() . '-list';
71
	}
72
73
	/**
74
	 * Generates the name for an update action
75
	 * 
76
	 * @param Table $model
77
	 * @return string
78
	 */
79
	public static function generateUpdate(Table $model) {
80
		return $model->getOriginCommonName() . '-update';
81
	}
82
83
	/**
84
	 * Generates the name for a delete action
85
	 * 
86
	 * @param Table $model
87
	 * @return string
88
	 */
89
	public static function generateDelete(Table $model) {
90
		return $model->getOriginCommonName() . '-delete';
91
	}
92
	
93
	/**
94
	 * Generates the name for a relationship action with a given relationship and type
95
	 * 
96
	 * @param string $type
97
	 * @param Relationship $relationship
98
	 * @return string|null Returns the name or null if the type is unknown
99
	 */
100
	public static function generateRelationship($type, Relationship $relationship) {
101
		switch ($type) {
102
			case 'read':
103
				return self::generateRelationshipRead($relationship);
104
				
105
			case 'update':
106
				return self::generateRelationshipUpdate($relationship);
107
				
108
			case 'add':
109
				return self::generateRelationshipAdd($relationship);
110
				
111
			case 'remove':
112
				return self::generateRelationshipRemove($relationship);
113
		}
114
		
115
		return null;
116
	}
117
118
	/**
119
	 * Generates the name for a relationship read action
120
	 * 
121
	 * @param Relationship $relationship
122
	 * @return string
123
	 */
124
	public static function generateRelationshipRead(Relationship $relationship) {
125
		$model = $relationship->getModel();
126
		$related = NameUtils::toSnakeCase($relationship->getRelatedTypeName());
127
		return sprintf('%s-to-%s-relationship-read', 
128
			$model->getOriginCommonName(), $related);
129
	}
130
131
	/**
132
	 * Generates the name for a relationship update action
133
	 * 
134
	 * @param Relationship $relationship
135
	 * @return string
136
	 */
137
	public static function generateRelationshipUpdate(Relationship $relationship) {
138
		$model = $relationship->getModel();
139
		$related = NameUtils::toSnakeCase($relationship->getRelatedTypeName());
140
		return sprintf('%s-to-%s-relationship-update',
141
			$model->getOriginCommonName(), $related);
142
	}
143
144
	/**
145
	 * Generates the name for a relationship add action
146
	 * 
147
	 * @param Relationship $relationship
148
	 * @return string
149
	 */
150
	public static function generateRelationshipAdd(Relationship $relationship) {
151
		$model = $relationship->getModel();
152
		$related = NameUtils::toSnakeCase($relationship->getRelatedTypeName());
153
		return sprintf('%s-to-%s-relationship-add',
154
			$model->getOriginCommonName(), $related);
155
	}
156
157
	/**
158
	 * Generates the name for a relationship remove action
159
	 * 
160
	 * @param Relationship $relationship
161
	 * @return string
162
	 */
163
	public static function generateRelationshipRemove(Relationship $relationship) {
164
		$model = $relationship->getModel();
165
		$related = NameUtils::toSnakeCase($relationship->getRelatedTypeName());
166
		return sprintf('%s-to-%s-relationship-remove',
167
			$model->getOriginCommonName(), $related);
168
	}
169
}