1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Prozorov\DataVerification; |
6
|
|
|
|
7
|
|
|
use Prozorov\DataVerification\Models\Code; |
8
|
|
|
use Prozorov\DataVerification\Types\Address; |
9
|
|
|
use Prozorov\DataVerification\Messages\AbstractMessage; |
10
|
|
|
use Webmozart\Assert\Assert; |
11
|
|
|
|
12
|
|
|
class Locker |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Configuration $config |
16
|
|
|
*/ |
17
|
|
|
protected $config; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var CodeManager $manager |
21
|
|
|
*/ |
22
|
|
|
protected $manager; |
23
|
|
|
|
24
|
1 |
|
public function __construct(Configuration $config) |
25
|
|
|
{ |
26
|
1 |
|
$this->config = $config; |
27
|
1 |
|
$this->manager = new CodeManager($config); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* lockData. |
32
|
|
|
* |
33
|
|
|
* @access public |
34
|
|
|
* @param array $data - the data that must be locked and verified |
35
|
|
|
* @param Address $address - address where we will send one-time password to |
36
|
|
|
* @param AbstractMessage|string $message - message object or string code for the message factory |
37
|
|
|
* @return Code |
38
|
|
|
*/ |
39
|
1 |
|
public function lockData(array $data, Address $address, $message): Code |
40
|
|
|
{ |
41
|
1 |
|
$code = $this->manager->generate($address, $data); |
42
|
|
|
|
43
|
1 |
|
if (is_string($message)) { |
44
|
|
|
$message = $this->config->getMessageFactory()->make($message); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
Assert::isInstanceOf($message, AbstractMessage::class); |
48
|
|
|
|
49
|
1 |
|
$message->setCode($code)->setAddress($address); |
50
|
|
|
|
51
|
1 |
|
$transport = $this->config->getTransportFactory() |
52
|
1 |
|
->make($message->getTransportCode()); |
53
|
|
|
|
54
|
1 |
|
$transport->send($message); |
55
|
|
|
|
56
|
1 |
|
return $code; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Unlocks the data and gets the protected data |
61
|
|
|
* |
62
|
|
|
* @access public |
63
|
|
|
* @param string $verificationCode |
64
|
|
|
* @param string $pass |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
1 |
|
public function getUnlockedData(string $verificationCode, string $pass): array |
68
|
|
|
{ |
69
|
1 |
|
$code = $this->manager->verify($verificationCode, $pass); |
70
|
|
|
|
71
|
1 |
|
return $code->getVerificationData(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|