Passed
Push — master ( fae04d...859641 )
by Artem
01:45
created

Locker::lockData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 2.004
rs 9.9666
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
    /**
25
     * @var string $otp
26
     */
27
    protected static $otp;
28
29
    /**
30
     * useFakePass.
31
     *
32
     * @access	public static
33
     * @param	string	$code
34
     * @return	void
35
     */
36 1
    public static function useFakePass(string $otp): void
37
    {
38 1
        static::$otp = $otp;
39 1
    }
40
41 2
    public function __construct(Configuration $config)
42
    {
43 2
        $this->config = $config;
44 2
        $this->manager = new CodeManager($config);
45 2
    }
46
47
    /**
48
     * lockData.
49
     *
50
     * @access	public
51
     * @param	array $data - the data that must be locked and verified
52
     * @param	Address	$address - address where we will send one-time password to
53
     * @param	AbstractMessage|string $message - message object or string code for the message factory
54
     * @return	Code
55
     */
56 2
    public function lockData(array $data, Address $address, $message): Code
57
    {
58 2
        $code = $this->manager->generate($address, $data, static::$otp);
59
60 2
        if (is_string($message)) {
61
            $message = $this->config->getMessageFactory()->make($message);
62
        }
63
64 2
        Assert::isInstanceOf($message, AbstractMessage::class);
65
66 2
        $message->setCode($code)->setAddress($address);
67
68 2
        $transport = $this->config->getTransportFactory()
69 2
            ->make($message->getTransportCode());
70
71 2
        $transport->send($message);
72
73 2
        return $code;
74
    }
75
76
    /**
77
     * Unlocks the data and gets the protected data
78
     *
79
     * @access	public
80
     * @param	string	$verificationCode	
81
     * @param	string	$pass            	
82
     * @return	array
83
     */
84 1
    public function getUnlockedData(string $verificationCode, string $pass): array
85
    {
86 1
        $code = $this->manager->verify($verificationCode, $pass);
87
88 1
        return $code->getVerificationData();
89
    }
90
}
91