GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 698fd2...95f32c )
by Christian
10:54 queued 08:21
created

HasEnigma::encryptWithIndexes()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 4
rs 9.8666
1
<?php
2
3
namespace Omatech\Enigma\Database\Eloquent;
4
5
use Illuminate\Database\Query\Builder as QueryBuilder;
6
use Omatech\Enigma\Database\Eloquent\Builder as EloquentBuilder;
7
use Omatech\Enigma\Enigma;
8
use Omatech\Enigma\Exceptions\RepeatedAttributesException;
9
use Omatech\Enigma\Jobs\IndexHydrate;
10
use ParagonIE\CipherSweet\Exception\BlindIndexNotFoundException;
11
use ParagonIE\CipherSweet\Exception\CryptoOperationException;
12
use SodiumException;
13
use Throwable;
14
15
trait HasEnigma
16
{
17
    private $engine;
18
19
    /**
20
     * Get the table associated with the model.
21
     *
22
     * @return string
23
     */
24
    abstract public function getTable();
25
26
    /**
27
     * Create a new Eloquent model instance.
28
     *
29
     * @param array $attributes
30
     * @return void
31
     * @throws Throwable
32
     */
33 56
    public function __construct(array $attributes = [])
34
    {
35 56
        parent::__construct($attributes);
36
37 56
        $this->engine = new Enigma;
38
39 56
        $repeated = count(array_intersect($this->getLaravelEncryptable(), $this->getEnigmaEncryptable()));
40
41 56
        throw_if($repeated, new RepeatedAttributesException('One or more attributes are repeated on both encryptation types.'));
42 52
    }
43
44
    /**
45
     * @return array
46
     */
47 56
    public function getLaravelEncryptable(): array
48
    {
49 56
        return $this->laravelEncryptable ?? [];
50
    }
51
52
    /**
53
     * @return array
54
     */
55 56
    public function getEnigmaEncryptable(): array
56
    {
57 56
        return $this->enigmaEncryptable ?? [];
58
    }
59
60
    /**
61
     * Boot trait on the model.
62
     *
63
     * @return void
64
     */
65 56
    public static function bootHasEnigma(): void
66
    {
67
        static::saving(static function ($model) {
68 52
            $model->encrypt(
69 52
                $model->getDirty()
70
            );
71 56
        });
72
73
        static::retrieved(static function ($model) {
74 52
            $model->decrypt(
75 52
                $model->attributes
76
            );
77 56
        });
78
79
        static::saved(static function ($model) {
80 52
            dispatch(new IndexHydrate(get_class($model), $model->id))
81 52
                ->onQueue('enigma');
82
83 52
            $model->decrypt(
84 52
                $model->attributes
85
            );
86 56
        });
87 56
    }
88
89
    /**
90
     * @param array $attributes
91
     * @return $this
92
     * @throws BlindIndexNotFoundException
93
     * @throws CryptoOperationException
94
     * @throws SodiumException
95
     * @throws \ParagonIE\CipherSweet\Exception\BlindIndexNameCollisionException
96
     */
97 52
    public function encrypt(array $attributes = ['*']): self
98
    {
99 52
        foreach ($this->getEnigmaEncryptable() as $column) {
100 48
            if (isset($attributes[$column])) {
101 48
                $this->{$column} = $this->engine->encrypt(
102 48
                    $this->getTable(),
103
                    $column,
104 48
                    $this->{$column}
105
                );
106
            }
107
        }
108
109 52
        foreach ($this->getLaravelEncryptable() as $column) {
110 4
            if (isset($attributes[$column])) {
111 4
                $this->{$column} = encrypt($this->{$column});
112
            }
113
        }
114
115 52
        return $this;
116
    }
117
118
    /**
119
     * @param string[] $attributes
120
     * @return $this
121
     * @throws CryptoOperationException
122
     */
123 52
    public function decrypt($attributes = ['*']): self
124
    {
125 52
        foreach ($this->getEnigmaEncryptable() as $column) {
126 48
            if (isset($attributes[$column])) {
127 48
                $this->{$column} = $this->engine->decrypt(
128 48
                    $this->getTable(),
129
                    $column,
130 48
                    $this->{$column}
131
                );
132
            }
133
        }
134
135 52
        foreach ($this->getLaravelEncryptable() as $column) {
136 4
            if (isset($attributes[$column])) {
137 4
                $this->{$column} = decrypt($this->{$column});
138
            }
139
        }
140
141 52
        return $this;
142
    }
143
144
    /**
145
     * Create a new Eloquent query builder for the model.
146
     *
147
     * @param QueryBuilder $query
148
     * @return EloquentBuilder
149
     */
150 52
    public function newEloquentBuilder($query)
151
    {
152 52
        return new EloquentBuilder($query);
153
    }
154
}
155