Completed
Push — master ( 2f2450...0a6175 )
by Adriano
06:10
created

VendaApi::vender()   F

Complexity

Conditions 9
Paths 436

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 3.5744
c 0
b 0
f 0
cc 9
eloc 28
nc 436
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: a.moreira
5
 * Date: 20/10/2016
6
 * Time: 09:51
7
 */
8
9
namespace Integracao\ControlPay\API;
10
11
use GuzzleHttp\Exception\RequestException;
12
use Integracao\ControlPay\AbstractAPI;
13
use Integracao\ControlPay\Client;
14
use Integracao\ControlPay\Constants\ControlPayParameterConst;
15
use Integracao\ControlPay\Helpers\SerializerHelper;
16
use Integracao\ControlPay\Contracts;
17
18
/**
19
 * Class VenderApi
20
 * @package Integracao\ControlPay\API
21
 */
22
class VendaApi extends AbstractAPI
23
{
24
    /**
25
     * @var PedidoApi
26
     */
27
    private $pedidoApi;
28
29
    /**
30
     * VenderApi constructor.
31
     */
32
    public function __construct(Client $client = null)
33
    {
34
        parent::__construct('venda', $client);
35
        $this->pedidoApi = new PedidoApi($client);
0 ignored issues
show
Bug introduced by
It seems like $client defined by parameter $client on line 32 can be null; however, Integracao\ControlPay\API\PedidoApi::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
36
    }
37
38
    /**
39
     * @param Contracts\Venda\VenderRequest $venderRequest
40
     * @return Contracts\Venda\VenderResponse
41
     * @throws \Exception
42
     */
43
    public function vender(Contracts\Venda\VenderRequest $venderRequest)
44
    {
45
        try{
46
47
            if(empty($venderRequest->getTerminalId()))
48
                $venderRequest->setTerminalId(
49
                    $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_TERMINAL_ID)
50
                );
51
52
            if(empty($venderRequest->getFormaPagamentoId()))
53
                $venderRequest->setFormaPagamentoId(
54
                    $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_FORMA_PAGAMENTO_ID)
55
                );
56
57
            if(empty($venderRequest->isAguardarTefIniciarTransacao()))
58
                $venderRequest->setAguardarTefIniciarTransacao(
59
                    boolval(
60
                        $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_FORMA_AGUARDA_TEF)
61
                    )
62
                );
63
64
            foreach ($venderRequest->getProdutosVendidos() as $key => $produto)
65
            {
66
                if(empty($produto->getId()))
67
                    $venderRequest->getProdutosVendidos()[$key]->setId(
68
                        $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_PRODUTO_ID)
69
                    );
70
71
                if(empty($produto->getQuantidade()))
72
                    $venderRequest->getProdutosVendidos()[$key]->setQuantidade(
73
                        $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_PRODUTO_QTDE)
74
                    );
75
            }
76
77
            $this->response = $this->_httpClient->post(__FUNCTION__,[
78
                'body' => json_encode($venderRequest),
79
            ]);
80
81
            return SerializerHelper::denormalize(
82
                $this->response->json(),
83
                Contracts\Venda\VenderResponse::class
84
            );
85
        }catch (RequestException $ex) {
86
            $this->requestException($ex);
87
        }catch (\Exception $ex){
88
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex);
89
        }
90
    }
91
92
    /**
93
     * @param Contracts\Venda\VenderComPedidoRequest $venderComPedidoRequest
94
     * @return Contracts\Venda\VenderResponse
95
     * @throws \Exception
96
     */
97
    public function venderComPedido(Contracts\Venda\VenderComPedidoRequest $venderComPedidoRequest)
98
    {
99
        try{
100
            $response = $this->pedidoApi->insert($venderComPedidoRequest->getPedidoInserirRequest());
101
102
            if(empty($response->getPedido()->getId()))
103
                throw new \Exception("Falha ao inserir pedido");
104
105
            $venderComPedidoRequest->getInventarioVenderRequest()->setPedidoId(
106
                $response->getPedido()->getId()
107
            );
108
109
            return $this->vender($venderComPedidoRequest->getInventarioVenderRequest());
110
        }catch (RequestException $ex) {
111
            $this->requestException($ex);
112
        }catch (\Exception $ex){
113
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex);
114
        }
115
    }
116
117
    /**
118
     * @param Contracts\Venda\CancelarVendaRequest $cancelarVendaRequest
119
     * @return Contracts\Venda\CancelarVendaResponse
120
     * @throws \Exception
121
     */
122
    public function cancelarVenda(Contracts\Venda\CancelarVendaRequest $cancelarVendaRequest)
123
    {
124
        try{
125
126
            if(empty($cancelarVendaRequest->getTerminalId()))
127
                $cancelarVendaRequest->setTerminalId(
128
                    $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_TERMINAL_ID)
129
                );
130
131
            if(empty($cancelarVendaRequest->getSenhaTecnica()))
132
                $cancelarVendaRequest->setSenhaTecnica(
133
                    $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_SENHA_TECNICA)
134
                );
135
136
            if(empty($cancelarVendaRequest->isAguardarTefIniciarTransacao()))
137
                $cancelarVendaRequest->setAguardarTefIniciarTransacao(
138
                    boolval(
139
                        $this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_FORMA_AGUARDA_TEF)
140
                    )
141
                );
142
143
144
            $this->response = $this->_httpClient->post(__FUNCTION__,[
145
                'body' => json_encode($cancelarVendaRequest),
146
            ]);
147
148
            return SerializerHelper::denormalize(
149
                $this->response->json(),
150
                Contracts\Venda\CancelarVendaResponse::class
151
            );
152
        }catch (RequestException $ex) {
153
            $this->requestException($ex);
154
        }catch (\Exception $ex){
155
            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex);
156
        }
157
    }
158
159
160
//    /**
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
161
//     * API Provavelmente descontinuada
162
//     *
163
//     * @return Contracts\Venda\ConsultarVendasResponse
164
//     * @throws \Exception
165
//     */
166
//    public function consultarVendas(array $ids = [])
167
//    {
168
//        try{
169
//            $this->response = $this->_httpClient->post(__FUNCTION__, [
170
//                'body' => json_encode(array_map(function($id){
171
//                    return [
172
//                        'Id' => (string)$id
173
//                    ];
174
//                },$ids))
175
//            ]);
176
//
177
//            return SerializerHelper::denormalize(
178
//                $this->response->json(),
179
//                Contracts\Venda\ConsultarVendasResponse::class
180
//            );
181
//        }catch (RequestException $ex) {
182
//            $this->response = $ex->getResponse();
183
//            $responseBody = $ex->getResponse()->json();
184
//            throw new \Exception($responseBody['message']);
185
//        }catch (\Exception $ex){
186
//            throw new \Exception($ex->getMessage(), $ex->getCode(), $ex);
187
//        }
188
//    }
189
190
}