Completed
Push — master ( c985f8...7064aa )
by Kazi Mainuddin
03:41
created

BizVerifier::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Tzsk\Payu\Verifiers;
4
5
class BizVerifier extends AbstractVerifier
6
{
7
    /**
8
     * Command to Pass to the PayuService.
9
     *
10
     * @var string
11
     */
12
    protected $command = 'verify_payment';
13
14
    /**
15
     * Request for Verification Status.
16
     *
17
     * @return $this
18
     */
19 View Code Duplication
    public function verify()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
20
    {
21
        try {
22
            $response = $this->client->request('POST', $this->url(), [
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
                'form_params' => $this->fields()
24
            ]);
25
    
26
            return $this->makeResponse(json_decode($response->getBody()));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->makeRespon...$response->getBody())); (array) is incompatible with the return type declared by the abstract method Tzsk\Payu\Verifiers\AbstractVerifier::verify of type object.

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...
27
        } catch (\Exception $e) {
28
            return (object) ['status' => false, 'message' => $e->getMessage()];
29
        }
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    protected function url()
36
    {
37
        return "https://{$this->prefix()}.payu.in/merchant/postservice?form=2";
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    protected function fields()
44
    {
45
        $txn_string = implode("|", $this->txnIds);
46
        $hash_str = $this->config->getKey().'|'.$this->command.'|'.$txn_string.'|'.$this->config->getSalt();
47
48
        return [
49
            'key' => $this->config->getKey(),
50
            'hash' => strtolower(hash('sha512', $hash_str)),
51
            'var1' => $txn_string,
52
            'command' => $this->command
53
        ];
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    protected function prefix()
60
    {
61
        $env = $this->config->getEnv();
62
63
        return ($env == 'test') ? $env : 'info';
64
    }
65
66
    /**
67
     * @param object $data
68
     * @return array
69
     */
70
    protected function makeResponse($data)
71
    {
72
        if ($data->status < 1) {
73
            return (object) ['status' => false, 'data' => [], 'message' => $data->msg];
74
        }
75
        
76
        $response = ['status' => true, 'data' => [], 'message' => ''];
77
78
        foreach($this->txnIds as $id) {
79
            if (! empty($data->transaction_details->{$id})) {
80
                $response['data'][$id] = $this->getInstance($data->transaction_details->{$id});
81
            }
82
        }
83
84
        return (object) $response;
85
    }
86
}
87