1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the cfdi-certificate project. |
5
|
|
|
* |
6
|
|
|
* (c) Kinedu |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Kinedu\CfdiCertificate; |
13
|
|
|
|
14
|
|
|
use Kinedu\CfdiCertificate\Strategies\CerStrategy; |
15
|
|
|
use Kinedu\CfdiCertificate\Strategies\KeyStrategy; |
16
|
|
|
use Kinedu\CfdiCertificate\IO; |
17
|
|
|
use Exception; |
18
|
|
|
|
19
|
|
|
class Certificate |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* File to decode. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $file; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Password to the decode the file. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $password; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $strategy; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new certificate instance. |
42
|
|
|
* |
43
|
|
|
* @param string $file |
44
|
|
|
* @param string $password |
45
|
|
|
* @param string $strategy |
46
|
|
|
*/ |
47
|
|
|
public function __construct(string $file, string $password = null, string $strategy = null) |
48
|
|
|
{ |
49
|
|
|
$this->file = new IO($file); |
|
|
|
|
50
|
|
|
$this->password = $password; |
51
|
|
|
$this->strategy = $strategy; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return CerStrategy|KeyStrategy|null |
56
|
|
|
*/ |
57
|
|
|
protected function getStrategy() |
58
|
|
|
{ |
59
|
|
|
switch ($this->getFileExstensionName()) { |
60
|
|
|
case 'cer': |
61
|
|
|
$strategy = new CerStrategy( |
62
|
|
|
$this->file->getOrginalRoute() |
|
|
|
|
63
|
|
|
); |
64
|
|
|
break; |
65
|
|
|
|
66
|
|
|
case 'key': |
67
|
|
|
$strategy = new KeyStrategy( |
68
|
|
|
$this->file->getOrginalRoute(), |
|
|
|
|
69
|
|
|
$this->password |
70
|
|
|
); |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $strategy; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function decode() : string |
81
|
|
|
{ |
82
|
|
|
$strategy = $this->getStrategy(); |
83
|
|
|
$pem = $strategy->convertToPem(); |
84
|
|
|
|
85
|
|
|
return $pem; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $directory |
90
|
|
|
* @param string $filename |
91
|
|
|
* |
92
|
|
|
* @return integer|bool |
93
|
|
|
*/ |
94
|
|
|
public function save(string $directory, string $filename = null) |
95
|
|
|
{ |
96
|
|
|
$filename = $filename ?? $this->file->getFileName(); |
|
|
|
|
97
|
|
|
$extension = $this->getFileExstensionName(); |
98
|
|
|
|
99
|
|
|
$directory = rtrim($directory, '/').'/'; |
100
|
|
|
$directory = "{$directory}{$filename}.{$extension}.pem"; |
101
|
|
|
|
102
|
|
|
return file_put_contents($directory, $this->decode()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $name |
107
|
|
|
* @param array $arguments |
108
|
|
|
*/ |
109
|
|
|
public function __call(string $name, array $arguments) |
110
|
|
|
{ |
111
|
|
|
$strategy = $this->getStrategy(); |
112
|
|
|
|
113
|
|
|
if (method_exists($strategy, $name)) { |
114
|
|
|
return $strategy->{$name}($arguments); |
115
|
|
|
} else { |
116
|
|
|
throw new Exception("This method doesn't exist"); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
protected function getFileExstensionName() |
124
|
|
|
{ |
125
|
|
|
return $this->strategy ?? $this->file->getFileExstensionName(); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..