Passed
Push — develop ( 974831...7c221a )
by Nikita
06:16
created

ClientCertificateRepository::destroy()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 1
dl 0
loc 24
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Illuminate\Support\Facades\Storage;
6
use Carbon\Carbon;
7
use Gameap\Models\ClientCertificate;
8
use Gameap\Services\CertificateService;
9
use Gameap\Http\Requests\ClientCertificatesRequest;
10
use Gameap\Exceptions\GameapException;
11
12
class ClientCertificateRepository
13
{
14
    const STORAGE_CERTS_PATH = 'certs/client';
15
16
    /**
17
     * @var ClientCertificate
18
     */
19
    protected $model;
20
21
    /**
22
     * ClientCertificateRepository constructor.
23
     * @param ClientCertificate $clientCertificate
24
     */
25
    public function __construct(ClientCertificate $clientCertificate)
26
    {
27
        $this->model = $clientCertificate;
28
    }
29
30
    /**
31
     * @param int $perPage
32
     *
33
     * @return mixed
34
     */
35
    public function getAll($perPage = 20)
36
    {
37
        return ClientCertificate::orderBy('id')->paginate($perPage);
38
    }
39
40
    /**
41
     * @param ClientCertificatesRequest $request
42
     *
43
     * @return ClientCertificate
44
     * @throws GameapException
45
     */
46
    public function store(ClientCertificatesRequest $request)
47
    {
48
        $attributes = $request->all();
49
        
50
        if ($request->hasFile('certificate')) {
51
            $attributes['certificate'] = $request->file('certificate')->store(
52
                self::STORAGE_CERTS_PATH, 'local'
53
            );
54
        }
55
56
        if ($request->hasFile('private_key')) {
57
            $attributes['private_key'] = $request->file('private_key')->store(
58
                self::STORAGE_CERTS_PATH, 'local'
59
            );
60
        }
61
        
62
        if (!openssl_x509_check_private_key(Storage::get($attributes['certificate']), Storage::get($attributes['private_key']))) {
63
            throw new GameapException(__('client_certificates.private_key_not_correspond'));
64
        }
65
66
        $info = CertificateService::certificateInfo($attributes['certificate']);
67
68
        $attributes['expires'] = $info['expires'];
69
        $attributes['fingerprint'] = CertificateService::fingerprintString($attributes['certificate'], 'sha1');
0 ignored issues
show
Unused Code introduced by
The call to Gameap\Services\Certific...ce::fingerprintString() has too many arguments starting with 'sha1'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        /** @scrutinizer ignore-call */ 
70
        $attributes['fingerprint'] = CertificateService::fingerprintString($attributes['certificate'], 'sha1');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
70
        $attributes['private_key_pass'] = '';
71
        
72
        return ClientCertificate::create($attributes);
73
    }
74
75
    /**
76
     * @param ClientCertificate $clientCertificate
77
     * @param ClientCertificatesRequest $request
78
     */
79
    public function update(ClientCertificate $clientCertificate, array $request)
80
    {
81
        $attributes = $request->all();
82
83
        if ($request->hasFile('certificate')) {
84
            $attributes['gdaemon_server_cert'] = $request->file('certificate')->store(
85
                self::STORAGE_CERTS_PATH, 'local'
86
            );
87
        }
88
89
        if ($request->hasFile('gdaemon_server_cert')) {
90
            $attributes['gdaemon_server_cert'] = $request->file('private_key')->store(
91
                self::STORAGE_CERTS_PATH, 'local'
92
            );
93
        }
94
        
95
        $clientCertificate->update($attributes);
96
    }
97
98
    /**
99
     * @param ClientCertificate $clientCertificate
100
     * @throws \Exception
101
     */
102
    public function destroy(ClientCertificate $clientCertificate)
103
    {
104
        if (! Storage::disk('local')->exists('certs/client/' . $clientCertificate->certificate)) {
105
            // TODO: Not working =(
106
            // Storage::disk('local')->delete('certs/client/' . $clientCertificate->certificate);
107
108
            $file = Storage::disk('local')
109
                ->getDriver()
110
                ->getAdapter()
111
                ->applyPathPrefix($clientCertificate->certificate);
112
113
            unlink($file);
114
        }
115
116
        if (! Storage::disk('local')->exists('certs/client/' . $clientCertificate->private_key)) {
117
            $file = Storage::disk('local')
118
                ->getDriver()
119
                ->getAdapter()
120
                ->applyPathPrefix($clientCertificate->private_key);
121
122
            unlink($file);
123
        }
124
125
        $clientCertificate->delete();
126
    }
127
128
    /**
129
     * @param ClientCertificate $clientCertificate
130
     */
131
    public function certificateInfo(ClientCertificate $clientCertificate)
132
    {
133
        return CertificateService::certificateInfo($clientCertificate->certificate);
134
    }
135
136
    /**
137
     * @param int $id
138
     * @return ClientCertificate
139
     */
140
    public function getFirstOrGenerate()
141
    {
142
        $clientCertificate = ClientCertificate::select()->first();
143
144
        if (empty($clientCertificate)) {
145
            $attributes = $this->generate();
146
            $clientCertificate = ClientCertificate::create($attributes);
147
        } else {
148
            // Fix. If client certificate exists in database but not exists certificates files.
149
            // Delete invalid files. Generate new certificates.
150
            if (! Storage::exists($clientCertificate->certificate) || ! Storage::exists($clientCertificate->private_key)) {
151
                if (Storage::exists($clientCertificate->certificate)) {
152
                    Storage::delete($clientCertificate->certificate);
153
                }
154
155
                if (Storage::exists($clientCertificate->private_key)) {
156
                    Storage::delete($clientCertificate->private_key);
157
                }
158
159
                $attributes = $this->generate();
160
161
                $clientCertificate->fingerprint = $attributes['fingerprint'];
162
                $clientCertificate->certificate = $attributes['certificate'];
163
                $clientCertificate->private_key = $attributes['private_key'];
164
                $clientCertificate->private_key_pass = $attributes['private_key_pass'];
165
                $clientCertificate->save();
166
            }
167
        }
168
169
        return $clientCertificate;
170
    }
171
172
    /**
173
     * Generate certificate
174
     * Return array with paths to certificates
175
     *
176
     * @return array
177
     */
178
    private function generate()
179
    {
180
        $timestamp = time();
181
        $certificateName = self::STORAGE_CERTS_PATH . "/client_{$timestamp}.crt";
182
        $privateKeyName = self::STORAGE_CERTS_PATH . "/client_{$timestamp}.key";
183
184
        $info = CertificateService::certificateInfo($certificateName);
185
186
        return [
187
            'fingerprint' => CertificateService::fingerprintString($certificateName),
188
            'expires'     => $info['expires'],
189
            'certificate' => $certificateName,
190
            'private_key' => $privateKeyName,
191
            'private_key_pass' => '',
192
        ];
193
    }
194
}