Issues (41)

src/F72X.php (1 issue)

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 F72X\Exception\ConfigException;
14
15
class F72X {
16
17
    const RUNNING_ENV_GAE = 1;
18
    const RUNNING_ENV_GAE_DEV_SERVER = 2;
19
    const RUNNING_ENV_X = 3;
20
21
    private static $production = null;
22
    private static $requiredConfigFields = [
23
        'cconfigPath',
24
        'repoPath'
25
    ];
26
27
    /**
28
     * Inicializa el módulo con la configuración del contribuyente.
29
     * @param array $cfg datos del contribuyente
30
     * @throws ConfigException
31
     */
32
    public static function init(array $cfg) {
33
        self::validateConfig($cfg);
34
        // Wrong production mode config
35
        if (array_key_exists('prodMode', $cfg) && !is_bool($cfg['prodMode'])) {
36
            $vartype = gettype($cfg['prodMode']);
37
            throw new ConfigException("F72x::init error: La propiedad [prodMode] debe ser tipo boolean, no $vartype. O puede no indicar esta propiedad si no piensa hacer uso de los webservices de SUNAT");
38
        }
39
        if (isset($cfg['prodMode'])) {
40
            self::$production = $cfg['prodMode'];
41
        }
42
        Company::setConfig($cfg);
43
    }
44
45
    public static function isProductionMode() {
46
        if (is_null(self::$production)) {
47
            throw new ConfigException("F72x::init error: La propiedad [prodMode] no fue definida");
48
        }
49
        return self::$production;
50
    }
51
52
    /**
53
     * 
54
     * @param array $config
55
     * @throws ConfigException
56
     */
57
    private static function validateConfig(array $config) {
58
        foreach (self::$requiredConfigFields as $field) {
59
            if (!isset($config[$field])) {
60
                throw new ConfigException(sprintf('La propiedad %s es obligatoria, por favor revise su cofiguración.', $field));
61
            }
62
        }
63
        if (!file_exists($config['cconfigPath'])) {
64
            throw new ConfigException(sprintf('No se encuentra el directorio de configuración del contribuyente, verifique la ubicación %s sea la correcta.', $config['cconfigPath']));
65
        }
66
        if (!file_exists($config['repoPath'])) {
67
            throw new ConfigException(sprintf('No se encuentra el directorio que será usado para guardar los documentos electronicos, verifique la ubicación %s sea la correcta.', $config['repoPath']));
68
        }
69
    }
70
71
    public static function getModuleDir() {
72
        return __DIR__ . '/..';
73
    }
74
75
    public static function getCatalogoSunatDir() {
76
        return __DIR__ . '/Sunat/catalogo';
77
    }
78
79
    public static function getTempDir() {
80
        return Company::getTempPath();
81
    }
82
83
    public static function getDefaultPdfTemplatesPath() {
84
        return self::getModuleDir() . '/cdefaults/tpls';
85
    }
86
87
    public static function getDefaultListsPath() {
88
        return self::getModuleDir() . '/cdefaults/lists';
89
    }
90
91
    public static function getSrcDir() {
92
        return __DIR__;
93
    }
94
95
    /**
96
     * Return the environment code where this module is running.
97
     * @return int
98
     */
99
    public static function getRunningEnvironment() {
100
        $serverSofware = getenv('SERVER_SOFTWARE');
101
        if (strpos($serverSofware, 'Google App Engine') === 0) {
102
            return self::RUNNING_ENV_GAE;
103
        }
104
        if (strpos($serverSofware, 'Development') === 0) {
105
            return self::RUNNING_ENV_GAE_DEV_SERVER;
106
        }
107
        return self::RUNNING_ENV_X;
108
    }
109
110
    /**
111
     * Return the environment code where this module is running.
112
     * @return int
113
     */
114
    public static function isRunninOnGAE() {
115
        $re = self::getRunningEnvironment();
116
        return ($re == self::RUNNING_ENV_GAE || $re == self::RUNNING_ENV_GAE_DEV_SERVER);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $re == self::RUNN...NING_ENV_GAE_DEV_SERVER returns the type boolean which is incompatible with the documented return type integer.
Loading history...
117
    }
118
119
}
120