Code Duplication    Length = 125-125 lines in 2 locations

src/Intraface/XMLRPC/Shop/Server0100.php 1 location

@@ 435-559 (lines=125) @@
432
     *
433
     * @return array
434
     */
435
    public function getProduct($credentials, $shop_id, $id)
436
    {
437
        $this->checkCredentials($credentials);
438
439
        $this->_factoryWebshop($shop_id);
440
441
        if (!is_numeric($id)) {
442
            require_once 'XML/RPC2/Exception.php';
443
            throw new XML_RPC2_FaultException('product id must be an integer', -5);
444
        }
445
446
        $id = $this->processRequestData(intval($id));
447
448
        $return = array();
449
450
        $product = new Product($this->kernel, $id);
451
        if ($product->get('id') == 0 || $product->get('do_show') == 0 || $product->get('active') == 0) {
452
            return array('product' => array('id' => 0));
453
        }
454
455
        $product->getPictures();
456
        $return['product'] = $product->get();
457
        $return['product']['currency']['DKK']['price'] = $product->getDetails()->getPrice()->getAsIso(2);
458
        $return['product']['currency']['DKK']['price_incl_vat'] = $product->getDetails()->getPriceIncludingVat()->getAsIso(2);
459
        $return['product']['currency']['DKK']['before_price'] = $product->getDetails()->getBeforePrice()->getAsIso(2);
460
        $return['product']['currency']['DKK']['before_price_incl_vat'] = $product->getDetails()->getBeforePriceIncludingVat()->getAsIso(2);
461
462
        if (false !== ($currency_gateway = $this->getCurrencyGateway())) {
463
            foreach ($currency_gateway->findAllWithExchangeRate() as $currency) {
464
                $return['product']['currency'][$currency->getType()->getIsoCode()]['price'] = $product->getDetails()->getPriceInCurrency($currency)->getAsIso(2);
465
                $return['product']['currency'][$currency->getType()->getIsoCode()]['price_incl_vat'] = $product->getDetails()->getPriceIncludingVatInCurrency($currency)->getAsIso(2);
466
                $return['product']['currency'][$currency->getType()->getIsoCode()]['before_price'] = $product->getDetails()->getBeforePriceInCurrency($currency)->getAsIso(2);
467
                $return['product']['currency'][$currency->getType()->getIsoCode()]['before_price_incl_vat'] = $product->getDetails()->getBeforePriceIncludingVatInCurrency($currency)->getAsIso(2);
468
            }
469
        }
470
471
        if (!$product->hasVariation() && $product->get('stock')) {
472
            $return['stock'] = $product->getStock()->get();
473
        }
474
475
        if ($product->get('has_variation')) {
476
            $variations = $product->getVariations();
477
            foreach ($variations as $variation) {
478
                if ($product->get('stock')) {
479
                    $stock = $variation->getStock($product)->get();
480
                } else {
481
                    $stock = false;
482
                }
483
484
                $detail = $variation->getDetail();
485
                $attribute_string = '';
486
                $attributes_array = $variation->getAttributesAsArray();
487
488
                foreach ($attributes_array as $attribute) {
489
                    if ($attribute_string != '') {
490
                        $attribute_string .= '-';
491
                    }
492
                    $attribute_string .= $attribute['id'];
493
494
                    // We calculate all products which is on stock with this attribute to be able to mark unused attributes in list.
495
                    if (!isset($attribute_for_sale[$attribute['id']])) {
496
                        $attribute_for_sale[$attribute['id']] = 0;
497
                    }
498
                    if ($stock !== false) {
499
                        // If for_sale is less than zero we add zero.
500
                        $attribute_for_sale[$attribute['id']] += (($stock['for_sale'] < 0) ? 0 : $stock['for_sale']);
501
                    } else {
502
                        // If product does not use stock, then we calculate one up, as the attribute is always in use.
503
                        $attribute_for_sale[$attribute['id']] += 1;
504
                    }
505
                }
506
507
                $variation_currency['DKK']['price'] = $detail->getPrice($product)->getAsIso(2);
508
                $variation_currency['DKK']['price_incl_vat'] = $detail->getPriceIncludingVat($product)->getAsIso(2);
509
510
                if (isset($currency_gateway) && is_object($currency_gateway)) {
511
                    foreach ($currency_gateway->findAllWithExchangeRate() as $currency) {
512
                        $variation_currency[$currency->getType()->getIsoCode()]['price'] = $detail->getPriceInCurrency($currency, 0, $product)->getAsIso(2);
513
                        $variation_currency[$currency->getType()->getIsoCode()]['price_incl_vat'] = $detail->getPriceIncludingVatInCurrency($currency, 0, $product)->getAsIso(2);
514
                    }
515
                }
516
517
                $return['variations'][] = array(
518
                    'variation' => array(
519
                        'id' => $variation->getId(),
520
                        'detail_id' => $detail->getId(),
521
                        'number' => $variation->getNumber(),
522
                        'name' => $variation->getName(),
523
                        'attributes' => $attributes_array,
524
                        'identifier' => $attribute_string,
525
                        'price_incl_vat' => $detail->getPriceIncludingVat($product)->getAsIso(2),
526
                        'weight' => $product->get('weight') + $detail->getWeightDifference(2),
527
                        'currency' => $variation_currency
528
                    ),
529
                    'stock' => $stock
530
                );
531
            }
532
533
            // We should make a Doctrine Product_X_AttributeGroup class and get all the groups i one sql
534
            $groups = $product->getAttributeGroups();
535
            $group_gateway = new Intraface_modules_product_Attribute_Group_Gateway;
536
            foreach ($groups as $key => $group) {
537
                // Make sure we only include necessary data
538
                $return['attribute_groups'][$key]['id'] = $group['id'];
539
                $return['attribute_groups'][$key]['name'] = $group['name'];
540
                $attributes = $group_gateway->findById($group['id'])->getAttributesUsedByProduct($product);
541
                foreach ($attributes as $attribute) {
542
                    // No products has attribute on stock we remove it from the list.
543
                    if (isset($attribute_for_sale[$attribute->getId()]) && $attribute_for_sale[$attribute->getId()] == 0) {
544
                        $is_used = 0;
545
                    } else {
546
                        $is_used = 1;
547
                    }
548
549
                    $return['attribute_groups'][$key]['attributes'][] = array(
550
                        'id' => $attribute->getId(),
551
                        'name' => $attribute->getName(),
552
                        'is_used' => $is_used
553
                    );
554
                }
555
            }
556
        }
557
558
        return $this->prepareResponseData($return);
559
    }
560
561
    /**
562
     * Gets related products

src/Intraface/XMLRPC/Shop/Server0004.php 1 location

@@ 270-394 (lines=125) @@
267
     *
268
     * @return array
269
     */
270
    public function getProduct($credentials, $shop_id, $id)
271
    {
272
        $this->checkCredentials($credentials);
273
274
        $this->_factoryWebshop($shop_id);
275
276
        if (!is_numeric($id)) {
277
            require_once 'XML/RPC2/Exception.php';
278
            throw new XML_RPC2_FaultException('product id must be an integer', -5);
279
        }
280
281
        $id = $this->processRequestData(intval($id));
282
283
        $return = array();
284
285
        $product = new Product($this->kernel, $id);
286
        if ($product->get('id') == 0 || $product->get('do_show') == 0 || $product->get('active') == 0) {
287
            return array('product' => array('id' => 0));
288
        }
289
290
        $product->getPictures();
291
        $return['product'] = $product->get();
292
        $return['product']['currency']['DKK']['price'] = $product->getDetails()->getPrice()->getAsIso(2);
293
        $return['product']['currency']['DKK']['price_incl_vat'] = $product->getDetails()->getPriceIncludingVat()->getAsIso(2);
294
        $return['product']['currency']['DKK']['before_price'] = $product->getDetails()->getBeforePrice()->getAsIso(2);
295
        $return['product']['currency']['DKK']['before_price_incl_vat'] = $product->getDetails()->getBeforePriceIncludingVat()->getAsIso(2);
296
297
        if (false !== ($currency_gateway = $this->getCurrencyGateway())) {
298
            foreach ($currency_gateway->findAllWithExchangeRate() as $currency) {
299
                $return['product']['currency'][$currency->getType()->getIsoCode()]['price'] = $product->getDetails()->getPriceInCurrency($currency)->getAsIso(2);
300
                $return['product']['currency'][$currency->getType()->getIsoCode()]['price_incl_vat'] = $product->getDetails()->getPriceIncludingVatInCurrency($currency)->getAsIso(2);
301
                $return['product']['currency'][$currency->getType()->getIsoCode()]['before_price'] = $product->getDetails()->getBeforePriceInCurrency($currency)->getAsIso(2);
302
                $return['product']['currency'][$currency->getType()->getIsoCode()]['before_price_incl_vat'] = $product->getDetails()->getBeforePriceIncludingVatInCurrency($currency)->getAsIso(2);
303
            }
304
        }
305
306
        if (!$product->hasVariation() && $product->get('stock')) {
307
            $return['stock'] = $product->getStock()->get();
308
        }
309
310
        if ($product->get('has_variation')) {
311
            $variations = $product->getVariations();
312
            foreach ($variations as $variation) {
313
                if ($product->get('stock')) {
314
                    $stock = $variation->getStock($product)->get();
315
                } else {
316
                    $stock = false;
317
                }
318
319
                $detail = $variation->getDetail();
320
                $attribute_string = '';
321
                $attributes_array = $variation->getAttributesAsArray();
322
323
                foreach ($attributes_array as $attribute) {
324
                    if ($attribute_string != '') {
325
                        $attribute_string .= '-';
326
                    }
327
                    $attribute_string .= $attribute['id'];
328
329
                    // We calculate all products which is on stock with this attribute to be able to mark unused attributes in list.
330
                    if (!isset($attribute_for_sale[$attribute['id']])) {
331
                        $attribute_for_sale[$attribute['id']] = 0;
332
                    }
333
                    if ($stock !== false) {
334
                        // If for_sale is less than zero we add zero.
335
                        $attribute_for_sale[$attribute['id']] += (($stock['for_sale'] < 0) ? 0 : $stock['for_sale']);
336
                    } else {
337
                        // If product does not use stock, then we calculate one up, as the attribute is always in use.
338
                        $attribute_for_sale[$attribute['id']] += 1;
339
                    }
340
                }
341
342
                $variation_currency['DKK']['price'] = $detail->getPrice($product)->getAsIso(2);
343
                $variation_currency['DKK']['price_incl_vat'] = $detail->getPriceIncludingVat($product)->getAsIso(2);
344
345
                if (isset($currency_gateway) && is_object($currency_gateway)) {
346
                    foreach ($currency_gateway->findAllWithExchangeRate() as $currency) {
347
                        $variation_currency[$currency->getType()->getIsoCode()]['price'] = $detail->getPriceInCurrency($currency, 0, $product)->getAsIso(2);
348
                        $variation_currency[$currency->getType()->getIsoCode()]['price_incl_vat'] = $detail->getPriceIncludingVatInCurrency($currency, 0, $product)->getAsIso(2);
349
                    }
350
                }
351
352
                $return['variations'][] = array(
353
                    'variation' => array(
354
                        'id' => $variation->getId(),
355
                        'detail_id' => $detail->getId(),
356
                        'number' => $variation->getNumber(),
357
                        'name' => $variation->getName(),
358
                        'attributes' => $attributes_array,
359
                        'identifier' => $attribute_string,
360
                        'price_incl_vat' => $detail->getPriceIncludingVat($product)->getAsIso(2),
361
                        'weight' => $product->get('weight') + $detail->getWeightDifference(2),
362
                        'currency' => $variation_currency
363
                    ),
364
                    'stock' => $stock
365
                );
366
            }
367
368
            // We should make a Doctrine Product_X_AttributeGroup class and get all the groups i one sql
369
            $groups = $product->getAttributeGroups();
370
            $group_gateway = new Intraface_modules_product_Attribute_Group_Gateway;
371
            foreach ($groups as $key => $group) {
372
                // Make sure we only include necessary data
373
                $return['attribute_groups'][$key]['id'] = $group['id'];
374
                $return['attribute_groups'][$key]['name'] = $group['name'];
375
                $attributes = $group_gateway->findById($group['id'])->getAttributesUsedByProduct($product);
376
                foreach ($attributes as $attribute) {
377
                    // No products has attribute on stock we remove it from the list.
378
                    if (isset($attribute_for_sale[$attribute->getId()]) && $attribute_for_sale[$attribute->getId()] == 0) {
379
                        $is_used = 0;
380
                    } else {
381
                        $is_used = 1;
382
                    }
383
384
                    $return['attribute_groups'][$key]['attributes'][] = array(
385
                        'id' => $attribute->getId(),
386
                        'name' => $attribute->getName(),
387
                        'is_used' => $is_used
388
                    );
389
                }
390
            }
391
        }
392
393
        return $this->prepareResponseData($return);
394
    }
395
396
    /**
397
     * Gets related products