Task_Jam_Model   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 100
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build_validation() 0 6 1
B _execute() 0 81 7
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