Passed
Push — master ( c4fb31...6466f2 )
by Antônio
01:43
created

ApiData::addHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace OBRSDK\HttpClient\Nucleo;
4
5
/**
6
 * @author Antonio
7
 * @since 01/11/2017
8
 */
9
class ApiData {
10
11
    /**
12
     *
13
     * @var array
14
     */
15
    private $data;
16
17
    /**
18
     *
19
     * @var int
20
     */
21
    private $dataSize = 0;
22
23
    public function __construct($data = null) {
24
        if (is_null($data)) {
25
            $data = [];
26
        }
27
28
        if (!is_array($data)) {
29
            throw new \Exception("Parametro data deve ser um formato array em OBRSDK\HttpClient\Nucleo\HttpCliente->request");
30
        }
31
32
        $this->dataSize = count($data);
33
        $this->data = $data;
34
35
        $this->verificarSeArquivo();
36
    }
37
38
    /**
39
     * 
40
     * @return array
41
     */
42
    public function getData() {
43
        return $this->data;
44
    }
45
46
    public function getDataSize() {
47
        return $this->dataSize;
48
    }
49
50
    /**
51
     * 
52
     * @param array $headers
53
     */
54
    public function addHeaders(array $headers) {
55
        if (count($headers) == 0) {
56
            return;
57
        }
58
59
        $this->data = array_merge([
60
            'headers' => $headers
61
                ], $this->data);
62
63
        $this->dataSize += 1;
64
    }
65
66
    private function verificarSeArquivo() {
67
        if (!isset($this->data['__uploadfile__'])) {
68
            return;
69
        }
70
71
        $this->data = [
72
            'multipart' => [
73
                [
74
                    'name' => 'uploadArquivo',
75
                    'contents' => file_get_contents($this->data['__uploadfile__']),
76
                    'filename' => basename($this->data['__uploadfile__'])
77
                ]
78
            ]
79
        ];
80
81
        $this->dataSize = 1;
82
    }
83
84
}
85