Locker   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 96
ccs 18
cts 27
cp 0.6667
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A lockData() 0 27 3
A verifyOrFail() 0 3 1
A generateAndSend() 0 3 1
A getUnlockedData() 0 5 1
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