Passed
Pull Request — master (#4)
by Artem
07:17 queued 13s
created

Locker::generateAndSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 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
     * Generate Code and send
61
     *
62
     * @access	public
63
     * @param	Address	$address	
64
     * @param	AbstractMessage|string  	$message	
65
     * @return	Code
66
     */
67
    public function generateAndSend(Address $address, $message): Code
68
    {
69
        return $this->lockData([], $address, $message);
70
    }
71
72
    /**
73
     * Verifies 
74
     *
75
     * @access	public
76
     * @param	string	$verificationCode	
77
     * @param	string	$pass            	
78
     * @return	array
79
     */
80
    public function verifyOrFail(string $verificationCode, string $pass): void
81
    {
82
        $this->manager->verify($verificationCode, $pass);
83
    }
84
85
    /**
86
     * Unlocks the data and gets the protected data
87
     *
88
     * @access	public
89
     * @param	string	$verificationCode	
90
     * @param	string	$pass            	
91
     * @return	array
92
     */
93 1
    public function getUnlockedData(string $verificationCode, string $pass): array
94
    {
95 1
        $code = $this->manager->verify($verificationCode, $pass);
96
97 1
        return $code->getVerificationData();
98
    }
99
}
100