Passed
Push — master ( 58a500...0a2fd6 )
by Thalles
02:41 queued 01:17
created

Rastreio::getPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace ThallesDKoester\Entregas;
4
5
use Exception;
6
use Sunra\PhpSimple\HtmlDomParser;
7
8
/**
9
 * Thalles D. Koester | Class Rastreio
10
 *
11
 * @author  Thalles D. koester <[email protected]>
12
 * @package ThallesDKoester\Entregas
13
 */
14
class Rastreio
15
{
16
    use Curl;
17
18
    const ALTERNATIVE = 'https://www2.correios.com.br/sistemas/rastreamento/resultado_semcontent.cfm';
19
20
    /** @var string */
21
    private $code;
22
23
    /** @var string */
24
    private $error;
25
26
    /** @var array */
27
    private $options = [];
28
29
    /**
30
     * Rastreio constructor.
31
     * @param string     $code
32
     * @param array|null $options
33
     */
34
    public function __construct(string $code, ?array $options = [])
35
    {
36
        $this->setCode($code);
37
38
        if ($options) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $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...
39
            $this->setOptions($options);
40
        }
41
    }
42
43
    /**
44
     * @return array|null
45
     */
46
    public function getPath(): ?array
47
    {
48
        $url = self::ALTERNATIVE;
49
        try {
50
            $data = $this->request($url, 'POST', ['Objetos' => $this->code]);
51
        } catch (Exception $e) {
52
            $this->error = $e;
53
            return null;
54
        }
55
56
        return $this->parseHtml($data);
57
    }
58
59
    /**
60
     * @param string $code
61
     */
62
    private function setCode(string $code): void
63
    {
64
        if (empty($code)) {
65
            $this->error = 'Insira o código de rastreio para proceguir';
66
        }
67
        $this->code = $code;
68
    }
69
70
    /**
71
     * @param array $options
72
     */
73
    private function setOptions(array $options): void
74
    {
75
        foreach ($options as $key => $value) {
76
            $this->options[$key] = $value;
77
        }
78
    }
79
80
    /**
81
     * @param string $hmtl
82
     * @return array|null
83
     */
84
    private function parseHtml(string $hmtl): ?array
85
    {
86
        $dom = HtmlDomParser::str_get_html($hmtl);
87
        $table = $dom->find('.listEvent');
88
89
        if (!$table) {
90
            $this->error = 'Erro no parsing';
91
            return null;
92
        }
93
94
        $return = [];
95
        foreach ($table->children() as $key => $value) {
96
            $datetime = explode('<br />', $value->find('.sroDtEvent')->plaintext);
97
            $return[] = [
98
                'date' => $datetime[0],
99
                'time' => $datetime[1],
100
                'location' => strip_tags($datetime[2]),
101
                'action' => strip_tags($value->find('.sroLbEvent')->plaintext)
102
            ];
103
        }
104
        return $return;
105
    }
106
}