ОПТИМИЗАЦИЯ МЕТОДОВ СЦЕНАРИЯ PHP

Вот некоторый php-код, который, я думаю, оптимизирован, но все еще хочу его оптимизировать, но не знаю, как это сделать.

<?php
$result = curl_exec($ch); // here i get a json response from a curl.

$result = json_decode($result, TRUE);
$result = arrangeData($result, $app, $currentSite);
$app->exit($result);


function arrangeData($result, $app, $currentSite){
    $total = $result['hits']['total']['value'];
    $final_result = array (
      'data' =>
      array (
        'customer' =>
        array (
          'wishlist' =>
          array (
            'id' => '',
            'items_count' => $total,
            'items' => getProducts($result,$app,$currentSite),
          ),
        ),
      ),
    );
    return $final_result;
}

function getProducts($result,$app,$currentSite){
    $product_set = array();
    $all_products = $result['hits']['hits'];
    foreach ($all_products as $productdata) {
        $product_set[] = getArrangedProduct($productdata,$app,$currentSite);
    }
    return $product_set;
}

function getArrangedProduct($productdata,$app,$currentSite){
    $productId = $productdata['_id'];
    $data = $productdata['_source'];
    $config = getConfigValue($app,$currentSite);
    $image_label = $data['image'] ? getImageLabel($data['image'],$app,$currentSite) : null;
    $product =
    array (
    'id' => '',
    'website' => '',
    'product' => array(
      'id' => $productId,
      'name' => $data['name'],
      'sku' => $data['sku'],
      'url_key' =>$data['url_key']['0'],
      'url_suffix' => '.html',
      'display_brand' => $data['display_brand'],
      'manufacturer' => $data['manufacturer'],
      'manufacturer_label' => $data['manufacturer_value'],
      'quantity_in_stock' => 0,
      'trend' => $data['trend_value'],
      'microcategory' => $data['microcategory'],
      'size_label' => $data['size_value'],
      'over_image' => $data['over_image'],
      '__typename' => 'ConfigurableProduct',
      'image' =>
      array (
        'url' => $config['media_url'].$data['image'],
        'label' => $image_label,
        'position' => '',
      ),
      'gc_image' => $data['gc_image'],
      'whole_preorder' => $data['whole_preorder_value']=='Yes' ? 1 : 0,
      'preorder_shipping_date' => $data['preorder_shipping_date'],
      'variants' => getVariants($productId,$app,$currentSite)
      )
    );
    return $product;
}

function getImageLabel($image_path, $app, $currentSite){
    $result = $app->getReadDb()->query("SELECT `label`
        FROM `catalog_product_entity_media_gallery_value`
        WHERE `value_id` = (SELECT `value_id`
        FROM `catalog_product_entity_media_gallery`
        WHERE `value` = '".$image_path."' LIMIT 1) AND `store_id`= '".$currentSite."'
        LIMIT 1");
    $image_data = $result->fetchAll();
    $image_label = $image_data ? $image_data[0]['label'] : false;
    return $image_label;
}

function getVariants($productId,$app,$currentSite){
    $config = getConfigValue($app,$currentSite);
    $productInfo = getProductInfo($productId,$app,$currentSite);
    if(!$productInfo['sku']){
        return null;
    }
    $special_price = getProductSpecialPrice($productInfo['simple_id'],$app,$currentSite);
    $rule_price = $productInfo['rule_price'] ? $productInfo['rule_price'] : null;
    $price = $productInfo['price'] ? $productInfo['price'] : null;
    $improvised_price = $rule_price ? $rule_price : $special_price;
    $final_price = $improvised_price ? $improvised_price : $price;
    $amount_off = $price ? $price - $final_price : null;
    $percent_off = $price ? (($price - $final_price)*100) / $price : null;
    $variants =  array (
        0 =>
        array (
          'product' =>
          array (
            'id' => $productInfo['simple_id'],
            'name' => $productInfo['name'],
            'sku' => $productInfo['sku'],
            'price_range' =>
            array (
              'minimum_price' =>
              array (
                'regular_price' =>
                array (
                  'value' => $price,
                  'currency' => $config['currency'],
                ),
                'final_price' =>
                array (
                  'value' =>  $final_price,
                  'currency' => $config['currency'],
                ),
                'discount' =>
                array (
                  'amount_off' => $amount_off,
                  'percent_off' => round($percent_off),
                ),
              ),
            ),
            'special_price' => $special_price,
            'special_to_date' => NULL,
            'special_from_date' => NULL,
          ),
        ),
        );
    return $variants;
}

function getProductSpecialPrice($productId, $app, $currentSite) {
    $result = $app->getReadDb()->query("SELECT value
    FROM `catalog_product_entity_decimal`
    WHERE `store_id`= '".$currentSite."' AND `row_id` = '".$productId."'  AND `attribute_id` IN (SELECT `attribute_id` FROM `eav_attribute`
    WHERE `attribute_code` = 'special_price')");
    $prices = $result->fetchAll();
    $special_price = $prices ? $prices[0]['value'] : null;
    return $special_price;
}

function getConfigValue($app, $currentSite) {
    $result = $app->getReadDb()->query("SELECT `value` FROM `core_config_data` WHERE `scope` = 'stores'
    AND `path` = 'currency/options/default' AND `scope_id` = '".$currentSite."'
    union
    SELECT `value` from `core_config_data`
    WHERE `path` = 'dollskill_catalog/product/base_media_url' AND `scope` = 'stores' AND `scope_id` = '".$currentSite."'");
    $getConfigValue = $result->fetchAll();
    $currency = $getConfigValue ? $getConfigValue[0]['value'] : null;
    $media_url = $getConfigValue ? $getConfigValue[1]['value'] : null;
    $config_values = array('currency' => $currency,'media_url' => $media_url);
    return $config_values;
}

function getProductInfo($productId, $app,$currentSite){
    $result = $app->getReadDb()->query("SELECT parent.entity_id AS parent_id,
            simple.entity_id AS simple_id,
            simple.sku AS simple_sku,
            cpp.rule_price AS rule_price,
            cped.value AS price,
            cpev.value AS simple_name
            FROM catalog_product_entity AS parent
            JOIN catalog_product_super_link AS link ON parent.row_id = link.parent_id
            JOIN catalog_product_entity AS simple ON link.product_id = simple.entity_id
            LEFT JOIN catalogrule_product_price AS cpp ON cpp.product_id = simple.entity_id AND
            cpp.website_id = (SELECT `website_id` FROM `store` WHERE `store_id` = '".$currentSite."')
            LEFT JOIN catalog_product_entity_decimal AS cped ON cped.row_id = simple.entity_id
            AND `attribute_id` IN (SELECT `attribute_id` FROM `eav_attribute`
            WHERE `attribute_code` IN ('price')) AND cped.store_id= '".$currentSite."'
            LEFT JOIN catalog_product_entity_varchar AS cpev ON cpev.row_id = simple.entity_id
            AND cpev.store_id = '".$currentSite."'
            WHERE parent.entity_id = '".$productId."' LIMIT 1");
            $product_info = $result->fetchAll();
            $product_info = array('sku' => $product_info[0]['simple_sku'],'name' => $product_info[0]['simple_name'],'rule_price' => $product_info[0]['rule_price'],'price' => $product_info[0]['price'],'simple_id' => $product_info[0]['simple_id']);
    return $product_info;
}

Есть мысли, как это можно оптимизировать?

0

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *