Passed
Push — master ( b72680...5fbcde )
by Artem
01:02 queued 10s
created

Locker::getUnlockedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
        try {
42 1
            $this->config->getCodeRepo()->openTransaction();
43
44 1
            $code = $this->manager->generate($address, $data);
45
46 1
            if (is_string($message)) {
47
                $message = $this->config->getMessageFactory()->make($message);
48
            }
49
50 1
            Assert::isInstanceOf($message, AbstractMessage::class);
51
52 1
            $message->setCode($code)->setAddress($address);
53
54 1
            $transport = $this->config->getTransportFactory()
55 1
                ->make($message->getTransportCode());
56
57 1
            $transport->send($message);
58
59 1
            $this->config->getCodeRepo()->commitTransaction();
60
61 1
            return $code;
62
        } catch (\Exception $exception) {
63
            $this->config->getCodeRepo()->rollbackTransaction();
64
65
            throw $exception;
66
        }
67
    }
68
69
    /**
70
     * Generate Code and send
71
     *
72
     * @access	public
73
     * @param	Address	$address	
74
     * @param	AbstractMessage|string  	$message	
75
     * @return	Code
76
     */
77
    public function generateAndSend(Address $address, $message): Code
78
    {
79
        return $this->lockData([], $address, $message);
80
    }
81
82
    /**
83
     * Verifies 
84
     *
85
     * @access	public
86
     * @param	string	$verificationCode	
87
     * @param	string	$pass            	
88
     * @return	array
89
     */
90
    public function verifyOrFail(string $verificationCode, string $pass): void
91
    {
92
        $this->manager->verify($verificationCode, $pass);
93
    }
94
95
    /**
96
     * Unlocks the data and gets the protected data
97
     *
98
     * @access	public
99
     * @param	string	$verificationCode	
100
     * @param	string	$pass            	
101
     * @return	array
102
     */
103 1
    public function getUnlockedData(string $verificationCode, string $pass): array
104
    {
105 1
        $code = $this->manager->verify($verificationCode, $pass);
106
107 1
        return $code->getVerificationData();
108
    }
109
}
110