Test Failed
Push — master ( 5878a7...1a655d )
by Alexey
05:08
created

PickPoint::calcPrice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 16
nc 2
nop 1
dl 0
loc 20
rs 9.4285
c 2
b 1
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
14
class PickPoint extends \Ecommerce\DeliveryProvider {
15
    static $name = 'PickPoint - курьерская служба';
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...
16
17
    /**
18
     * @param \Ecommerce\Cart $cart
0 ignored issues
show
Bug introduced by
There is no parameter named $cart. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
19
     * @return \Money\Sums
20
     */
21
    static function curl_get_file_contents($URL, $data) {
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...
22
        //var_dump($URL,$data);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
23
        $xml = json_encode($data);
24
        $headers = array(
25
            "Content-type: text/json",
26
            "Content-length: " . strlen($xml),
27
            "Connection: close",
28
        );
29
        $c = curl_init();
30
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
31
        curl_setopt($c, CURLOPT_URL, $URL);
32
        curl_setopt($c, CURLOPT_POST, 1);
33
        curl_setopt($c, CURLOPT_POSTFIELDS, $xml);
34
        curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
35
        $contents = curl_exec($c);
36
        curl_close($c);
37
38
        if ($contents) return $contents;
39
        else return FALSE;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return FALSE; (false) is incompatible with the return type documented by Ecommerce\DeliveryProvid...:curl_get_file_contents of type Money\Sums.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
40
    }
41
42
    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...
43
        $result = self::request($cart);
44
        if (!empty($result['Zones'][0])) {
45
            return [
46
                'min' => $result['Zones'][0]['DeliveryMin'],
47
                'max' => $result['Zones'][0]['DeliveryMax']
48
            ];
49
        }
50
        return ['min' => 0, 'max' => 0];
51
    }
52
53
    /* $result = json_decode(self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/CreateShipment', [
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
54
     * 'SessionId' => $sessionId,
55
     * 'Sendings' => [[
56
     * 'IKN' => $config['ikn']->value,
57
     * 'Invoice' => [
58
     * 'Description' => 'test',
59
     * 'PostamatNumber' => $toId,
60
     * 'MobilePhone' => '+79999999999',
61
     * 'PostageType' => '10003',
62
     * 'GettingType' => '101',
63
     * 'PayType' => 1,
64
     * 'Sum' => '1000'
65
     * ]
66
     * ]],
67
     * ]), true);
68
     * var_dump($result);
69
     * $result = json_decode(self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/calctariff', [
70
     * 'SessionId' => $sessionId,
71
     * 'FromCity' => $senderCity,
72
     * 'IKN' => $config['ikn']->value,
73
     * 'PTNumber' => $toId,
74
     * 'Length' => 25,
75
     * 'Depth' => 25,
76
     * 'Width' => 25,
77
     * 'Weight' => 0.5,
78
     * ]), true);
79
     *
80
     * $summ = 0;
81
     * var_dump($result['Services']);
82
     * foreach ($result['Services'] as $service) {
83
     * $summ = $service['Tariff'] + $service['NDS'];
84
     * }
85
     * //$result = json_decode(self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/cancelInvoice', ['SessionId' => $sessionId, 'InvoiceNumber' => '15938160323']), true);
86
     */
87
    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...
88
        $config = self::config();
89
        $sessionId = \Cache::get('PickPointSession', [
90
            'Login' => $config['login'],
91
            'Password' => $config['pass'],
92
        ], function ($data) {
93
            $result = self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/login', $data);
94
            return json_decode($result, true)['SessionId'];
95
        }, 12 * 60 * 60);
96
        $toId = '';
97 View Code Duplication
        foreach ($cart->delivery->fields as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
98
            if ($field->code === 'pickpoint') {
99
                if (!empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) {
100
                    $toId = $_POST['deliveryFields'][$field->id];
101
                } elseif (isset($cart->deliveryInfos[$field->id])) {
102
                    $toId = $cart->deliveryInfos[$field->id]->value;
103
                }
104
                break;
105
            }
106
        }
107
108
        if (!$toId) {
109
            $fieldInfo = \Ecommerce\UserAdds\Field::get('deliveryfield_city', 'code');
110
            $field = \Ecommerce\Delivery\Field::get('city', 'code');
111
            if (isset($cart->infos[$fieldInfo->id])) {
112
                $item = \Ecommerce\Delivery\Field\Item::get([['id', $cart->infos[$fieldInfo->id]->value], ['delivery_field_id', $field->id]]);
113
                if ($item) {
114
                    $data = json_decode($item->data, true);
115
                    if (!empty($data['PostCodeList'])) {
116
                        $post = explode(',', $data['PostCodeList'])[0];
117
                        $result = json_decode(self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/postindexpostamatlist', ['PostIndex' => $post]), true);
118
                        if (!empty($result['PostamatList'][0]['CitiName'])) {
119
                            $toId = $result['PostamatList'][0]['Number'];
120
                        }
121
                    }
122
123
                }
124
            }
125
        }
126
        if (!$toId) {
127
            return false;
128
        }
129
130
        $senderCity = 'Москва';
131
132
        return \Cache::get('pickPointCalc', [
133
            'SessionId' => $sessionId,
134
            'IKN' => $config['ikn'],
135
            'FromCity' => $senderCity,
136
            'ToPT' => $toId,
137
        ], function ($data) {
138
            return json_decode(self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/getzone', $data), true);
139
        });
140
    }
141
142
    /**
143
     * @param \Ecommerce\Cart $cart
144
     * @return \Money\Sums
145
     */
146
    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...
147
        $result = self::request($cart);
148
        if (!$result) {
149
            return new \Money\Sums([$cart->delivery->currency_id => 0]);
150
        }
151
        $zones = [
152
            0 => 270,
153
            1 => 292,
154
            2 => 299,
155
            3 => 315,
156
            4 => 328,
157
            5 => 368,
158
            6 => 443,
159
            7 => 466,
160
            8 => 527,
161
        ];
162
        return new \Money\Sums([
163
            $cart->delivery->currency_id => round($zones[$result['Zones'][0]['Zone']] * $result['Zones'][0]['Koeff'] * 1.1, 2)
164
        ]);
165
    }
166
167
    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...
168
        return ['online'];
169
    }
170
}