BasePdf::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace TarfinLabs\EasyPdf;
4
5
use finfo;
6
use setasign\Fpdi\Tcpdf\Fpdi;
7
use TarfinLabs\EasyPdf\Exceptions\UnableToOpen;
8
9
abstract class BasePdf
10
{
11
    /**
12
     * @var Fpdi
13
     */
14
    protected $pdf;
15
16
    /**
17
     * @var string
18
     */
19
    protected $content;
20
21
    /**
22
     * BasePdf constructor.
23
     */
24
    public function __construct()
25
    {
26
        $this->pdf = new Fpdi();
27
        $this->pdf->setPrintHeader(false);
28
        $this->pdf->setPrintFooter(false);
29
    }
30
31
    /**
32
     * Check for url given path.
33
     *
34
     * @param  $url
35
     * @return bool
36
     */
37
    protected function validUrl($url)
38
    {
39
        if (filter_var($url, FILTER_VALIDATE_URL)) {
40
            return true;
41
        }
42
43
        return false;
44
    }
45
46
    /**
47
     * Check file type from string content is pdf.
48
     *
49
     * @param  $content
50
     * @return string
51
     */
52
    protected function mimeType($content)
53
    {
54
        $finfo = new finfo(1040);
55
56
        $buffer = $finfo->buffer($content);
57
58
        if ($buffer === 'application/pdf; charset=binary') {
59
            return  true;
60
        }
61
62
        return false;
63
    }
64
65
    /**
66
     * Set file content.
67
     *
68
     * @param  $pdf
69
     *
70
     * @throws UnableToOpen
71
     */
72
    protected function setFileContent($pdf)
73
    {
74
        if ($this->validUrl($pdf)) {
75
            $this->content = file_get_contents($pdf);
76
        } elseif ($this->mimeType($pdf)) {
77
            $this->content = $pdf;
78
        } elseif (file_exists($pdf)) {
79
            $this->content = file_get_contents($pdf);
80
        } else {
81
            throw UnableToOpen::noSuchFile($pdf);
82
        }
83
    }
84
85
    /**
86
     * Render pdf content.
87
     *
88
     * @return mixed
89
     */
90
    abstract public function render();
91
92
    /**
93
     * Save to a local server file with the name given by name.
94
     *
95
     * @param  string  $filename
96
     * @return string
97
     */
98
    public function save(string $filename)
99
    {
100
        $this->render();
101
102
        return $this->pdf->Output($filename, 'F');
103
    }
104
105
    /**
106
     * Send the Pdf inline to the browser.
107
     *
108
     * @return string
109
     */
110
    public function stream()
111
    {
112
        $this->render();
113
114
        return $this->pdf->Output('doc.pdf', 'I');
115
    }
116
117
    /**
118
     * Return the pdf as a string.
119
     *
120
     * @return string
121
     */
122
    public function content()
123
    {
124
        $this->render();
125
126
        return $this->pdf->Output('doc.pdf', 'S');
127
    }
128
}
129