Completed
Push — master ( e76e8a...6b7664 )
by Alexey
05:29
created

YandexExportController::indexAction()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 84
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 84
rs 8.35
cc 6
eloc 60
nc 12
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A YandexExportController.php ➔ addToXml() 0 6 1

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
3
/**
4
 * Yandex export controller
5
 * @author Alexey Krupskiy <[email protected]>
6
 * @link http://inji.ru/
7
 * @copyright 2016 Alexey Krupskiy
8
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
9
 */
10
class YandexExportController extends Controller
11
{
12
    function indexAction()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
    {
14
        function addToXml($xml, $parent, $nodeName, $text)
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
        {
16
            $node = $parent->appendChild($xml->createElement($nodeName));
17
            $node->appendChild($xml->createTextNode($text));
18
            return $node;
19
        }
20
21
        $imp = new DOMImplementation;
22
23
        $dtd = $imp->createDocumentType('yml_catalog', '', 'shops.dtd');
24
25
        $xml = $imp->createDocument('', '', $dtd);
26
27
        $xml->encoding = 'UTF-8';
28
29
        $xml->formatOutput = true;
30
31
        $root = $xml->createElement('yml_catalog');
32
        $root->setAttribute("date", date('Y-m-d H:i'));
33
        $root = $xml->appendChild($root);
34
35
        $shop = $xml->createElement('shop');
36
37
        addToXml($xml, $shop, 'name', \App::$cur->config['site']['name']);
38
        addToXml($xml, $shop, 'company', \App::$cur->config['site']['company_name']);
39
        addToXml($xml, $shop, 'url', 'http://' . INJI_DOMAIN_NAME);
40
41
        $currencies = $xml->createElement('currencies');
42
        $currency = $currencies->appendChild($xml->createElement('currency'));
43
        $currency->setAttribute('id', 'RUR');
44
        $currency->setAttribute('rate', '1');
45
        $currency->setAttribute('plus', '');
46
        $shop->appendChild($currencies);
47
48
        $categories = $xml->createElement('categories');
49
        foreach (Ecommerce\Category::getList() as $category) {
50
            $xmlCategory = addToXml($xml, $categories, 'category', $category->name);
51
            $xmlCategory->setAttribute('id', $category->id);
52
            if ($category->parent_id) {
53
                $xmlCategory->setAttribute('parentId', $category->parent_id);
54
            }
55
        }
56
        $shop->appendChild($categories);
57
58
        addToXml($xml, $shop, 'local_delivery_cost', '300');
59
60
        $offers = $xml->createElement('offers');
61
        foreach (App::$cur->ecommerce->getItems() as $item) {
62
            $offer = $offers->appendChild($xml->createElement('offer'));
63
            addToXml($xml, $offer, 'url', 'http://' . INJI_DOMAIN_NAME . '/ecommerce/view/' . $item->id);
64
            addToXml($xml, $offer, 'price', $item->getPrice()->price);
65
            addToXml($xml, $offer, 'currencyId', 'RUR');
66
            addToXml($xml, $offer, 'categoryId', $item->category_id);
67
            addToXml($xml, $offer, 'delivery', 'true');
68
            addToXml($xml, $offer, 'local_delivery_cost', '300');
69
            addToXml($xml, $offer, 'vendor', 'vendor');
70
            addToXml($xml, $offer, 'model', 'model');
71
            addToXml($xml, $offer, 'description', $item->description);
72
            addToXml($xml, $offer, 'manufacturer_warranty', 'true');
73
            addToXml($xml, $offer, 'country_of_origin', 'Китай');
74
75
            foreach ($item->options as $option) {
76
                $param = addToXml($xml, $offer, 'param', $option->value);
77
                $param->setAttribute('name', $option->item_option_name);
78
                if ($option->item_option_postfix) {
79
                    $param->setAttribute('unit', $option->item_option_postfix);
80
                }
81
            }
82
        }
83
        $shop->appendChild($offers);
84
85
        $root->appendChild($shop);
86
87
        header("Content-Type: text/xml");
88
        header("Expires: Thu, 19 Feb 1998 13:24:18 GMT");
89
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
90
        header("Cache-Control: no-cache, must-revalidate");
91
        header("Cache-Control: post-check=0,pre-check=0");
92
        header("Cache-Control: max-age=0");
93
        header("Pragma: no-cache");
94
        echo $xml->saveXML();
95
    }
96
97
}
98