Retrieve::getIncludeFiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the cfdi-xslt project.
5
 *
6
 * (c) Kinedu
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kinedu\CfdiXslt;
13
14
use SimpleXMLElement;
15
use DOMDocument;
16
use DOMXpath;
17
18
class Retrieve
19
{
20
    /**
21
     * SAT Endpoint
22
     *
23
     * @var string
24
     */
25
    const SAT_ENDPOINT = 'http://www.sat.gob.mx/sitio_internet/cfd/3/cadenaoriginal_3_3/cadenaoriginal_3_3.xslt';
26
27
    /**
28
     * @return array
29
     */
30
    protected function getIncludeFiles(): array
31
    {
32
        $files[] = static::SAT_ENDPOINT;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$files was never initialized. Although not strictly required by PHP, it is generally a good practice to add $files = 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...
33
34
        $xml = new SimpleXMLElement(
35
            file_get_contents(static::SAT_ENDPOINT)
36
        );
37
38
        foreach ($xml->xpath('//xsl:include') as $node) {
39
            $files[] = reset($node['href']);
40
        }
41
42
        return $files;
43
    }
44
45
    /**
46
     * @param string $directory
47
     *
48
     * @return bool
49
     */
50
    public function changeNodeReference(string $directory): bool
51
    {
52
        $file = $directory.$this->getFileName(static::SAT_ENDPOINT);
53
54
        $dom = new DOMDocument();
55
        $dom->load($file);
56
57
        $xpath = new DOMXpath($dom);
58
        $query = '//xsl:include';
59
60
        foreach ($xpath->query($query) as $node) {
61
            $href = $node->getAttribute('href');
62
63
            $node->setAttribute(
64
                'href',
65
                $this->getFileName($href)
66
            );
67
        }
68
69
        return $dom->save($file);
70
    }
71
72
    /**
73
     * @param string $directory
74
     *
75
     * @return bool
76
     */
77
    public function download(string $directory): bool
78
    {
79
        foreach ($this->getIncludeFiles() as $url) {
80
            $file = file_get_contents($url);
81
            $fileName = $this->getFileName($url);
82
83
            file_put_contents("{$directory}{$fileName}", $file);
84
        }
85
86
        $this->changeNodeReference($directory);
87
88
        return true;
89
    }
90
91
    /**
92
     * @param string $file
93
     *
94
     * @return string
95
     */
96
    protected function getFileName(string $file): string
97
    {
98
        return basename($file);
99
    }
100
}
101