Test Failed
Push — master ( 5aebc0...cf7a15 )
by Alexey
05:49
created

PickPoint::calcPrice()   C

Complexity

Conditions 10
Paths 40

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 35
c 1
b 0
f 0
nc 40
nop 1
dl 0
loc 48
rs 5.3454

How to fix   Complexity   

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
 * 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
use Ecommerce\Delivery\Provider\ConfigItem;
14
15
class PickPoint extends \Ecommerce\DeliveryProvider {
16
    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...
17
18
    /**
19
     * @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...
20
     * @return \Money\Sums
21
     */
22
    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...
23
        //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...
24
        $xml = json_encode($data);
25
        $headers = array(
26
            "Content-type: text/json",
27
            "Content-length: " . strlen($xml),
28
            "Connection: close",
29
        );
30
        $c = curl_init();
31
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
32
        curl_setopt($c, CURLOPT_URL, $URL);
33
        curl_setopt($c, CURLOPT_POST, 1);
34
        curl_setopt($c, CURLOPT_POSTFIELDS, $xml);
35
        curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
36
        $contents = curl_exec($c);
37
        curl_close($c);
38
39
        if ($contents) return $contents;
40
        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...
41
    }
42
43
    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...
44
45
46
        $sessionId = \Cache::get('PickPointSession', []);
47
        if (!$sessionId) {
48
            $config = ConfigItem::getList(['where' => ['delivery_provider_id', $cart->delivery->delivery_provider_id], 'key' => 'name']);
49
            $result = self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/login', [
50
                'Login' => $config['login']->value,
51
                'Password' => $config['pass']->value,
52
            ]);
53
            $sessionId = json_decode($result, true)['SessionId'];
54
            \Cache::set('PickPointSession', $sessionId, [], 12 * 60 * 60);
55
        }
56
        $city = '';
57
        foreach ($cart->delivery->fields as $field) {
58
            if ($field->code === 'index' && !empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) {
59
                $result = json_decode(self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/postindexpostamatlist', ['PostIndex' => $_POST['deliveryFields'][$field->id]]), true);
60
                //print_r($result['PostamatList'][0]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
91% 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...
61
                if (!empty($result['PostamatList'][0]['CitiName'])) {
62
                    $city = $result['PostamatList'][0]['CitiName'];
63
                    $toId = $result['PostamatList'][0]['Number'];
64
                }
65
            }
66
        }
67
        if (!$city) {
68
            return new \Money\Sums([$cart->delivery->currency_id => 0]);
69
        }
70
        if ($city === 'Красноярск') {
71
            $senderCity = 'Красноярск';
72
        } else {
73
            $senderCity = 'Москва';
74
        }
75
        $result = json_decode(self::curl_get_file_contents('https://e-solution.pickpoint.ru/api/calctariff', [
76
            'SessionId' => $sessionId,
77
            'FromCity' => $senderCity,
78
            'IKN' => $config['ikn']->value,
79
            'PTNumber' => $toId,
80
            'Length' => 25,
81
            'Depth' => 25,
82
            'Width' => 25,
83
        ]), true);
84
        $summ = 0;
85
        if (!empty($result['Services'][0]['Tariff'])) {
86
            $summ = $result['Services'][0]['Tariff'] + $result['Services'][0]['NDS'];
87
        }
88
89
        return new \Money\Sums([$cart->delivery->currency_id => $summ]);
90
    }
91
}