Completed
Pull Request — master (#7)
by
unknown
01:14
created

AbstractXmlLoteria   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 90
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
putFileName() 0 1 ?
B formatResultXpathToConcursoArray() 0 25 2
A getSimpleXml() 0 4 1
A findByConcurso() 0 13 2
A findLatestConcursos() 0 15 2
A findLastConcurso() 0 13 2
1
<?php
2
3
namespace LoteriaApi\Provider\Reader;
4
5
use LoteriaApi\Config;
6
7
abstract class AbstractXmlLoteria implements IFinder
8
{
9
    protected $configPath;
10
    protected $configDatasource;
11
    protected $filename;
12
13
    public function __construct(Config $configPath, Config $configDatasource)
14
    {
15
        $this->configPath = $configPath;
16
        $this->configDatasource = $configDatasource;
17
        $this->putFileName();
18
    }
19
20
    abstract protected function putFileName();
21
22
    private function formatResultXpathToConcursoArray($resultXpath)
23
    {
24
        $arrayConcursos = [];
25
26
        foreach ($resultXpath as $key => $concurso) {
27
            $arrayConcursos[$key]['concurso'] = (string) $concurso->attributes()->numero;
28
29
            $children = $concurso[0]->children();
30
31
            $arrayConcursos[$key]['data'] = (string) $children->data;
32
            $arrayConcursos[$key]['dezenas'] = (array) $children->dezenas->children()->dezena;
33
            $arrayConcursos[$key]['arrecadacao'] = (string) $children->arrecadacao;
34
            $arrayConcursos[$key]['total_ganhadores_primeiro_premio'] = (string) $children->total_ganhadores_primeiro_premio;
35
            $arrayConcursos[$key]['total_ganhadores_segundo_premio'] = (string) $children->total_ganhadores_segundo_premio;
36
            $arrayConcursos[$key]['total_ganhadores_terceiro_premio'] = (string) $children->total_ganhadores_terceiro_premio;
37
            $arrayConcursos[$key]['valor_ganhadores_primeiro_premio'] = (string) $children->valor_ganhadores_primeiro_premio;
38
            $arrayConcursos[$key]['valor_ganhadores_segundo_premio'] = (string) $children->valor_ganhadores_segundo_premio;
39
            $arrayConcursos[$key]['valor_ganhadores_terceiro_premio'] = (string) $children->valor_ganhadores_terceiro_premio;
40
            $arrayConcursos[$key]['acumulado'] = (string) $children->acumulado;
41
            $arrayConcursos[$key]['valor_acumulado'] = (string) $children->valor_acumulado;
42
            $arrayConcursos[$key]['valor_estimado_proximo_concurso'] = (string) $children->valor_estimado_proximo_concurso;
43
        }
44
45
        return $arrayConcursos;
46
    }
47
48
    private function getSimpleXml()
49
    {
50
        return simplexml_load_file($this->filename);
51
    }
52
53
    public function findByConcurso($nrconcurso)
54
    {
55
        $concurso = $this->getSimpleXml()
56
            ->xpath("/concursos/concurso[@numero='{$nrconcurso}']");
57
58
        if (!isset($concurso[0])) {
59
             throw new \InvalidArgumentException("Concurso does not exist");
60
        }
61
        
62
        $concurso = $this->formatResultXpathToConcursoArray($concurso);
63
64
        return $concurso[0];
65
    }
66
67
    public function findLatestConcursos($nrconcursoRange)
68
    {
69
        // Get last concurso
70
        $lastConcurso = $this->findLastConcurso();
71
72
        // Range bounds
73
        $rangeIni = (($lastConcurso['concurso'] + 1) - $nrconcursoRange);
74
        $rangeEnd = $lastConcurso['concurso'];
75
76
        foreach (range($rangeIni, $rangeEnd) as $nrconcurso) {
77
            $concursos[] = $this->findByConcurso($nrconcurso);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$concursos was never initialized. Although not strictly required by PHP, it is generally a good practice to add $concursos = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
78
        }
79
80
        return $concursos;
0 ignored issues
show
Bug introduced by
The variable $concursos does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
81
    }
82
83
    public function findLastConcurso()
84
    {
85
        $concurso = $this->getSimpleXml()
86
            ->xpath("(/concursos/concurso)[last()]");
87
        
88
        $concurso = $this->formatResultXpathToConcursoArray($concurso);
89
90
        if (!isset($concurso[0])) {
91
             throw new \InvalidArgumentException("Last concurso not found");
92
        }
93
94
        return $concurso[0];
95
    }
96
}
97