Issues (41)

src/Repository.php (4 issues)

Severity
1
<?php
2
3
/**
4
 * MÓDULO DE EMISIÓN ELECTRÓNICA F72X
5
 * UBL 2.1
6
 * Version 1.0
7
 * 
8
 * Copyright 2019, Jaime Cruz
9
 */
10
11
namespace F72X;
12
13
use ZipArchive;
14
use F72X\F72X;
15
use F72X\Tools\ZipFile;
16
use F72X\Exception\FileException;
17
18
class Repository {
19
20
    public static function saveDocument($documentName, $billContent) {
21
        self::saveFile("xml/$documentName.xml", $billContent);
22
    }
23
24
    /**
25
     * 
26
     * @param string $documentName
27
     * @param boolean $throwEx set to *false*, to avoid an exception if the file doesn't exist
28
     */
29
    public static function removeFiles($documentName, $throwEx = true) {
30
        $rp = self::getRepositoryPath();
31
        self::removeFile("$rp/xml/$documentName.xml", $throwEx);
32
        self::removeFile("$rp/input/$documentName.json", $throwEx);
33
        self::removeFile("$rp/sxml/S-$documentName.xml", $throwEx);
34
        self::removeFile("$rp/zip/$documentName.zip", $throwEx);
35
        self::removeFile("$rp/pdf/$documentName.pdf", $throwEx);
36
        self::removeFile("$rp/ticket/$documentName.txt", $throwEx);
37
        self::removeFile("$rp/cdr/R$documentName.zip", $throwEx);
38
    }
39
40
    /**
41
     * 
42
     * @param string $documentName
43
     */
44
    public static function removeInputBasedFiles($documentName) {
45
        $rp = self::getRepositoryPath();
46
        self::removeFile("$rp/xml/$documentName.xml", false);
47
        self::removeFile("$rp/sxml/S-$documentName.xml", false);
48
        self::removeFile("$rp/zip/$documentName.zip", false);
49
        self::removeFile("$rp/pdf/$documentName.pdf", false);
50
    }
51
52
    public static function saveDocumentInput($documentName, $billContent) {
53
        self::saveFile("input/$documentName.json", $billContent);
54
    }
55
56
    public static function saveSignedXml($documentName, $billContent) {
57
        self::saveFile("sxml/S-$documentName.xml", $billContent);
58
    }
59
60
    public static function saveTicket($documentName, $ticket) {
61
        self::saveFile("ticket/$documentName.txt", $ticket, true);
62
    }
63
64
    public static function saveCdr($documentName, $billContent) {
65
        self::saveFile("cdr/R$documentName.zip", $billContent, true);
66
    }
67
68
    public static function savePDF($documentName, $fileContent) {
69
        self::saveFile("pdf/$documentName.pdf", $fileContent);
70
    }
71
72
    public static function zipDocument($documentName) {
73
        $rp = self::getRepositoryPath();
74
        $zipPath = "$rp/zip/$documentName.zip";
75
        $xmlPath = "$rp/sxml/S-$documentName.xml";
76
        $xmlFileName = "$documentName.xml";
77
        if (F72X::isRunninOnGAE()) {
78
            $zip = new ZipFile();
79
            $zip->addFile(file_get_contents($xmlPath), $xmlFileName);
80
            file_put_contents($zipPath, $zip->file());
81
        } else {
82
            $zip = new ZipArchive();
83
            if ($zip->open($zipPath, ZipArchive::CREATE) === TRUE) {
84
                $zip->addFile($xmlPath, $xmlFileName);
85
                $zip->close();
86
            }
87
        }
88
    }
89
90
    public static function billExist($documentName) {
91
        $rp = self::getRepositoryPath();
92
        return fileExists("$rp/xml/$documentName.xml");
93
    }
94
95
    public static function cdrExist($documentName) {
96
        $rp = self::getRepositoryPath();
97
        return fileExists("$rp/cdr/R$documentName.zip");
98
    }
99
100
    public static function getRepositoryPath() {
101
        return Company::getRepositoryPath();
102
    }
103
104
    public static function getXmlPath($documentName) {
105
        $rp = self::getRepositoryPath();
106
        return "$rp/xml/$documentName.xml";
107
    }
108
109
    public static function getBillInputPath($documentName) {
110
        $rp = self::getRepositoryPath();
111
        return "$rp/input/$documentName.json";
112
    }
113
114
    public static function getSignedBillPath($documentName) {
115
        $rp = self::getRepositoryPath();
116
        return "$rp/sxml/S-$documentName.xml";
117
    }
118
119
    public static function getZippedBillPath($documentName) {
120
        $rp = self::getRepositoryPath();
121
        return "$rp/zip/$documentName.zip";
122
    }
123
124
    public static function getDocumentTicketPath($documentName) {
125
        $rp = self::getRepositoryPath();
126
        return "$rp/ticket/$documentName.txt";
127
    }
128
129
    public static function getBillInputContent($documentName) {
130
        $filePath = self::getBillInputPath($documentName);
131
        if (file_exists($filePath)) {
132
            return file_get_contents($filePath);
133
        }
134
        throw new FileException("El archivo: $filePath no existe.");
135
    }
136
137
    public static function getZipContent($documentName) {
138
        return file_get_contents(self::getZippedBillPath($documentName));
139
    }
140
141
    public static function getDocumentTicketContent($documentName) {
142
        return file_get_contents(self::getDocumentTicketPath($documentName));
143
    }
144
145
    public static function getPdfPath($documentName) {
146
        $rp = self::getRepositoryPath();
147
        return "$rp/pdf/$documentName.pdf";
148
    }
149
150
    private static function saveFile($filePath, $fileContent, $overwrite = false) {
151
        $rp = self::getRepositoryPath();
152
        self::writeFile("$rp/$filePath", $fileContent, $overwrite);
153
    }
154
155
    public static function removeFile($filePath, $throwEx = true) {
156
        if (!file_exists($filePath)) {
157
            if ($throwEx) {
158
                throw new FileException("El archivo: $filePath no existe.");
159
            }
160
            return;
161
        }
162
        unlink($filePath);
163
    }
164
165
    public static function getTicketInfo($documentName) {
166
        return self::getDocumentTicketContent($documentName);
167
    }
168
169
    public static function getCdrInfo($documentName) {
170
        $rp = self::getRepositoryPath();
171
        $zipPath = "$rp/cdr/R$documentName.zip";
172
        $xmlFile = "R-$documentName.xml";
173
        if (F72X::isRunninOnGAE()) {
174
            $xmlString = ZipFile::getEntry($zipPath, $xmlFile);
175
            if ($xmlString === 0) {
176
                throw new FileException("No se encontró el archivo $zipPath");
177
            }
178
        } else {
179
            $zip = new ZipArchive();
180
            if ($zip->open($zipPath) === true) {
181
                $xmlString = $zip->getFromName($xmlFile);
182
                $zip->close();
183
            } else {
184
                throw new FileException("No se encontró el archivo $zipPath");
185
            }
186
        }
187
        $info = self::getMapCdr($xmlString);
188
        return $info;
189
    }
190
191
    private static function getMapCdr($xmlString) {
192
        $xmlStringI1 = str_replace(['ar:', 'ext:', 'cac:', 'cbc:'], '', $xmlString);
193
        $SimpleXml = simplexml_load_string($xmlStringI1);
194
        $origin = json_decode(json_encode($SimpleXml), 1);
195
        $respNode = $origin['DocumentResponse'];
196
        return [
197
            'id' => $origin['ID'],
198
            'documentId' => $respNode['DocumentReference']['ID'],
199
            'receiverId' => $respNode['RecipientParty']['PartyIdentification']['ID'],
200
            'issueDate' => $origin['IssueDate'],
201
            'issueTime' => $origin['IssueTime'],
202
            'responseDate' => $origin['ResponseDate'],
203
            'responseTime' => $origin['ResponseTime'],
204
            'responseCode' => $respNode['Response']['ResponseCode'],
205
            'responseDesc' => $respNode['Response']['Description']
206
        ];
207
    }
208
209
    public static function xmlStream($documentName) {
210
        $filePath = self::getXmlPath($documentName);
211
        if (file_exists($filePath)) {
212
            header('Content-Type: application/xml');
213
            header("Content-Disposition: attachment;filename=$documentName.xml");
214
            header('Cache-Control:max-age=0');
215
            echo file_get_contents($filePath);
216
            exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
217
        }
218
        throw new FileException("El archivo: $filePath no existe.");
219
    }
220
221
    public static function signedXmlStream($documentName) {
222
        $filePath = self::getSignedBillPath($documentName);
223
        if (file_exists($filePath)) {
224
            header('Content-Type: application/xml');
225
            header("Content-Disposition: attachment;filename=S-$documentName.xml");
226
            header('Cache-Control:max-age=0');
227
            echo file_get_contents($filePath);
228
            exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
229
        }
230
        throw new FileException("El archivo: $filePath no existe.");
231
    }
232
233
    public static function getPdfStream($documentName) {
234
        $filePath = self::getPdfPath($documentName);
235
        if (file_exists($filePath)) {
236
            return file_get_contents($filePath);
237
        }
238
        throw new FileException("El archivo: $filePath no existe.");
239
    }
240
241
    public static function pdfStream($documentName, $browserView = false) {
242
        $stream = self::getPdfStream($documentName);
243
        header('Content-Type: application/pdf');
244
        if (!$browserView) {
245
            header("Content-Disposition: attachment;filename=$documentName.pdf");
246
        }
247
        header('Cache-Control:max-age=0');
248
        echo $stream;
249
        exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
250
    }
251
252
    public static function billInputStream($documentName) {
253
        $filePath = self::getBillInputPath($documentName);
254
        if (file_exists($filePath)) {
255
            header('Content-Type: application/json');
256
            header("Content-Disposition: attachment;filename=$documentName.json");
257
            header('Cache-Control:max-age=0');
258
            echo file_get_contents($filePath);
259
            exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
260
        }
261
        throw new FileException("El archivo: $filePath no existe.");
262
    }
263
264
    public static function writeFile($filePath, $fileContent, $overwrite = false) {
265
        if (file_exists($filePath) && !$overwrite) {
266
            throw new FileException("El archivo: $filePath ya existe.");
267
        }
268
        file_put_contents($filePath, $fileContent);
269
    }
270
271
}
272