Task_Jam_Model::_execute()   B
last analyzed

Complexity

Conditions 7
Paths 48

Size

Total Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 7.4812
c 0
b 0
f 0
cc 7
nc 48
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
/**
4
 * Generate a model file
5
 *
6
 * options:
7
 *
8
 *  - name: required, the name of the widget, eg product_purchase
9
 *  - author: set the author
10
 *  - module: which module to put this in, leave blank to put it in application
11
 *  - force: boolean flag - overwrite existing files
12
 *  - collection: boolean flag - optionally generate the 'collection' file for this model
13
 *  - unlink: boolean flag - unlink the generated files
14
 *
15
 * @package Jam tart
16
 * @author Ivan Kerin
17
 * @copyright  (c) 2011-2013 Despark Ltd.
18
 */
19
class Task_Jam_Model extends Minion_Task {
20
21
	protected $_options = array(
22
		'name' => FALSE,
23
		'module' => FALSE,
24
		'author' => FALSE,
25
		'force' => FALSE,
26
		'unlink' => FALSE,
27
		'collection' => FALSE,
28
	);
29
30
	public function build_validation(Validation $validation)
31
	{
32
		return parent::build_validation($validation)
33
			->rule('module', 'in_array', array(':value', array_keys(Kohana::modules())))
34
			->rule('name', 'not_empty');;
35
	}
36
37
	protected function _execute(array $options)
38
	{
39
		$module_name = $options['module'] ?: 'applicaiton';
40
		$module_dir = $options['module'] ? Arr::get(Kohana::modules(), $module_name) : APPPATH;
41
42
		$author = $options['author'] ?: '-';
43
44
		$name = $options['name'];
45
		$title = Jam::capitalize_class_name(str_replace('_', ' ', $name));
46
		$path = str_replace('_', DIRECTORY_SEPARATOR, $title);
47
48
		$dir = $module_dir.'classes'.DIRECTORY_SEPARATOR.'Model';
49
		$file = $dir.DIRECTORY_SEPARATOR.$path.EXT;
50
		$class ='Model_'.str_replace(' ', '_', $title);
51
52
		if ( ! is_dir(dirname($file)))
53
		{
54
			mkdir(dirname($file), 0777, TRUE);
55
		}
56
57
		$content = <<<MODEL
58
<?php defined('SYSPATH') OR die('No direct script access.');
59
60
/**
61
 * Jam Model: $title
62
 *
63
 * @package $module_name
64
 * @author $author
65
 * @copyright  (c) 2011-2013 Despark Ltd.
66
 */
67
class {$class} extends Jam_Model {
68
69
	public static function initialize(Jam_Meta \$meta)
70
	{
71
		\$meta
72
			->associations(array(
73
			))
74
75
			->fields(array(
76
				'id' => Jam::field('primary'),
77
				'name' => Jam::field('string'),
78
			))
79
80
			->validator('name', array('present' => TRUE));
81
	}
82
}
83
MODEL;
84
85
		Minion_Jam_Generate::modify_file($file, $content, $options['force'] !== FALSE, $options['unlink'] !== FALSE);
86
87
		if ($options['collection'] !== FALSE)
88
		{
89
90
			$dir = $module_dir.'classes'.DIRECTORY_SEPARATOR.'model'.DIRECTORY_SEPARATOR.'collection';
91
			$file = $dir.DIRECTORY_SEPARATOR.$path.EXT;
92
			$class ='Model_Collection_'.str_replace(' ', '_', $title);
93
94
			if ( ! is_dir(dirname($file)))
95
			{
96
				mkdir(dirname($file), 0777, TRUE);
97
			}
98
99
			$content = <<<COLLECTION
100
<?php defined('SYSPATH') OR die('No direct script access.');
101
102
/**
103
 * Collection for $title
104
 *
105
 * @package $module_name
106
 * @author $author
107
 * @copyright  (c) 2011-2013 Despark Ltd.
108
 */
109
class {$class} extends Jam_Query_Builder_Collection {
110
111
112
}
113
COLLECTION;
114
			Minion_Jam_Generate::modify_file($file, $content, $options['force'] !== FALSE, $options['unlink'] !== FALSE);
115
		}
116
117
	}
118
}
119