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
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $file; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $password; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new certificate instance. |
33
|
|
|
* |
34
|
|
|
* @param string $file |
35
|
|
|
* @param string $password |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $file, string $password = null) |
38
|
|
|
{ |
39
|
|
|
$this->file = new IO($file); |
|
|
|
|
40
|
|
|
$this->password = $password; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return CerStrategy|KeyStrategy|null |
45
|
|
|
*/ |
46
|
|
|
protected function getStrategy() |
47
|
|
|
{ |
48
|
|
|
switch ($this->file->getFileExstensionName()) { |
|
|
|
|
49
|
|
|
case 'cer': |
50
|
|
|
$strategy = new CerStrategy( |
51
|
|
|
$this->file->getOrginalRoute() |
|
|
|
|
52
|
|
|
); |
53
|
|
|
break; |
54
|
|
|
|
55
|
|
|
case 'key': |
56
|
|
|
$strategy = new KeyStrategy( |
57
|
|
|
$this->file->getOrginalRoute(), |
|
|
|
|
58
|
|
|
$this->password |
59
|
|
|
); |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $strategy; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function decode() : string |
70
|
|
|
{ |
71
|
|
|
$strategy = $this->getStrategy(); |
72
|
|
|
$pem = $strategy->convertToPem(); |
73
|
|
|
|
74
|
|
|
return $pem; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $filename |
79
|
|
|
* |
80
|
|
|
* @return |
81
|
|
|
*/ |
82
|
|
|
public function save(string $filename) |
83
|
|
|
{ |
84
|
|
|
return file_put_contents($filename, $this->decode()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $name |
89
|
|
|
* @param array $arguments |
90
|
|
|
*/ |
91
|
|
|
public function __call(string $name, array $arguments) |
92
|
|
|
{ |
93
|
|
|
$strategy = $this->getStrategy(); |
94
|
|
|
|
95
|
|
|
if(method_exists($strategy, $name)) { |
96
|
|
|
return $strategy->{$name}($arguments); |
97
|
|
|
} else { |
98
|
|
|
throw new Exception("This method doesn't exist"); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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..