Completed
Pull Request — master (#7)
by Markus
06:25
created

GenerateControllerMethodsTask::main()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 8
nop 0
dl 0
loc 42
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
// +---------------------------------------------------------------------------+
4
// | This file is part of the Agavi package.                                   |
5
// | Copyright (c) 2005-2010 the Agavi Project.                                |
6
// |                                                                           |
7
// | For the full copyright and license information, please view the LICENSE   |
8
// | file that was distributed with this source code. You can also view the    |
9
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
10
// |   vi: set noexpandtab:                                                    |
11
// |   Local Variables:                                                        |
12
// |   indent-tabs-mode: t                                                     |
13
// |   End:                                                                    |
14
// +---------------------------------------------------------------------------+
15
16
require_once(__DIR__ . '/AgaviTask.php');
17
18
/**
19
 * Creates the methods to handle requests for an agavi controller.
20
 *
21
 * @package    agavi
22
 * @subpackage build
23
 *
24
 * @author     Felix Gilcher <[email protected]>
25
 * @copyright  Authors
26
 * @copyright  The Agavi Project
27
 *
28
 * @since      1.1.0
29
 *
30
 * @version    $Id$
31
 */
32
class GenerateControllerMethodsTask extends AgaviTask
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
33
{
34
	/**
35
	 * @var          string The property to modify.
36
	 */
37
	protected $property = null;
38
	
39
	/**
40
	 * @var          array the list of request methods to generate handlers for
41
	 */
42
	protected $methods = array();
43
	
44
	/**
45
	 * @var          boolean whether the generated controller should be simple
46
	 */
47
	protected $isSimple = false;
48
	
49
	/**
50
	 * @var          string the template to use for the handler methods
51
	 */
52
	protected $requestMethodTemplate;
53
	
54
	/**
55
	 * @var          string the template to use for the isSimple method
56
	 */
57
	protected $simpleMethodTemplate;
58
	
59
	/**
60
	 * Sets the property that this task will modify.
61
	 *
62
	 * @param      string The property to modify.
63
	 */
64
	public function setProperty($property)
65
	{
66
		$this->property = $property;
67
	}
68
	
69
	/**
70
	 * Sets the methods to generate code for.
71
	 *
72
	 * @param      string a space separated list of methodnames.
73
	 */
74
	public function setMethods($methodNames)
75
	{
76
		if ("" == trim($methodNames)) {
77
			$this->methods = array();
78
		} else {
79
			$this->methods = explode(" ", $methodNames);
80
		}		
81
	}
82
	
83
	/**
84
	 * Sets if the controller should be simple.
85
	 *
86
	 * @param      boolean true if the controller is simple.
87
	 */
88
	public function setSimple($flag)
89
	{
90
		$this->isSimple = 'y' == $flag;
91
	}
92
	
93
	/**
94
	 * Sets the template to use for the request method handling methods.
95
	 *
96
	 * @param      string the template path
97
	 */
98
	public function setRequestMethodTemplate($path)
99
	{
100
		$this->requestMethodTemplate = $path;
101
	}
102
	
103
	/**
104
	 * Sets the template to use for the isSimple() method.
105
	 *
106
	 * @param      string the template path
107
	 */
108
	public function setSimpleMethodTemplate($path)
109
	{
110
		$this->simpleMethodTemplate = $path;
111
	}
112
	
113
	/**
114
	 * Executes the task.
115
	 */
116
	public function main()
117
	{
118
		if($this->property === null) {
119
			throw new \Agavi\Build\Exception\BuildException('The property attribute must be specified');
120
		}
121
		
122
		if(count($this->methods) > 0 && $this->isSimple) {
123
			throw new \Agavi\Build\Exception\BuildException('A controller cannot serve request methods and be simple at the same time.');
124
		}
125
		
126
		if($this->requestMethodTemplate === null || !is_readable($this->requestMethodTemplate)) {
127
			throw new \Agavi\Build\Exception\BuildException(
128
				sprintf(
129
					'The requestMethodTemplate attribute must be specified and must point to a readable template file. Current value is "%1$s".',
130
					$this->requestMethodTemplate
131
				)
132
			);
133
		}
134
		
135
		if($this->simpleMethodTemplate === null || !is_readable($this->simpleMethodTemplate)) {
136
			throw new \Agavi\Build\Exception\BuildException(
137
				sprintf(
138
					'The simpleMethodTemplate attribute must be specified and must point to a readable template file. Current value is "%1$s".',
139
					$this->simpleMethodTemplate
140
				)
141
			);
142
		}
143
		
144
		$template = file_get_contents($this->requestMethodTemplate);
145
		
146
		$methodDeclarations = '';
147
		foreach($this->methods as $methodName) {
148
			$methodDeclarations .= str_replace('%%METHOD_NAME%%', ucfirst($methodName), $template);
149
		}
150
		
151
		if($this->isSimple) {
152
			$methodDeclarations .= file_get_contents($this->simpleMethodTemplate);
153
		}
154
	
155
		$this->project->setUserProperty($this->property, $methodDeclarations);
156
		return;
157
	}
158
}
159
160
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...