Intraface_XMLRPC_Shop_Server::checkCredentials()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 5
nop 1
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
ccs 0
cts 27
cp 0
crap 30
1
<?php
2
/**
3
 * WebshopServer
4
 *
5
 * @category XMLRPC_Server
6
 * @package  Intraface_XMLRPC_Shop
7
 * @author   Lars Olesen <[email protected]>
8
 * @version  @package-version@
9
 */
10
require_once 'Intraface/modules/webshop/Webshop.php';
11
require_once 'Intraface/modules/webshop/FeaturedProducts.php';
12
13
class Intraface_XMLRPC_Shop_Server
14
{
15
    private $kernel;
16
    private $webshop;
17
    private $basket;
18
    private $product;
0 ignored issues
show
Unused Code introduced by
The property $product is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
    private $credentials;
20
21
    /**
22
     * Gets a list with products
23
     *
24
     * @param struct $credentials Credentials to use the server
25
     * @param array  $search      Optional search array
26
     *
27
     * @return array
28
     */
29
    public function getProducts($credentials, $search = array())
30
    {
31
        $this->checkCredentials($credentials);
32
33
        $offset = 0;
0 ignored issues
show
Unused Code introduced by
$offset is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
35
        $mixed = array();
36
        if (!empty($search)) {
37
            $mixed = $search;
38
        }
39
40
        $search = '';
0 ignored issues
show
Unused Code introduced by
$search is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
42
        $this->_factoryWebshop();
43
44
        $products = array();
0 ignored issues
show
Unused Code introduced by
$products is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
46
        $area = '';
47
48
        if (!empty($mixed['area'])) {
49
            $area = $mixed['area'];
50
        }
51
52
        $product = new Product($this->webshop->kernel);
53
54
        if (!isset($mixed['use_paging']) || $mixed['use_paging'] == 'true') {
55
            $product->getDBQuery()->usePaging('paging');
56
        }
57
58
59
        // sublevel has to be used so other searches are not overwritten
60
        $product->getDBQuery()->storeResult('use_stored', 'webshop_' . $area . '_' .  md5($this->credentials['session_id']), 'sublevel');
61
        $debug2 = serialize($mixed);
62
        if (isset($mixed['offset']) and array_key_exists('offset', $mixed) and is_numeric($mixed['offset'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
63
            $product->getDBQuery()->useStored(true);
64
            $product->getDBQuery()->setPagingOffset((int)$mixed['offset']);
65
            $debug2 .= 'offset ' . $mixed['offset'];
66
        } elseif (isset($mixed['use_stored']) and array_key_exists('use_stored', $mixed) and $mixed['use_stored'] == 'true') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
67
            $product->getDBQuery()->useStored(true);
68
            $debug2 .= 'use_stored true';
69
        } else {
70
            if (isset($mixed['search']) and array_key_exists('search', $mixed) and !empty($mixed['search'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
71
                $product->getDBQuery()->setFilter('search', $mixed['search']);
72
                $debug2 .= 'search ' . $mixed['search'];
73
            }
74
75
            if (isset($mixed['keywords']) and array_key_exists('keywords', $mixed) and !empty($mixed['keywords'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
76
                $product->getDBQuery()->setFilter('keywords', $mixed['keywords']);
77
                $debug2 .= 'keyword ' . $mixed['keywords'];
78
            }
79
80
            if (isset($mixed['ids']) and array_key_exists('ids', $mixed) and is_array($mixed['ids'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
81
                $product->getDBQuery()->setFilter('ids', $mixed['ids']);
82
                $debug2 .= 'ids ' . implode(', ', $mixed['ids']);
83
            }
84
85
            if (isset($mixed['sorting']) and array_key_exists('sorting', $mixed) and !empty($mixed['sorting'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
86
                $product->getDBQuery()->setFilter('sorting', $mixed['sorting']);
87
                $debug2 .= 'sorting ' . $mixed['sorting'];
88
            }
89
        }
90
91
        return array(
92
            'parameter' => $mixed,
93
            'debug2' => $debug2,
94
            'products' => $product->getList('webshop'),
95
            'paging' => $product->getDBQuery()->getPaging(),
96
            'search' => array(),
97
        );
98
    }
99
100
101
    /**
102
     * Gets one product
103
     *
104
     * @param struct  $credentials Credentials to use the server
105
     * @param integer $id          Product id
106
     *
107
     * @return array
108
     */
109
    public function getProduct($credentials, $id)
110
    {
111
        $this->checkCredentials($credentials);
112
113
        $this->_factoryWebshop();
114
115
        $id = intval($id);
116
117
        if (!is_numeric($id)) {
118
            require_once 'XML/RPC2/Exception.php';
119
            throw new XML_RPC2_FaultException('product id must be an integer', -5);
120
        }
121
122
        $product = new Product($this->kernel, $id);
123
        $product->getPictures();
124
        if (!$product->get('has_variation')) {
125
            // loads stock information to array;
126
            $product->getStock();
127
        }
128
129
        return $product->get();
130
    }
131
132
    /**
133
     * Gets related products
134
     *
135
     * @param struct  $credentials Credentials to use the server
136
     * @param integer $id          Product id
137
     *
138
     * @return array
139
     */
140 View Code Duplication
    public function getRelatedProducts($credentials, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $this->checkCredentials($credentials);
143
144
        $this->_factoryWebshop();
145
146
        $product_id = intval($id);
147
148
        if (!is_numeric($product_id)) {
149
            require_once 'XML/RPC2/Exception.php';
150
            throw new XML_RPC2_FaultException('product id must be an integer', -5);
151
        }
152
153
        $product = new Product($this->kernel, $product_id);
154
        return $product->getRelatedProducts();
155
    }
156
157
   /**
158
     * Gets featured products
159
     *
160
     * Method is experimental and only used by discimport.dk. If you need to use it
161
     * as well, please contact [email protected].
162
     *
163
     * @param struct  $credentials Credentials to use the server
164
     *
165
     * @return array
166
     */
167
    public function getFeaturedProducts($credentials)
168
    {
169
        $related_products = array();
0 ignored issues
show
Unused Code introduced by
$related_products is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
170
171
        $this->checkCredentials($credentials);
172
173
        $this->_factoryWebshop();
174
175
        $db = MDB2::singleton(DB_DSN);
176
177
        if (PEAR::isError($db)) {
178
            require_once 'XML/RPC2/Exception.php';
179
            throw new XML_RPC2_FaultException($db->getMessage() . $db->getUserInfo(), -1);
180
        }
181
182
        $featured = new Intraface_Webshop_FeaturedProducts($this->kernel->intranet, $db);
183
        $all = $featured->getAll();
184
185
        $related_products = array();
186
187
        foreach ($all as $row) {
188
            $product = new Product($this->kernel);
189
            // 265
190
            $product->getDBQuery()->setFilter('keywords', array($row['keyword_id']));
191
192
            $related_products[] = array(
193
                'title' => $row['headline'],
194
                'products' => $product->getList()
195
            );
196
        }
197
198
        return $related_products;
199
    }
200
201
   /**
202
     * Gets product keywords which can be used to sort ones webshop
203
     *
204
     * Method is experimental and only used by nylivsstil.dk. If you need to use it
205
     * as well, please contact [email protected].
206
     *
207
     * @param struct  $credentials Credentials to use the server
208
     *
209
     * @return array with id and keywords
210
     */
211 View Code Duplication
    function getProductKeywords($credentials)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
    {
213
214
        $this->checkCredentials($credentials);
215
        $this->_factoryWebshop();
216
217
        $product = new Product($this->kernel);
218
        $keywords = $product->getKeywordAppender();
219
        return $keywords->getUsedKeywords();
220
    }
221
222
    /**
223
     * Add product to basket
224
     *
225
     * @param struct  $credentials       Credentials to use the server
226
     * @param integer $id                Product id to add
227
     * @param integer $quantity          Optional quantity
228
     * @param string  $text              Extra text to the itemline
229
     * @param integer $product_detail_id Product detail id
230
     *
231
     * @return boolean
232
     */
233
    public function addProductToBasket($credentials, $id, $quantity = 1, $text = '', $product_detail_id = 0)
234
    {
235
        if (is_object($return = $this->checkCredentials($credentials))) {
236
            return $return;
237
        }
238
239
        $this->_factoryWebshop();
240
241
        $product_id = intval($id);
242
243
        if (!is_numeric($product_id)) {
244
            require_once 'XML/RPC2/Exception.php';
245
            throw new XML_RPC2_FaultException('product id must be an integer', -5);
246
        }
247
248
        $text = $this->utf8Decode($text);
249
        return $this->webshop->basket->add(intval($product_id), intval($quantity), $text, $product_detail_id);
250
    }
251
252
    /**
253
     * Change the quantity of one product in basket
254
     *
255
     * @param struct  $credentials       Credentials to use the server
256
     * @param integer $product_id        Product id to change
257
     * @param integer $quantity          New quantity
258
     * @param string  $text              Extra text to the itemline
259
     * @param integer $product_detail_id Product detail id
260
     *
261
     * @return mixed
262
     */
263
    public function changeProductInBasket($credentials, $product_id, $quantity, $text = '', $product_detail_id = 0)
264
    {
265
        $this->checkCredentials($credentials);
266
267
        $this->_factoryWebshop();
268
269
        $product_id = intval($product_id);
270
        $quantity = intval($quantity);
271
272
        if (!is_numeric($product_id) and !is_numeric($quantity)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
273
            require_once 'XML/RPC2/Exception.php';
274
            throw new XML_RPC2_FaultException('product id and quantity must be integers', -5);
275
        }
276
277
        $text = $this->utf8Decode($text);
278
        if (!$this->webshop->basket->change($product_id, $quantity, $text, $product_detail_id)) {
279
            return false;
280
        }
281
282
        return true;
283
    }
284
285
    /**
286
     * Gets an array with the current basket
287
     *
288
     * @param struct $credentials Credentials to use the server
289
     * @param struct $customer customer values
290
     *
291
     * @return array
292
     */
293
    public function getBasket($credentials, $customer = array())
294
    {
295
        $this->checkCredentials($credentials);
296
297
        $this->_factoryWebshop();
298
299
        // we put the possibility for BasketEvaluation not to be run.
300
        if (is_string($customer) && $customer == 'no_evaluation') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
301
            // nothing happens
302
        } elseif (is_array($customer)) {
303
            require_once 'Intraface/modules/webshop/BasketEvaluation.php';
304
            $basketevaluation = new BasketEvaluation($this->webshop->kernel);
305
            if (!$basketevaluation->run($this->webshop->basket, $customer)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
306
                // We should see to return the result in some way.
307
            }
308
        }
309
310
        return array(
311
            'items' => $this->webshop->basket->getItems(),
312
            'price_total' => $this->webshop->basket->getTotalPrice(),
313
            'weight' => $this->webshop->basket->getTotalWeight()
314
        );
315
    }
316
317
    /**
318
     * Places an order in Intraface based on the current basket
319
     *
320
     * <code>
321
     *
322
     * </code>
323
     *
324
     * @param struct $credentials Credentials to use the server
325
     * @param struct $values      Values to save
326
     *
327
     * @return integer $order_id
328
     */
329
    public function placeOrder($credentials, $values)
330
    {
331
        $this->checkCredentials($credentials);
332
333
        $this->_factoryWebshop();
334
335
        if (!is_array($this->webshop->basket->getItems()) or count($this->webshop->basket->getItems()) <= 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
336
            require_once 'XML/RPC2/Exception.php';
337
            throw new XML_RPC2_FaultException('order could not be sent - cart is empty', -4);
338
        }
339
340
        if (empty($values['description'])) {
341
            $values['description'] = 'Onlineshop';
342
        }
343
344
        $values = $this->utf8Decode($values);
345
346
        if (!$order_id = $this->webshop->placeOrder($values)) {
347
            require_once 'XML/RPC2/Exception.php';
348
            throw new XML_RPC2_FaultException('order could not be placed. It returned the following error: ' . strtolower(implode(', ', $this->webshop->error->getMessage())), -4);
349
        }
350
351
        return $order_id;
352
    }
353
354
355
    /**
356
     * Saves details for a processed onlineoayment
357
     *
358
     *
359
     * @param struct $credentials Credentials to use the server
360
     * @param struct $values      Values to save
361
     *
362
     * @return integer $payment_id
363
     */
364
    public function saveOnlinePayment($credentials, $values)
365
    {
366
        $this->checkCredentials($credentials);
367
368
        $this->_factoryWebshop();
369
370
        if (!$this->kernel->intranet->hasModuleAccess('onlinepayment')) {
0 ignored issues
show
Unused Code introduced by
The call to FakeKernelIntranet::hasModuleAccess() has too many arguments starting with 'onlinepayment'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to FakeKernelIntranetWithNoAccess::hasModuleAccess() has too many arguments starting with 'onlinepayment'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to FakeDebtorIntranet::hasModuleAccess() has too many arguments starting with 'onlinepayment'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
371
            require_once 'XML/RPC2/Exception.php';
372
            throw new XML_RPC2_FaultException('The intranet did not have access to OnlinePayment', -4);
373
        }
374
375
        $this->kernel->useModule('onlinepayment', true); // true: ignores user access;
376
377
        if (isset($values['payment_id']) && intval($values['payment_id']) > 0) {
378
            $onlinepayment = OnlinePayment::factory($this->kernel, 'id', intval($values['payment_id']));
379
        } else {
380
            $onlinepayment = OnlinePayment::factory($this->kernel);
381
        }
382
383
        if (!$payment_id = $onlinepayment->save($values)) {
0 ignored issues
show
Documentation introduced by
$values is of type object<Struct>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
384
            require_once 'XML/RPC2/Exception.php';
385
            throw new XML_RPC2_FaultException('Onlinebetaling kunne ikke blive gemt' . strtolower(implode(', ', $onlinepayment->error->getMessage())), -4);
386
        }
387
388
        return $payment_id;
389
    }
390
391
392
    /**
393
     * Returns an onlinepayment id to be processed to the id can be used in payment
394
     *
395
     * @param struct $credentials Credentials to use the server
396
     *
397
     * @return integer $payment_id
398
     */
399
    public function createOnlinePayment($credentials)
400
    {
401
        $this->checkCredentials($credentials);
402
403
        $this->_factoryWebshop();
404
405
        if (!$this->kernel->intranet->hasModuleAccess('onlinepayment')) {
0 ignored issues
show
Unused Code introduced by
The call to FakeKernelIntranet::hasModuleAccess() has too many arguments starting with 'onlinepayment'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to FakeKernelIntranetWithNoAccess::hasModuleAccess() has too many arguments starting with 'onlinepayment'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to FakeDebtorIntranet::hasModuleAccess() has too many arguments starting with 'onlinepayment'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
406
            require_once 'XML/RPC2/Exception.php';
407
            throw new XML_RPC2_FaultException('The intranet did not have access to OnlinePayment', -4);
408
        }
409
410
        $this->kernel->useModule('onlinepayment', true); // true: ignores user access;
411
412
        $onlinepayment = OnlinePayment::factory($this->kernel);
413
414
        if (!$payment_id = $onlinepayment->create()) {
415
            require_once 'XML/RPC2/Exception.php';
416
            throw new XML_RPC2_FaultException('onlinepayment could not be created' . strtolower(implode(', ', $onlinepayment->error->getMessage())), -4);
417
        }
418
419
        return $payment_id;
420
    }
421
422
    /**
423
     * Saves buyer details
424
     *
425
     * @param struct $credentials Credentials to use the server
426
     * @param struct $values      Values to save
427
     *
428
     * @return boolean true or false
429
     */
430 View Code Duplication
    public function saveAddress($credentials, $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
431
    {
432
        $this->checkCredentials($credentials);
433
434
        $this->_factoryWebshop();
435
436
        if (!is_array($values)) {
437
            require_once 'XML/RPC2/Exception.php';
438
            throw new XML_RPC2_FaultException('details could not be saved - nothing to save', -4);
439
        }
440
441
        $values = $this->utf8Decode($values);
442
443
        if (!$this->webshop->basket->saveAddress($values)) {
444
            require_once 'XML/RPC2/Exception.php';
445
            throw new XML_RPC2_FaultException('datails could not be saved ' . strtolower(implode(', ', $this->webshop->error->getMessage())), -4);
446
        }
447
448
        return true;
449
    }
450
451
    /**
452
     * Get buyer details
453
     *
454
     * @param struct  $credentials Credentials to use the server
455
     *
456
     * @return array
457
     */
458
    public function getAddress($credentials)
459
    {
460
        $this->checkCredentials($credentials);
461
462
        $this->_factoryWebshop();
463
464
        return $this->webshop->basket->getAddress();
465
    }
466
467
    /**
468
     * Saves customer coupon
469
     *
470
     * @param struct $credentials     Credentials to use the server
471
     * @param string $customer_coupon Customer coupon to save
472
     *
473
     * @return boolean true or false
474
     */
475 View Code Duplication
    public function saveCustomerCoupon($credentials, $customer_coupon)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
476
    {
477
        $this->checkCredentials($credentials);
478
479
        $this->_factoryWebshop();
480
481
        $customer_coupon = $this->utf8Decode($customer_coupon);
482
        if (!$this->webshop->basket->saveCustomerCoupon($customer_coupon)) {
483
            require_once 'XML/RPC2/Exception.php';
484
            throw new XML_RPC2_FaultException('datails could not be saved ' . strtolower(implode(', ', $this->webshop->error->getMessage())), -4);
485
        }
486
487
        return true;
488
    }
489
490
491
    /**
492
     * Get customer coupon
493
     *
494
     * @param struct $credentials Credentials to use the server
495
     *
496
     * @return array
497
     */
498
    public function getCustomerCoupon($credentials)
499
    {
500
        $this->checkCredentials($credentials);
501
502
        $this->_factoryWebshop();
503
504
        return $this->webshop->basket->getCustomerCoupon();
505
    }
506
507
    /**
508
     * Saves customer EAN location number
509
     *
510
     * @param struct $credentials     Credentials to use the server
511
     * @param string $customer_ean Customer EAN to save
512
     *
513
     * @return boolean true or false
514
     */
515 View Code Duplication
    public function saveCustomerEan($credentials, $customer_ean)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
516
    {
517
        $this->checkCredentials($credentials);
518
519
        $this->_factoryWebshop();
520
521
        $customer_ean = $this->utf8Decode($customer_ean);
522
        if (!$this->webshop->basket->saveCustomerEan($customer_ean)) {
523
            require_once 'XML/RPC2/Exception.php';
524
            throw new XML_RPC2_FaultException('ean could not be saved ' . strtolower(implode(', ', $this->webshop->error->getMessage())), -4);
525
        }
526
527
        return true;
528
    }
529
530
531
    /**
532
     * Get customer EAN location number
533
     *
534
     * @param struct $credentials Credentials to use the server
535
     *
536
     * @return array
537
     */
538
    public function getCustomerEan($credentials)
539
    {
540
        $this->checkCredentials($credentials);
541
542
        $this->_factoryWebshop();
543
544
        return $this->webshop->basket->getCustomerEan();
545
    }
546
547
    /**
548
     * Saves customer comment
549
     *
550
     * @param struct $credentials     Credentials to use the server
551
     * @param string $customer_comment Customer coupon to save
552
     *
553
     * @return boolean true or false
554
     */
555 View Code Duplication
    public function saveCustomerComment($credentials, $customer_comment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
556
    {
557
        $this->checkCredentials($credentials);
558
559
        $this->_factoryWebshop();
560
561
        $customer_comment = $this->utf8Decode($customer_comment);
562
        if (!$this->webshop->basket->saveCustomerComment($customer_comment)) {
563
            require_once 'XML/RPC2/Exception.php';
564
            throw new XML_RPC2_FaultException('datails could not be saved ' . strtolower(implode(', ', $this->webshop->error->getMessage())), -4);
565
        }
566
567
        return true;
568
    }
569
570
571
    /**
572
     * Get customer comment
573
     *
574
     * @param struct $credentials Credentials to use the server
575
     *
576
     * @return array
577
     */
578
    public function getCustomerComment($credentials)
579
    {
580
        $this->checkCredentials($credentials);
581
582
        $this->_factoryWebshop();
583
584
        return $this->webshop->basket->getCustomerComment();
585
    }
586
587
    /**
588
     * Get receipt text
589
     *
590
     * @param struct $credentials Credentials to use the server
591
     *
592
     * @return array
593
     */
594
    public function getReceiptText($credentials)
595
    {
596
        $this->checkCredentials($credentials);
597
598
        $this->_factoryWebshop();
599
600
        return $this->webshop->getReceiptText();
601
    }
602
603
    /**
604
     * Checks credentials
605
     *
606
     * @param struct $credentials Credentials to use the server
607
     *
608
     * @return array
609
     */
610
    private function checkCredentials($credentials)
611
    {
612
        $this->credentials = $credentials;
613
614
        if (count($credentials) != 2) { // -4
615
            require_once 'XML/RPC2/Exception.php';
616
            throw new XML_RPC2_FaultException('wrong argument count in $credentials - got ' . count($credentials) . ' arguments - need 2', -4);
617
        }
618
        if (empty($credentials['private_key'])) { // -5
619
            require_once 'XML/RPC2/Exception.php';
620
            throw new XML_RPC2_FaultException('supply a private_key', -5);
621
        }
622
        if (empty($credentials['session_id'])) { // -5
623
            require_once 'XML/RPC2/Exception.php';
624
            throw new XML_RPC2_FaultException('supply a session_id', -5);
625
        }
626
627
        $auth_adapter = new Intraface_Auth_PrivateKeyLogin(MDB2::singleton(DB_DSN), $credentials['session_id'], $credentials['private_key']);
628
        $weblogin = $auth_adapter->auth();
629
630
        if (!$weblogin) {
631
            require_once 'XML/RPC2/Exception.php';
632
            throw new XML_RPC2_FaultException('access to intranet denied', -2);
633
        }
634
635
        $this->kernel = new Intraface_Kernel($credentials['session_id']);
636
        $this->kernel->weblogin = $weblogin;
0 ignored issues
show
Bug introduced by
The property weblogin does not seem to exist in Intraface_Kernel.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
637
        $this->kernel->intranet = new Intraface_Intranet($weblogin->getActiveIntranetId());
638
        $this->kernel->setting = new Intraface_Setting($this->kernel->intranet->get('id'));
639
640
        return true;
641
    }
642
643
    /**
644
     * Initialize the webshop
645
     *
646
     * @return void
647
     */
648
    private function _factoryWebshop()
649
    {
650
        if (!$this->kernel->intranet->hasModuleAccess('webshop')) {
0 ignored issues
show
Unused Code introduced by
The call to FakeKernelIntranet::hasModuleAccess() has too many arguments starting with 'webshop'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to FakeKernelIntranetWithNoAccess::hasModuleAccess() has too many arguments starting with 'webshop'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to FakeDebtorIntranet::hasModuleAccess() has too many arguments starting with 'webshop'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
651
            require_once 'XML/RPC2/Exception.php';
652
            throw new XML_RPC2_FaultException('The intranet does not have access to the module webshop', -2);
653
        }
654
        $this->kernel->module('webshop');
655
656
        $this->webshop = new Webshop($this->kernel, $this->credentials['session_id']);
657
    }
658
659
    private function utf8Decode($values)
660
    {
661
        if (is_array($values)) {
662
            return array_map('utf8_decode', $values);
663
        } elseif (is_string($values)) {
664
            return utf8_decode($values);
665
        } else {
666
            return $values;
667
        }
668
    }
669
}
670