Passed
Push — master ( b8e26c...2f113f )
by Remco
05:42
created

ReturnParameters   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A from_array() 0 17 4
A get_signature_data() 0 4 1
A get_order_id() 0 2 1
A get_status() 0 2 1
A contains() 0 7 3
A __construct() 0 5 1
1
<?php
2
/**
3
 * Return parameters
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
use InvalidArgumentException;
14
use Pronamic\WordPress\Pay\GatewayPostType;
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\GatewayPostType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Pronamic\WordPress\Pay\Plugin;
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\Plugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Pronamic\WordPress\Pay\Core\Gateway;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Pronamic\WordPress\Pay\Gateways\OmniKassa2\Gateway. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
18
/**
19
 * Return parameters
20
 *
21
 * @author  Remco Tolsma
22
 * @version 2.0.2
23
 * @since   2.0.2
24
 */
25
class ReturnParameters extends ResponseMessage {
26
	/**
27
	 * The "merchantOrderId" as used in the Order announce.
28
	 *
29
	 * @var string
30
	 */
31
	private $order_id;
32
33
	/**
34
	 * The status of the order, see below for more details.
35
	 *
36
	 * @var string
37
	 */
38
	private $status;
39
40
	/**
41
	 * Construct return parameters object.
42
	 *
43
	 * @param string $order_id  Order ID.
44
	 * @param string $status    Status.
45
	 * @param string $signature Signature.
46
	 */
47
	public function __construct( $order_id, $status, $signature ) {
48
		parent::__construct( $signature );
49
50
		$this->order_id = $order_id;
51
		$this->status   = $status;
52
	}
53
54
	/**
55
	 * Get order ID.
56
	 *
57
	 * @return string
58
	 */
59
	public function get_order_id() {
60
		return $this->order_id;
61
	}
62
63
	/**
64
	 * Get status.
65
	 *
66
	 * @return string
67
	 */
68
	public function get_status() {
69
		return $this->status;
70
	}
71
72
	/**
73
	 * Get signature data.
74
	 *
75
	 * The signature is calculated in the same way as other signatures. In this case, the two fields (in order: order_id, status) are used as input.
76
	 *
77
	 * @return array
78
	 */
79
	public function get_signature_data() {
80
		return array(
81
			$this->get_order_id(),
82
			$this->get_status(),
83
		);
84
	}
85
86
	/**
87
	 * Check if data array contains return parameters.
88
	 *
89
	 * @param array $data Data array.
90
	 * @return bool True if array contains return parameters, false otherwise.
91
	 */
92
	public static function contains( array $data ) {
93
		return (
94
			array_key_exists( 'order_id', $data )
95
				&&
96
			array_key_exists( 'status', $data )
97
				&&
98
			array_key_exists( 'signature', $data )
99
		);
100
	}
101
102
	/**
103
	 * Get return parameters from the specifieid data array.
104
	 *
105
	 * @param array $data Data array.
106
	 * @return ReturnParameters
107
	 * @throws InvalidArgumentException Throws invalid argument exception when array does not contains the required keys.
108
	 */
109
	public static function from_array( array $data ) {
110
		if ( ! array_key_exists( 'order_id', $data ) ) {
111
			throw new InvalidArgumentException( 'Data array must contain `order_id` field.' );
112
		}
113
114
		if ( ! array_key_exists( 'status', $data ) ) {
115
			throw new InvalidArgumentException( 'Data array must contain `status` field.' );
116
		}
117
118
		if ( ! array_key_exists( 'signature', $data ) ) {
119
			throw new InvalidArgumentException( 'Data array must contain `signature` field.' );
120
		}
121
122
		return new self(
123
			$data['order_id'],
124
			$data['status'],
125
			$data['signature']
126
		);
127
	}
128
}
129