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 Exception; |
15
|
|
|
use Kinedu\CfdiCertificate\IO; |
16
|
|
|
use Kinedu\CfdiCertificate\Strategies\CerStrategy; |
17
|
|
|
use Kinedu\CfdiCertificate\Strategies\KeyStrategy; |
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
|
|
|
/** @var string */ |
36
|
|
|
protected $strategy; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new certificate instance. |
40
|
|
|
* |
41
|
|
|
* @param string $file |
42
|
|
|
* @param string $password |
43
|
|
|
* @param string $strategy |
44
|
|
|
*/ |
45
|
|
|
public function __construct(string $file, string $password = null, string $strategy = null) |
46
|
|
|
{ |
47
|
|
|
$this->file = new IO($file); |
|
|
|
|
48
|
|
|
$this->password = $password; |
49
|
|
|
$this->strategy = $strategy; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return CerStrategy|KeyStrategy|null |
54
|
|
|
*/ |
55
|
|
|
protected function getStrategy() |
56
|
|
|
{ |
57
|
|
|
switch ($this->getFileExtensionName()) { |
58
|
|
|
case 'cer': |
59
|
|
|
$strategy = new CerStrategy( |
60
|
|
|
$this->file->getOriginalRoute() |
|
|
|
|
61
|
|
|
); |
62
|
|
|
break; |
63
|
|
|
|
64
|
|
|
case 'key': |
65
|
|
|
$strategy = new KeyStrategy( |
66
|
|
|
$this->file->getOriginalRoute(), |
|
|
|
|
67
|
|
|
$this->password |
68
|
|
|
); |
69
|
|
|
break; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $strategy; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function decode(): string |
76
|
|
|
{ |
77
|
|
|
$strategy = $this->getStrategy(); |
78
|
|
|
$pem = $strategy->convertToPem(); |
79
|
|
|
|
80
|
|
|
return $pem; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $directory |
85
|
|
|
* @param string $filename |
86
|
|
|
* |
87
|
|
|
* @return integer|bool |
88
|
|
|
*/ |
89
|
|
|
public function save(string $directory, string $filename = null) |
90
|
|
|
{ |
91
|
|
|
$filename = $filename ?? $this->file->getFileName(); |
|
|
|
|
92
|
|
|
$extension = $this->getFileExtensionName(); |
93
|
|
|
|
94
|
|
|
$directory = rtrim($directory, '/').'/'; |
95
|
|
|
$directory = "{$directory}{$filename}.{$extension}.pem"; |
96
|
|
|
|
97
|
|
|
return file_put_contents($directory, $this->decode()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $name |
102
|
|
|
* @param array $arguments |
103
|
|
|
*/ |
104
|
|
|
public function __call(string $name, array $arguments) |
105
|
|
|
{ |
106
|
|
|
$strategy = $this->getStrategy(); |
107
|
|
|
|
108
|
|
|
if (method_exists($strategy, $name)) { |
109
|
|
|
return $strategy->{$name}($arguments); |
110
|
|
|
} else { |
111
|
|
|
throw new Exception("This method doesn't exist"); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function getFileExtensionName() |
116
|
|
|
{ |
117
|
|
|
return $this->strategy ?? $this->file->getFileExtensionName(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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..