Test Failed
Push — master ( 51d55d...1c8811 )
by Alexey
10:37 queued 05:27
created

CDEK::request()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 28
nc 4
nop 1
dl 0
loc 37
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * INJI
4
 *
5
 * @author Alexey Krupskiy <[email protected]>
6
 * @link http://inji.ru/
7
 * @copyright 2017 Alexey Krupskiy
8
 * @license https://github.com/injitools/Inji/blob/master/LICENSE
9
 */
10
11
namespace Ecommerce\DeliveryProvider;
12
13
class CDEK extends \Ecommerce\DeliveryProvider {
14
    static $name = 'СДЭК - курьерская служба';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
15
16
    /**
17
     * @param \Ecommerce\Cart $cart
18
     */
19
    static function getTariff($cart) {
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...
20
        $tariff = 138;
21
        $field = \Ecommerce\Delivery\Field::get('cdektype', 'code');
22
        if (isset($cart->deliveryInfos[$field->id])) {
23
            $item = \Ecommerce\Delivery\Field\Item::get([['id', $cart->deliveryInfos[$field->id]->value], ['delivery_field_id', $field->id]]);
24
            if ($item) {
25
                $tariff = $item->data;
26
            }
27
        }
28
        return $tariff;
29
    }
30
31
    static function request($cart) {
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...
32
        $cityId = 0;
33
        $senderCity = 44;
34
        $cityItem = static::getCity($cart);
35
        if ($cityItem) {
36
            $cityId = json_decode($cityItem->data, true)['ID'];
37
        }
38
        if (!$cityId) {
39
            return false;
40
        }
41
        $tariff = static::getTariff($cart);
42
        $config = self::config();
43
        return \Cache::get('pickPointCalc', [
44
            'login' => $config['authLogin'],
45
            'pass' => $config['authPassword'],
46
            'senderCity' => $senderCity,
47
            'cityId' => $cityId,
48
            'tariff' => $tariff,
49
        ], function ($data) {
50
            $calc = new \Ecommerce\Vendor\CalculatePriceDeliveryCdek();
51
            $calc->setAuth($data['login'], $data['pass']);
52
            $calc->setDateExecute(date('Y-m-d H:i:s'));
53
            $calc->setSenderCityId($data['senderCity']);
54
            //устанавливаем город-получатель
55
            $calc->setReceiverCityId($data['cityId']);
56
            $calc->setTariffId($data['tariff']);
57
            $calc->addGoodsItemBySize(3, 25, 25, 24);
58
            if ($calc->calculate()) {
59
                return $calc->getResult();
60
            } else {
61
                //var_dump($tariff,$calc->getError());
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
                return false;
63
            }
64
        });
65
66
67
    }
68
69
    static function calcPrice($cart) {
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...
70
        $result = self::request($cart);
71
        $curId = $cart->delivery ? $cart->delivery->currency_id : 0;
72
        if (!empty($result['result']['price'])) {
73
            return new \Money\Sums([$curId => round($result['result']['price'] * 1.1, 2)]);
74
        }
75
        return new \Money\Sums([$curId => 0]);
76
    }
77
78
    static function deliveryTime($cart) {
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...
79
        $result = self::request($cart);
80
        if (isset($result['result']['deliveryPeriodMin'])) {
81
            return [
82
                'min' => $result['result']['deliveryPeriodMin'],
83
                'max' => $result['result']['deliveryPeriodMax'],
84
            ];
85
        }
86
        return [
87
            'min' => 0,
88
            'max' => 0,
89
        ];
90
    }
91
92
    static function availablePayTypeGroups($cart) {
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...
93
        $tariff = static::getTariff($cart);
94
        if ($tariff == 139) {
95
            return ['*'];
96
        }
97
        return ['online'];
98
    }
99
}