Request   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 83
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getBody() 0 13 3
A getResponseClass() 0 4 1
A getData() 0 4 1
A setData() 0 5 1
A getOption() 0 8 3
A getOptions() 0 4 1
A setOptions() 0 5 1
1
<?php
2
namespace Loevgaard\Consignor\ShipmentServer\Request;
3
4
use Loevgaard\Consignor\ShipmentServer\Exception\EncodeJsonException;
5
use Loevgaard\Consignor\ShipmentServer\Response\Response;
6
use function Loevgaard\Consignor\ShipmentServer\encodeJson;
7
8
abstract class Request implements RequestInterface
9
{
10
    /*
11
     * Both $data and $options are only used in some requests, but we include them here to avoid code duplication
12
     */
13
    /**
14
     * @var array
15
     */
16
    protected $data;
17
18
    /**
19
     * @var array
20
     */
21
    protected $options;
22
23
    /**
24
     * @return array
25
     * @throws EncodeJsonException
26
     */
27
    public function getBody() : array
28
    {
29
        $body = [];
30
        if ($this->data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
31
            $body['data'] = encodeJson($this->data);
32
        }
33
34
        if ($this->options) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
35
            $body['Options'] = encodeJson($this->options);
36
        }
37
38
        return $body;
39
    }
40
41
    public function getResponseClass(): string
42
    {
43
        return Response::class;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function getData(): array
50
    {
51
        return (array)$this->data;
52
    }
53
54
    /**
55
     * @param array $data
56
     * @return Request
57
     */
58
    public function setData(array $data)
59
    {
60
        $this->data = $data;
61
        return $this;
62
    }
63
64
    public function getOption(string $option)
65
    {
66
        if (!$this->options || !array_key_exists($option, $this->options)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
67
            return null;
68
        }
69
70
        return $this->options[$option];
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getOptions(): array
77
    {
78
        return (array)$this->options;
79
    }
80
81
    /**
82
     * @param array $options
83
     * @return Request
84
     */
85
    public function setOptions(array $options)
86
    {
87
        $this->options = $options;
88
        return $this;
89
    }
90
}
91