PdfExportBehavior   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B pdf() 0 27 3
1
<?php namespace VojtaSvoboda\UserExportPdf\Behaviors;
2
3
use Backend\Classes\ControllerBehavior;
4
use Config;
5
use Exception;
6
use October\Rain\Exception\ApplicationException;
7
use RainLab\User\Models\User;
8
use Renatio\DynamicPDF\Classes\PDFWrapper;
9
use Response;
10
use Str;
11
12
class PdfExportBehavior extends ControllerBehavior
13
{
14
    public function pdf($id)
15
    {
16
        $user = User::find($id);
17
        if ($user === null) {
18
            throw new ApplicationException('User not found.');
19
        }
20
21
        $templateCode = Config::get('vojtasvoboda.userexportpdf::config.template', 'rainlab::user');
22
        $filename = Str::slug($user->name . '-' . $user->username) . '.pdf';
23
24
        try {
25
            /** @var PDFWrapper $pdf */
26
            $pdf = app('dynamicpdf');
27
28
            $options = [
29
                'logOutputFile' => storage_path('temp/log.htm'),
30
            ];
31
32
            return $pdf
33
                ->loadTemplate($templateCode, compact('user'))
34
                ->setOptions($options)
35
                ->download($filename);
36
37
        } catch (Exception $e) {
38
            throw new ApplicationException($e->getMessage());
39
        }
40
    }
41
}
42