Status::getStatus()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.439
cc 5
eloc 21
nc 4
nop 0
1
<?php namespace SOSTheBlack\Moip;
2
3
use Exception;
4
5
/**
6
 * Verificação de status da conta do MoIP. Atualmente somente com suporte à verificação de saldo e ultimas transações.
7
 * 
8
 * @author Herberth Amaral
9
 * @version 0.0.2
10
 * @package MoIP
11
 */
12
class Status
13
{
14
    /**
15
     * url for login
16
     * 
17
     * @var string
18
     */
19
    private $url_login = "https://www.moip.com.br/j_acegi_security_check";
20
21
    /**
22
     * setCredennciais
23
     * 
24
     * @param type $username 
25
     * @param type $password 
26
     * @return \SOSTheBlack\Moip\Status
27
     */
28
    function setCredenciais($username,$password)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
    {
30
        $this->username = $username;
0 ignored issues
show
Bug introduced by
The property username does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
        $this->password = $password;
0 ignored issues
show
Bug introduced by
The property password does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        return $this;
33
    }
34
35
    /**
36
     * getStatus
37
     * 
38
     * @return \SOSTheBlack\Moip\Status
39
     */
40
    function getStatus()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
    {
42
        if (!isset($this->username) or !isset($this->password))
43
            throw new Exception("Usuário ou senha não especificados.");
44
45
46
        $ch = curl_init($this->url_login);
47
        curl_setopt($ch, CURLOPT_POST      ,1);
48
        curl_setopt($ch, CURLOPT_POSTFIELDS    ,"j_username=$this->username&j_password=$this->password");
49
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
50
        curl_setopt($ch, CURLOPT_HEADER      ,0);
51
        curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
52
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
53
        $page = curl_exec($ch);
54
        $error = curl_error($ch);
55
56
        if (!empty($error))
57
        {
58
            $errno = curl_errno($ch);
59
            throw new Exception("Ooops, ocorreu um erro ao tentar recuperar os status #$errno: $error");
60
        }
61
62
        if (stristr($page,"Login e senha incorretos"))
63
            throw new Exception('Login incorreto');
64
65
        $this->saldo = pq('div.textoCinza11 b.textoAzul15')->text();
0 ignored issues
show
Bug introduced by
The property saldo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
        $this->saldo_a_receber = pq('div.textoCinza11 b.textoAzul11')->text();
0 ignored issues
show
Bug introduced by
The property saldo_a_receber does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
68
        $this->ultimas_transacoes = $this->getLastTransactions($page);
0 ignored issues
show
Bug introduced by
The property ultimas_transacoes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
70
        return $this;
71
    }
72
73
    /**
74
     * getLastTransactions
75
     * 
76
     * @param  string $page 
77
     * @return array
78
     */
79
    private function getLastTransactions($page)
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed.

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

Loading history...
80
    {
81
        $selector = 'div.conteudo>div:eq(1)>div:eq(1)>div:eq(1)>div:eq(0) div.box table[cellpadding=5]>tbody tr';
82
83
        if (substr(utf8_encode(pq($selector)->find('td:eq(0)')->html()),0,7)=="Nenhuma")
84
            return null;
85
86
        $ultimas_transacoes = array();
87
        foreach(pq($selector) as $tr)
88
        {
89
            $tds = pq($tr);
90
91
            $transacao = array('data'=>$tds->find('td:eq(0)')->html(),
92
                'nome'=>$tds->find('td:eq(1)')->html(),
93
                'pagamento'=>$tds->find('td:eq(2)')->html(),
94
                'adicional'=>$tds->find('td:eq(3)')->html(),
95
                'valor'=>$tds->find('td:eq(4)')->html()
96
            );
97
            $ultimas_transacoes[] = $transacao;
98
        }
99
100
        return $ultimas_transacoes;
101
    }
102
}