Passed
Branch develop (00c356)
by JAIME ELMER
02:06
created

Repository::getCdrInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 12
rs 9.9332
c 0
b 0
f 0
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;
12
13
use ZipArchive;
14
use F72X\Company;
15
use F72X\Exception\FileException;
16
17
class Repository {
18
19
    public static function saveBill($billName, $billContent) {
20
        self::saveFile("bill/$billName.xml", $billContent);
21
    }
22
23
    public static function saveSignedBill($billName, $billContent) {
24
        self::saveFile("signedbill/S-$billName.xml", $billContent);
25
    }
26
27
    public static function saveCdr($billName, $billContent) {
28
        self::saveFile("cdr/R$billName.zip", $billContent);
29
    }
30
    public static function zipBill($billName) {
31
        $rp = self::getRepositoryPath();
32
        $zip = new ZipArchive();
33
        if ($zip->open("$rp/zippedbill/$billName.zip", ZipArchive::CREATE) === TRUE) {
34
            $zip->addFile("$rp/signedbill/S-$billName.xml", "$billName.xml");
35
            $zip->close();
36
        }
37
    }
38
    public static function billExist($billName) {
39
        $rp = self::getRepositoryPath();
40
        return fileExists("$rp/bill/$billName.xml");
41
    }
42
43
    public static function cdrExist($billName) {
44
        $rp = self::getRepositoryPath();
45
        return fileExists("$rp/cdr/R$billName.zip");
46
    }
47
48
    public static function getRepositoryPath() {
49
        return Company::getRepositoryPath();
50
    }
51
52
    public static function getBillPath($billName) {
53
        $rp = self::getRepositoryPath();
54
        return "$rp/bill/$billName.xml";
55
    }
56
57
    private static function saveFile($filePath, $fileContent) {
58
        $rp = self::getRepositoryPath();
59
        file_put_contents("$rp/$filePath", $fileContent);
60
    }
61
    public static function getCdrInfo($billName) {
62
        $rp = self::getRepositoryPath();
63
        $zip = new ZipArchive();
64
        $info = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $info is dead and can be removed.
Loading history...
65
        if ($zip->open("$rp/cdr/R$billName.zip") === true) {
66
            $xmlString = $zip->getFromName("R-$billName.xml");
67
            $info = self::getMapCdr($xmlString);
68
            $zip->close();
69
        } else {
70
            throw new FileException("No se encontró el archivo R$billName.zip");
71
        }
72
        return $info;
73
    }
74
75
    private static function getMapCdr($xmlString) {
76
        $xmlStringI1 = str_replace(['ar:', 'ext:', 'cac:', 'cbc:'], '', $xmlString);
77
        $SimpleXml = simplexml_load_string($xmlStringI1);
78
        $origin = json_decode(json_encode($SimpleXml), 1);
79
        $respNode = $origin['DocumentResponse'];
80
        return [
81
            'id' => $origin['ID'],
82
            'invoiceId'    => $respNode['DocumentReference']['ID'],
83
            'receiverId'   => $respNode['RecipientParty']['PartyIdentification']['ID'],
84
            'issueDate'    => $origin['IssueDate'],
85
            'issueTime'    => $origin['IssueTime'],
86
            'responseDate' => $origin['ResponseDate'],
87
            'responseTime' => $origin['ResponseTime'],
88
            'responseCode' => $respNode['Response']['ResponseCode'],
89
            'responseDesc' => $respNode['Response']['Description']
90
        ];
91
    }
92
}
93