Test Failed
Push — master ( b74686...9a59de )
by Alexey
05:52
created

RussianPost::request()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 34
Code Lines 26

Duplication

Lines 9
Ratio 26.47 %

Importance

Changes 0
Metric Value
cc 7
eloc 26
nc 10
nop 1
dl 9
loc 34
rs 6.7272
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
14
class RussianPost 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
19
     * @return \Money\Sums
20
     *
21
     */
22
    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...
23
        $city = '';
24 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...
25
            if ($field->code === 'index') {
26
                if (!empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) {
27
                    $city = $_POST['deliveryFields'][$field->id];
28
                } elseif (isset($cart->deliveryInfos[$field->id])) {
29
                    $city = $cart->deliveryInfos[$field->id]->value;
30
                }
31
            }
32
        }
33
        if (!$city) {
34
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type documented by Ecommerce\DeliveryProvider\RussianPost::request 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...
35
        }
36
        $senderCity = '101000';
37
38
        $url = 'http://tariff.russianpost.ru/tariff/v1/calculate?json&';
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
        $data = [
40
            'object' => 4030,
41
            'weight' => '1',
42
            'date' => date('Ymd'),
43
            'sumoc' => $cart->itemsSum()->sums[0],
44
            'from' => $senderCity,
45
            'to' => $city,
46
            'closed' => 1,
47
            'service' => 2,
48
            'isavia' => 0,
49
            'delivery' => 1
50
        ];
51
        $result = \Cache::get('russianPostCalc', $data, function ($data) {
52
            return file_get_contents('http://tariff.russianpost.ru/tariff/v1/calculate?json&' . http_build_query($data));
53
        });
54
        return json_decode($result, true);
55
    }
56
57
    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...
58
        $result = static::request($cart);
59
        if ($result) {
60
            return new \Money\Sums([$cart->delivery->currency_id => 0]);
61
        }
62
        $sum = !empty($result['tariff'][0]['ground']['valnds']) ? $result['tariff'][0]['ground']['valnds'] : (!empty($result['tariff'][0]['avia']['valnds']) ? $result['tariff'][0]['avia']['valnds'] : 0);
63
        return new \Money\Sums([$cart->delivery->currency_id => $sum / 100 * 1.1]);
64
    }
65
66
    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...
67
        $result = static::request($cart);
68
        return $result['delivery'];
69
    }
70
}