HasForms::resolveEncrypter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Spinen\Formio\Concerns;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Encryption\Encrypter;
7
use Illuminate\Support\Facades\Crypt;
8
9
/**
10
 * Trait HasForms
11
 *
12
 * @package Spinen\Formio
13
 *
14
 * @property string $formio_password
15
 */
16
trait HasForms
17
{
18
    /**
19
     * Accessor for FormioPassword.
20
     *
21
     * @return string
22
     */
23 2
    public function getFormioPasswordAttribute()
24
    {
25 2
        return is_null($this->attributes['formio_password'] ?? null)
26 1
            ? null
27 1
            : $this->resolveEncrypter()
28 2
                   ->decrypt($this->attributes['formio_password']);
29
    }
30
31
    /**
32
     * Build the array to login to Formio
33
     *
34
     * @return array
35
     */
36 1
    public function getLoginData()
37
    {
38
        return [
39 1
            'email'    => $this->email,
40 1
            'password' => $this->formio_password,
41
        ];
42
    }
43
44
    /**
45
     * Build the array to register a user to Formio
46
     *
47
     * @return array
48
     */
49 1
    public function getRegistrationData()
50
    {
51
        return [
52 1
            'email'     => $this->email,
53 1
            'firstName' => $this->first_name,
54 1
            'lastName'  => $this->last_name,
55 1
            'password'  => $this->formio_password,
56
        ];
57
    }
58
59
    /**
60
     * Make sure that the formio_password is fillable & protected
61
     */
62 1
    public function initializeHasFormsTrait()
63
    {
64 1
        $this->fillable[] = 'formio_password';
0 ignored issues
show
Bug Best Practice introduced by
The property fillable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
65 1
        $this->hidden[] = 'formio_password';
0 ignored issues
show
Bug Best Practice introduced by
The property hidden does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
66
    }
67
68
    /**
69
     * Resolve the encrypter from the IoC
70
     *
71
     * We are staying away from the Crypt facade, so that we can support PHP 7.4 with Laravel 5.x
72
     *
73
     * TODO: Remove this when dropping support of Laravel 5.5
74
     *
75
     * @return Encrypter
76
     */
77 2
    protected function resolveEncrypter()
78
    {
79 2
        return Container::getInstance()
80
                        ->make(Encrypter::class);
81
    }
82
83
    /**
84
     * Mutator for FormioPassword.
85
     *
86
     * @param string $formio_password
87
     */
88 2
    public function setFormioPasswordAttribute($formio_password)
89
    {
90 2
        $this->attributes['formio_password'] = is_null($formio_password)
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
introduced by
The condition is_null($formio_password) is always false.
Loading history...
91 1
            ? null
92 1
            : $this->resolveEncrypter()
93
                   ->encrypt($formio_password);
94
    }
95
}
96