Completed
Branch develop (aca1c1)
by
unknown
24:57
created

skeleton_webservice_server.php ➔ createSkeleton()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 54
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
nc 20
nop 2
dl 0
loc 54
rs 8.7449
c 0
b 0
f 0

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
2
/* Copyright (C) 2006-2011 Laurent Destailleur  <[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 *       \file       htdocs/webservices/server_skeleton.php
20
 *       \brief      File that is entry point to call Dolibarr WebServices
21
 *       \version    $Id: server_skeleton.php,v 1.7 2010/12/19 11:49:37 eldy Exp $
22
 */
23
24
// This is to make Dolibarr working with Plesk
25
set_include_path($_SERVER['DOCUMENT_ROOT'].'/htdocs');
26
27
require_once("../master.inc.php");
28
require_once(NUSOAP_PATH.'/nusoap.php');		// Include SOAP
29
require_once(DOL_DOCUMENT_ROOT."/core/lib/ws.lib.php");
30
require_once(DOL_DOCUMENT_ROOT."/skeleton/class/skeleton.class.php");
31
32
33
dol_syslog("Call Skeleton webservices interfaces");
34
35
// Enable and test if module web services is enabled
36
if (empty($conf->global->MAIN_MODULE_WEBSERVICES))
37
{
38
    $langs->load("admin");
39
    dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
40
    print $langs->trans("WarningModuleNotActive",'WebServices').'.<br><br>';
41
    print $langs->trans("ToActivateModule");
42
    exit;
43
}
44
45
// Create the soap Object
46
$server = new nusoap_server();
47
$server->soap_defencoding='UTF-8';
48
$server->decode_utf8=false;
49
$ns='http://www.dolibarr.org/ns/';
50
$server->configureWSDL('WebServicesDolibarrSkeleton',$ns);
51
$server->wsdl->schemaTargetNamespace=$ns;
52
53
54
// Define WSDL Authentication object
55
$server->wsdl->addComplexType(
56
    'authentication',
57
    'complexType',
58
    'struct',
59
    'all',
60
    '',
61
    array(
62
        'dolibarrkey' => array('name'=>'dolibarrkey','type'=>'xsd:string'),
63
    	'sourceapplication' => array('name'=>'sourceapplication','type'=>'xsd:string'),
64
    	'login' => array('name'=>'login','type'=>'xsd:string'),
65
    	'password' => array('name'=>'password','type'=>'xsd:string'),
66
        'entity' => array('name'=>'entity','type'=>'xsd:string'),
67
    )
68
);
69
70
// Define WSDL Return object
71
$server->wsdl->addComplexType(
72
    'result',
73
    'complexType',
74
    'struct',
75
    'all',
76
    '',
77
    array(
78
        'result_code' => array('name'=>'result_code','type'=>'xsd:string'),
79
        'result_label' => array('name'=>'result_label','type'=>'xsd:string'),
80
    )
81
);
82
83
// Define other specific objects
84
$server->wsdl->addComplexType(
85
    'skeleton',
86
    'complexType',
87
    'struct',
88
    'all',
89
    '',
90
    array(
91
	    'prop1'=>'xxx',
92
		'prop2'=>'xxx',
93
    //...
94
    )
95
);
96
97
98
99
// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
100
// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
101
// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
102
$styledoc='rpc';       // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
103
$styleuse='encoded';   // encoded/literal/literal wrapped
104
// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
105
106
107
// Register WSDL
108
$server->register(
109
    'getSkeleton',
110
    // Entry values
111
    array('authentication'=>'tns:authentication','id'=>'xsd:string','ref'=>'xsd:string','ref_ext'=>'xsd:string'),
112
    // Exit values
113
    array('result'=>'tns:result','skeleton'=>'tns:skeleton'),
114
    $ns,
115
    $ns.'#getSkeleton',
116
    $styledoc,
117
    $styleuse,
118
    'WS to get skeleton'
119
);
120
121
// Register WSDL
122
$server->register(
123
	'createSkeleton',
124
	// Entry values
125
	array('authentication'=>'tns:authentication','skeleton'=>'tns:skeleton'),
126
	// Exit values
127
	array('result'=>'tns:result','id'=>'xsd:string'),
128
	$ns,
129
	$ns.'#createSkeleton',
130
	$styledoc,
131
	$styleuse,
132
	'WS to create a skeleton'
133
);
134
135
136
137
138
/**
139
 * Get Skeleton
140
 *
141
 * @param	array		$authentication		Array of authentication information
142
 * @param	int			$id					Id of object
143
 * @param	string		$ref				Ref of object
144
 * @param	string		$ref_ext			Ref external of object
145
 * @return	mixed
146
 */
147
function getSkeleton($authentication,$id,$ref='',$ref_ext='')
148
{
149
    global $db,$conf,$langs;
150
151
    dol_syslog("Function: getSkeleton login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
152
153
    if ($authentication['entity']) $conf->entity=$authentication['entity'];
154
155
    // Init and check authentication
156
    $objectresp=array();
157
    $errorcode='';$errorlabel='';
158
    $error=0;
159
    $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
160
    // Check parameters
161
    if (! $error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
162
    {
163
        $error++;
164
        $errorcode='BAD_PARAMETERS'; $errorlabel="Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
165
    }
166
167
    if (! $error)
168
    {
169
        $fuser->getrights();
170
171
        if ($fuser->rights->skeleton->read)
172
        {
173
            $skeleton=new Skeleton($db);
174
            $result=$skeleton->fetch($id,$ref,$ref_ext);
175
            if ($result > 0)
176
            {
177
                // Create
178
                $objectresp = array(
179
			    	'result'=>array('result_code'=>'OK', 'result_label'=>''),
180
			        'skeleton'=>array(
181
				    'prop1'=>$skeleton->prop1,
182
				    'prop2'=>$skeleton->prop2,
183
                    //...
184
                    )
185
                );
186
            }
187
            else
188
            {
189
                $error++;
190
                $errorcode='NOT_FOUND'; $errorlabel='Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
191
            }
192
        }
193
        else
194
        {
195
            $error++;
196
            $errorcode='PERMISSION_DENIED'; $errorlabel='User does not have permission for this request';
197
        }
198
    }
199
200
    if ($error)
201
    {
202
        $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
203
    }
204
205
    return $objectresp;
206
}
207
208
209
/**
210
 * Create Skeleton
211
 *
212
 * @param	array		$authentication		Array of authentication information
213
 * @param	Skeleton	$skeleton		    $skeleton
214
 * @return	array							Array result
215
 */
216
function createSkeleton($authentication,$skeleton)
217
{
218
	global $db,$conf,$langs;
219
220
	$now=dol_now();
221
222
	dol_syslog("Function: createSkeleton login=".$authentication['login']);
223
224
	if ($authentication['entity']) $conf->entity=$authentication['entity'];
225
226
	// Init and check authentication
227
	$objectresp=array();
228
	$errorcode='';$errorlabel='';
229
	$error=0;
230
	$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
231
	// Check parameters
232
233
234
	if (! $error)
235
	{
236
		$newobject=new Skeleton($db);
237
		$newobject->prop1=$skeleton->prop1;
238
		$newobject->prop2=$skeleton->prop2;
239
		//...
240
241
		$db->begin();
242
243
		$result=$newobject->create($fuser);
244
		if ($result <= 0)
245
		{
246
			$error++;
247
		}
248
249
		if (! $error)
250
		{
251
			$db->commit();
252
			$objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>''),'id'=>$newobject->id,'ref'=>$newobject->ref);
253
		}
254
		else
255
		{
256
			$db->rollback();
257
			$error++;
258
			$errorcode='KO';
259
			$errorlabel=$newobject->error;
260
		}
261
	}
262
263
	if ($error)
264
	{
265
		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
266
	}
267
268
	return $objectresp;
269
}
270
271
// Return the results.
272
$server->service(file_get_contents("php://input"));
273