Completed
Pull Request — master (#46)
by Antonio Oertel
04:53
created

Configure::checkCerts()   D

Complexity

Conditions 9
Paths 180

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 11.6096

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
ccs 15
cts 22
cp 0.6818
rs 4.6666
cc 9
eloc 23
nc 180
nop 4
crap 11.6096
1
<?php
2
3
namespace NFePHP\Common\Configure;
4
5
use NFePHP\Common\Modules\Modules;
6
use NFePHP\Common\Files\FilesFolders;
7
use NFePHP\Common\Certificate\Pkcs12;
8
use NFePHP\Common\Exception\InvalidArgumentException;
9
use NFePHP\Common\Exception\RuntimeException;
10
11
class Configure
12
{
13
    /**
14
     * $cRed
15
     * @var hex
16
     */
17
    protected static $cRed = '#FF0000';
18
    
19
    /**
20
     *$cGreen
21
     * @var hex
22
     */
23
    protected static $cGreen = '#00CC00';
24
    
25
    /**
26
     * $aRequirements
27
     * @var array
28
     */
29
    public static $aRequirements = array(
30
        'PHP' => array('php','','','5.4.0','Versão do PHP'),
31
        'cURL'=> array('curl','cURL Information','','7.22.2','mínimo cURL 7.22.2'),
32
        'OpenSSL' => array('openssl','OpenSSL Library Version','','','mínimo OpenSSL 1.0'),
33
        'DOM' => array('dom','libxml Version','','2.0.6','mínimo DOM 2.0.6'),
34
        'GD' => array('gd','GD Support','','','-----'),
35
        'SOAP' => array('soap','Soap Client','','','-----'),
36
        'ZIP' => array('zip','Zip version', '', '', '-----')
37
    );
38
    
39
    /**
40
     * chkModules
41
     * @return string
42
     */
43
    public static function chkModules()
44
    {
45
        //instanciar a classe de modulos
46
        $modules = new Modules();
47
        //versão do php
48
        $phpversion = str_replace('-', '', substr(PHP_VERSION, 0, 6));
49
        $phpver = $modules->convVer($phpversion);
50
        $phpvermin = $modules->convVer(self::$aRequirements['PHP'][3]);
51
        $status = 'NOK';
52
        $bcor = "bgcolor=\"".self::$cRed."\"";
53
        $comment = "v. $phpversion Inadequada !!!";
54
        if ($phpver >= $phpvermin) {
55
            $comment = "mínimo PHP ". self::$aRequirements['PHP'][3];
56
            $status = 'OK';
57
            $bcor = "bgcolor=\"".self::$cGreen."\"";
58
        }
59
        $htmmod = "<table><tr bgcolor=\"#FFFF99\">"
60
            . "<td>Versão do PHP $phpversion</td>"
61
            . "<td $bcor><div align=\"center\">$status</div></td>"
62
            . "<td>$comment</td></tr>";
63
        
64
        foreach (self::$aRequirements as $key => $param) {
65
            if ($key != 'PHP') {
66
                $htmmod .= $modules->testModule(
67
                    $param[0],
68
                    $key,
69
                    $param[1],
70
                    $param[2],
71
                    $param[3],
72
                    $param[4]
73
                );
74
            }
75
        }
76
        return $htmmod.'</table>';
77
    }
78
    
79
    /**
80
     * checkCerts
81
     * @param string $cnpj
82
     * @param string $pathCertsFiles
83
     * @param string $certPfxName
84
     * @param string $certPassword
85
     * @return array
86
     */
87 1
    public static function checkCerts($cnpj = '', $pathCertsFiles = '', $certPfxName = '', $certPassword = '')
88
    {
89 1
        $flag = true;
90 1
        $msg = '';
91 1
        if (strlen($cnpj) != 14) {
92
            $flag = $flag && false;
93
            $msg .= "CNPJ incorreto! $cnpj \n";
94
        }
95 1
        if (! is_dir($pathCertsFiles)) {
96 1
            $flag = $flag && false;
97 1
            $msg .= "Diretório não localizado! $pathCertsFiles \n";
98
        }
99 1
        if (substr($pathCertsFiles, -1) !== DIRECTORY_SEPARATOR) {
100 1
            $pathCertsFiles .= DIRECTORY_SEPARATOR;
101
        }
102
        try {
103 1
            $cert = new Pkcs12($pathCertsFiles, $cnpj);
104
            $flag = $cert->loadPfxFile($pathCertsFiles.$certPfxName, $certPassword);
105 1
        } catch (InvalidArgumentException $exc) {
106 1
            $flag = false;
107 1
            $msg = $exc->getMessage();
108
        } catch (RuntimeException $exc) {
109
            $flag = false;
110
            $msg = $exc->getMessage();
111
        }
112 1
        if ($msg == '') {
113
            $msg = 'Certificado Validado, arquivos PEM criados na pasta.';
114
        }
115 1
        return array('cert' => array('status' => $flag, 'msg' => $msg));
116
    }
117
    
118
    /**
119
     * checkFolders
120
     * @param string $pathnfe
121
     * @param string $pathcte
122
     * @param string $pathmdfe
123
     * @param string $pathcle
124
     * @param string $pathnfse
125
     * @param string $pathcerts
126
     * @return array
127
     */
128
    public static function checkFolders(
129
        $pathnfe = '',
130
        $pathcte = '',
131
        $pathmdfe = '',
132
        $pathcle = '',
133
        $pathnfse = '',
134
        $pathcerts = ''
135
    ) {
136
        $aResp = array(
137
            'NFe' => array('status'=>true,'msg'=>''),
138
            'CTe' => array('status'=>true,'msg'=>''),
139
            'MDFe' => array('status'=>true,'msg'=>''),
140
            'CLe' => array('status'=>true,'msg'=>''),
141
            'NFSe' => array('status'=>true,'msg'=>''),
142
            'Certs' => array('status'=>true,'msg'=>'')
143
        );
144
        //testa e constroi a estrutura da pasta NFe
145
        $aResp['NFe'] = self::zFolderMTest($pathnfe);
146
        //testa e constroi a estrutura da pasta CTe
147
        $aResp['CTe'] = self::zFolderMTest($pathcte);
148
        //testa e constroi a estrutura da pasta MDFe
149
        $aResp['MDFe'] = self::zFolderMTest($pathmdfe);
150
        //testa e constroi a estrutura da pasta cle
151
        $aResp['CLe'] = self::zFolderMTest($pathcle);
152
        //testa e constroi a estrutura da pasta NFSe
153
        $aResp['NFSe'] = self::zFolderMTest($pathnfse);
154
        //testa diretorio certs
155
        if ($pathcerts != '') {
156
            if (! is_writable($pathcerts)) {
157
                $aResp['Certs'] = array('status'=>false, 'msg'=>'Diretório sem permissões de escrita');
158
            }
159
        }
160
        return $aResp;
161
    }
162
    
163
    /**
164
     * zFolderMTest
165
     * @param string $path
166
     * @return array
167
     */
168
    protected static function zFolderMTest($path = '')
169
    {
170
        $aResp = array('status' => true, 'msg' => '');
171
        if ($path != '') {
172
            try {
173
                FilesFolders::createFolders($path);
174
            } catch (RuntimeException $e) {
175
                $aResp = array('status' => false, 'msg' => $e->getMessage());
176
            }
177
        }
178
        return $aResp;
179
    }
180
}
181