Passed
Push — master ( d76556...230365 )
by Max Alex
01:42
created

Cliente::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
namespace codephix\Asaas;
4
5
use app\Asaas\Connection;
0 ignored issues
show
Bug introduced by
The type app\Asaas\Connection 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...
Bug introduced by
This use statement conflicts with another class in this namespace, codephix\Asaas\Connection. 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...
6
use app\Asaas\Exceptions\ClienteException;
0 ignored issues
show
Bug introduced by
The type app\Asaas\Exceptions\ClienteException 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...
7
use Exception;
8
9
/**
10
 * Class Cliente
11
 * @package app\Asaas
12
 *
13
 *
14
    \"name\": \"".((!empty($data['name'])) ? $data['name'] : '')."\",
15
    \"email\": \"".((!empty($data['email'])) ? $data['email'] : '')."\",
16
    \"company\": \"".((!empty($data['company'])) ? $data['company'] : '')."\",
17
    \"phone\": \"".((!empty($data['phone'])) ? $data['phone'] : '')."\",
18
    \"mobilePhone\": \"".((!empty($data['mobilePhone'])) ? $data['mobilePhone'] : '')."\",
19
    \"postalCode\": \"".((!empty($data['postalCode'])) ? $data['postalCode'] : '')."\",
20
    \"address\": \"".((!empty($data['address'])) ? $data['address'] : '')."\",
21
    \"addressNumber\": \"".((!empty($data['addressNumber'])) ? $data['addressNumber'] : '')."\",
22
    \"complement\": \"".((!empty($data['complement'])) ? $data['complement'] : '')."\",
23
    \"province\": \"".((!empty($data['province'] )) ? $data['province'] : '')."\",
24
    \"city\": \"".((!empty($data['city'])) ? $data['city'] : '')."\",
25
    \"state\": \"".((!empty($data['state'])) ? $data['state'] : '')."\",
26
    \"cpfCnpj\": \"".((!empty($data['cpfCnpj'])) ? $data['cpfCnpj'] : '')."\",
27
    \"additionalEmails\": \"".((!empty($data['additionalEmails'])) ? $data['additionalEmails'] : '')."\",
28
    \"notificationDisabled\": ".((!empty($data['notificationDisabled']) && $data['notificationDisabled'] == 1) ? 'true' : 'false').",
29
    \"externalReference\": \"".((!empty($data['externalReference'])) ? $data['externalReference'] : '')."\"
30
 */
31
32
class Cliente
33
{
34
    
35
    public $http;
36
    protected $cliente;
37
38
    public $cli;
39
40
    
41
    public function __construct()
42
    {
43
        $this->http = new Connection;
44
    }
45
    
46
    /**
47
     * Retorna array de clientes.
48
     * @return array
49
     */
50
    public function index()
51
    {
52
        return $this->http->get('/customers');
53
    }
54
55
    // Retorna a listagem de clientes
56
    public function getAll($filtros = false){
0 ignored issues
show
Unused Code introduced by
The parameter $filtros is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

56
    public function getAll(/** @scrutinizer ignore-unused */ $filtros = false){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
        return $this->http->get('/customers');
58
    }
59
60
    // Retorna os dados do cliente de acordo com o Id
61
    public function getById($id){
62
        return $this->http->get('/customers/'.$id);
63
    }
64
65
    // Retorna os dados do cliente de acordo com o Email
66
    public function getByEmail($email){
67
        $option = 'limit=1&email=' . $email;
68
        return $this->http->get('/customers', $option);
69
    }
70
71
    // Insere um novo cliente
72
    public function create(array $dadosCliente){
73
        $dadosCliente = $this->setCliente($dadosCliente);
74
        if(!empty($dadosCliente['error'])){
75
            return $dadosCliente;
76
        }else {
77
            return $this->http->post('/customers', $dadosCliente);
78
        }
79
    }
80
81
    // Atualiza os dados do cliente
82
    public function update($id, array $dadosCliente){
83
        $dadosCliente = $this->setCliente($dadosCliente);
84
        if(!empty($dadosCliente['error'])){
85
            return $dadosCliente;
86
        }else {
87
            return $this->http->post('/customers/' . $id, $dadosCliente);
88
        }
89
    }
90
91
    // Deleta uma cliente
92
    public function delete($id){
93
        return $this->http->get('/customers/'.$id,'','DELETE');
94
    }
95
96
97
98
99
    public function get($client_id) {
100
        return $this->http->get('/customers/' . $client_id);
101
    }
102
103
    /**
104
     * Cria um novo cliente no Asaas.
105
     * @param Array $cliente
106
     * @return Boolean
107
     */
108
    public function create2($cliente)
109
    {
110
        // Preenche as informações do cliente
111
        $cliente = $this->setCliente($cliente);
112
        
113
        // Faz o post e retorna array de resposta
114
        return $this->http->post('/customers', ['form_params' => $cliente]);
115
        
116
    }
117
    
118
    /**
119
     * Faz merge nas informações do cliente.
120
     * 
121
     * @see https://asaasv3.docs.apiary.io/#reference/0/clientes/criar-novo-cliente
122
     * @param Array $cliente
123
     * @return Array
124
     */
125
    public function setCliente($cliente)
126
    {
127
        try {
128
            if ( ! $this->cliente_valid($cliente) ) {
129
                return ClienteException::invalidClient();
130
            }
131
132
            $this->cliente = array(
133
                'name'                 => '',
134
                'cpfCnpj'              => '',
135
                'email'                => '',
136
                'phone'                => '',
137
                'mobilePhone'          => '',
138
                'address'              => '',
139
                'addressNumber'        => '',
140
                'complement'           => '',
141
                'province'             => '',
142
                'postalCode'           => '',
143
                'externalReference'    => '',
144
                'notificationDisabled' => '',
145
                'additionalEmails'     => ''
146
            );
147
            
148
            $this->cliente = array_merge($this->cliente, $cliente);
149
            return $this->cliente;
150
            
151
        } catch (Exception $e) {
152
            return 'Erro ao definir o cliente. - ' . $e->getMessage();
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Erro ao definir ... - ' . $e->getMessage() returns the type string which is incompatible with the documented return type array.
Loading history...
153
        }
154
    }
155
    
156
    /**
157
     * Verifica se os dados do cliente são válidos.
158
     * 
159
     * @param array $cliente
160
     * @return Boolean
161
     */
162
    public function cliente_valid($cliente)
163
    {
164
        return ! ( (empty($cliente['name']) OR empty($cliente['cpfCnpj']) OR empty($cliente['email'])) ? 1 : '' );
165
    }
166
}