Passed
Branch develop (5fc816)
by JAIME ELMER
01:58
created

FileService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMapCdr() 0 15 1
A getCdrInfo() 0 12 2
A doZip() 0 7 2
A getBase64() 0 2 1
1
<?php
2
3
/**
4
 * MÓDULO DE EMISIÓN ELECTRÓNICA F72X
5
 * UBL 2.1
6
 * Version 1.1
7
 * 
8
 * Copyright 2018, Jaime Cruz
9
 */
10
11
namespace F72X\Tools;
12
13
use ZipArchive;
14
use F72X\Company;
15
use F72X\Exception\FileException;
16
17
class FileService {
18
19
    public static function doZip($billName) {
20
        $fileDir = Company::getRepositoryPath();
21
        $zipFilename = explode('.', $billName)[0] . '.zip';
22
        $zip = new ZipArchive();
23
        if ($zip->open("$fileDir/zippedbill/$zipFilename", ZipArchive::CREATE) === TRUE) {
24
            $zip->addFile("$fileDir/signedbill/S-$billName", $billName);
25
            $zip->close();
26
        }
27
    }
28
29
    public static function getBase64($filePath) {
30
        return base64_encode(file_get_contents($filePath));
31
    }
32
33
    public static function getCdrInfo($billName) {
34
        $repository = Company::getRepositoryPath();
35
        $zip = new ZipArchive();
36
        $info = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $info is dead and can be removed.
Loading history...
37
        if ($zip->open("$repository/cdr/R$billName.zip") === true) {
38
            $xmlString = $zip->getFromName("R-$billName.xml");
39
            $info = self::getMapCdr($xmlString);
40
            $zip->close();
41
        } else {
42
            throw new FileException("No se encontró el archivo R$billName.zip");
43
        }
44
        return $info;
45
    }
46
47
    private static function getMapCdr($xmlString) {
48
        $xmlStringI1 = str_replace(['ar:', 'ext:', 'cac:', 'cbc:'], '', $xmlString);
49
        $SimpleXml = simplexml_load_string($xmlStringI1);
50
        $origin = json_decode(json_encode($SimpleXml), 1);
51
        $respNode = $origin['DocumentResponse'];
52
        return [
53
            'id' => $origin['ID'],
54
            'invoiceId'    => $respNode['DocumentReference']['ID'],
55
            'receiverId'   => $respNode['RecipientParty']['PartyIdentification']['ID'],
56
            'issueDate'    => $origin['IssueDate'],
57
            'issueTime'    => $origin['IssueTime'],
58
            'responseDate' => $origin['ResponseDate'],
59
            'responseTime' => $origin['ResponseTime'],
60
            'responseCode' => $respNode['Response']['ResponseCode'],
61
            'responseDesc' => $respNode['Response']['Description']
62
        ];
63
    }
64
65
}
66