Completed
Push — master ( 847696...e0da2f )
by Marcel
11:03
created

Controller::getTaxRateForLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Mpociot\VatCalculator\Http;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller as BaseController;
7
use Illuminate\Support\Facades\Response;
8
use Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException;
9
use Mpociot\VatCalculator\VatCalculator;
10
11
class Controller extends BaseController
12
{
13
    /**
14
     * @var VatCalculator
15
     */
16
    private $calculator;
17
18
    public function __construct(VatCalculator $calculator)
19
    {
20
        $this->calculator = $calculator;
21
    }
22
23
    /**
24
     * Returns the tax rate for the given country code and postal code.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function getTaxRateForLocation($countryCode = null, $postalCode = null)
29
    {
30
        return [
31
            'tax_rate' => $this->calculator->getTaxRateForLocation($countryCode, $postalCode),
32
        ];
33
    }
34
35
    /**
36
     * Returns the tax rate for the given country.
37
     *
38
     * @param Request $request
39
     *
40
     * @return \Illuminate\Http\Response
41
     */
42
    public function calculateGrossPrice(Request $request)
43
    {
44
        if (!$request->has('netPrice')) {
45
            return Response::json([
46
                'error' => "The 'netPrice' parameter is missing",
47
            ], 422);
48
        }
49
50
        $valid_vat_id = null;
51
        $valid_company = false;
52
        if ($request->has('vat_number')) {
53
            $valid_company = $this->validateVATID($request->get('vat_number'));
54
            $valid_company = $valid_company['is_valid'];
55
            $valid_vat_id = $valid_company;
56
        }
57
58
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('gross_pric..._id' => $valid_vat_id); (array<string,double|boolean|null>) is incompatible with the return type documented by Mpociot\VatCalculator\Ht...er::calculateGrossPrice of type Illuminate\Http\Response.

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...
59
            'gross_price'   => $this->calculator->calculate($request->get('netPrice'), $request->get('country'), $request->get('postal_code'), $valid_company),
60
            'net_price'     => $this->calculator->getNetPrice(),
61
            'tax_rate'      => $this->calculator->getTaxRate(),
62
            'tax_value'     => $this->calculator->getTaxValue(),
63
            'valid_vat_id'  => $valid_vat_id,
64
        ];
65
    }
66
67
    /**
68
     * Returns the tax rate for the given country.
69
     *
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function getCountryCode()
73
    {
74
        return [
75
            'country_code' => $this->calculator->getIPBasedCountry(),
76
        ];
77
    }
78
79
    /**
80
     * Returns the tax rate for the given country.
81
     *
82
     * @param string $vat_id
83
     *
84
     * @throws \Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException
85
     *
86
     * @return \Illuminate\Http\Response
87
     */
88
    public function validateVATID($vat_id)
89
    {
90
        try {
91
            $isValid = $this->calculator->isValidVATNumber($vat_id);
92
            $message = '';
93
        } catch (VATCheckUnavailableException $e) {
94
            $isValid = false;
95
            $message = $e->getMessage();
96
        }
97
98
        return [
99
            'vat_id'   => $vat_id,
100
            'is_valid' => $isValid,
101
            'message'  => $message,
102
        ];
103
    }
104
}
105