Completed
Push — develop ( d383bc...d9bd45 )
by Adrien
23:59
created

Sample::logRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Helper;
4
5
use PhpOffice\PhpSpreadsheet\IOFactory;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
8
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
9
use RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
use RecursiveRegexIterator;
12
use ReflectionClass;
13
use RegexIterator;
14
15
/**
16
 * Helper class to be used in sample code.
17
 */
18
class Sample
19
{
20
    /**
21
     * Returns whether we run on CLI or browser.
22
     *
23
     * @return bool
24
     */
25 107
    public function isCli()
26
    {
27 107
        return PHP_SAPI === 'cli';
28
    }
29
30
    /**
31
     * Return the filename currently being executed.
32
     *
33
     * @return string
34
     */
35
    public function getScriptFilename()
36
    {
37
        return basename($_SERVER['SCRIPT_FILENAME'], '.php');
38
    }
39
40
    /**
41
     * Whether we are executing the index page.
42
     *
43
     * @return bool
44
     */
45
    public function isIndex()
46
    {
47
        return $this->getScriptFilename() === 'index';
48
    }
49
50
    /**
51
     * Return the page title.
52
     *
53
     * @return string
54
     */
55
    public function getPageTitle()
56
    {
57
        return $this->isIndex() ? 'PHPSpreadsheet' : $this->getScriptFilename();
58
    }
59
60
    /**
61
     * Return the page heading.
62
     *
63
     * @return string
64
     */
65
    public function getPageHeading()
66
    {
67
        return $this->isIndex() ? '' : '<h1>' . str_replace('_', ' ', $this->getScriptFilename()) . '</h1>';
68
    }
69
70
    /**
71
     * Returns an array of all known samples.
72
     *
73
     * @return string[] [$name => $path]
74
     */
75
    public function getSamples()
76
    {
77
        // Populate samples
78
        $baseDir = realpath(__DIR__ . '/../../../samples');
79
        $directory = new RecursiveDirectoryIterator($baseDir);
0 ignored issues
show
Bug introduced by
The call to RecursiveDirectoryIterator::__construct() has too few arguments starting with flags. ( Ignorable by Annotation )

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

79
        $directory = /** @scrutinizer ignore-call */ new RecursiveDirectoryIterator($baseDir);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
80
        $iterator = new RecursiveIteratorIterator($directory);
81
        $regex = new RegexIterator($iterator, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH);
82
83
        $files = [];
84
        foreach ($regex as $file) {
85
            $file = str_replace($baseDir . '/', '', $file[0]);
86
            $info = pathinfo($file);
87
            $category = str_replace('_', ' ', $info['dirname']);
88
            $name = str_replace('_', ' ', preg_replace('/(|\.php)/', '', $info['filename']));
89
            if (!in_array($category, ['.', 'boostrap', 'templates'])) {
90
                if (!isset($files[$category])) {
91
                    $files[$category] = [];
92
                }
93
                $files[$category][$name] = $file;
94
            }
95
        }
96
97
        // Sort everything
98
        ksort($files);
99
        foreach ($files as &$f) {
100
            asort($f);
101
        }
102
103
        return $files;
104
    }
105
106
    /**
107
     * Write documents.
108
     *
109
     * @param Spreadsheet $spreadsheet
110
     * @param string $filename
111
     * @param string[] $writers
112
     */
113 42
    public function write(Spreadsheet $spreadsheet, $filename, array $writers = ['Xlsx', 'Xls'])
114
    {
115
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
116 42
        $spreadsheet->setActiveSheetIndex(0);
117
118
        // Write documents
119 42
        foreach ($writers as $writerType) {
120 42
            $path = $this->getFilename($filename, mb_strtolower($writerType));
121 42
            $writer = IOFactory::createWriter($spreadsheet, $writerType);
122 42
            if ($writer instanceof Pdf) {
123
                // PDF writer needs temporary directory
124 4
                $tempDir = $this->getTemporaryFolder();
125 4
                $writer->setTempDir($tempDir);
126
            }
127 42
            $callStartTime = microtime(true);
128 42
            $writer->save($path);
129 42
            $this->logWrite($writer, $path, $callStartTime);
130
        }
131
132 42
        $this->logEndingNotes();
133 42
    }
134
135
    /**
136
     * Returns the temporary directory and make sure it exists.
137
     *
138
     * @return string
139
     */
140 63
    private function getTemporaryFolder()
141
    {
142 63
        $tempFolder = sys_get_temp_dir() . '/phpspreadsheet';
143 63
        if (!is_dir($tempFolder)) {
144 1
            if (!mkdir($tempFolder) && !is_dir($tempFolder)) {
145
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $tempFolder));
146
            }
147
        }
148
149 63
        return $tempFolder;
150
    }
151
152
    /**
153
     * Returns the filename that should be used for sample output.
154
     *
155
     * @param string $filename
156
     * @param string $extension
157
     *
158
     * @return string
159
     */
160 61
    public function getFilename($filename, $extension = 'xlsx')
161
    {
162 61
        $originalExtension = pathinfo($filename, PATHINFO_EXTENSION);
163
164 61
        return $this->getTemporaryFolder() . '/' . str_replace('.' . $originalExtension, '.' . $extension, basename($filename));
165
    }
166
167
    /**
168
     * Return a random temporary file name.
169
     *
170
     * @param string $extension
171
     *
172
     * @return string
173
     */
174 7
    public function getTemporaryFilename($extension = 'xlsx')
175
    {
176 7
        $temporaryFilename = tempnam($this->getTemporaryFolder(), 'phpspreadsheet-');
177 7
        unlink($temporaryFilename);
178
179 7
        return $temporaryFilename . '.' . $extension;
180
    }
181
182 106
    public function log($message)
183
    {
184 106
        $eol = $this->isCli() ? PHP_EOL : '<br />';
185 106
        echo date('H:i:s ') . $message . $eol;
186 106
    }
187
188
    /**
189
     * Log ending notes.
190
     */
191 45
    public function logEndingNotes()
192
    {
193
        // Do not show execution time for index
194 45
        $this->log('Peak memory usage: ' . (memory_get_peak_usage(true) / 1024 / 1024) . 'MB');
195 45
    }
196
197
    /**
198
     * Log a line about the write operation.
199
     *
200
     * @param IWriter $writer
201
     * @param string $path
202
     * @param float $callStartTime
203
     *
204
     * @throws \ReflectionException
205
     */
206 61
    public function logWrite(IWriter $writer, $path, $callStartTime)
207
    {
208 61
        $callEndTime = microtime(true);
209 61
        $callTime = $callEndTime - $callStartTime;
210 61
        $reflection = new ReflectionClass($writer);
211 61
        $format = $reflection->getShortName();
212 61
        $message = "Write {$format} format to <code>{$path}</code>  in " . sprintf('%.4f', $callTime) . ' seconds';
213
214 61
        $this->log($message);
215 61
    }
216
217
    /**
218
     * Log a line about the read operation.
219
     *
220
     * @param string $format
221
     * @param string $path
222
     * @param float $callStartTime
223
     */
224 14
    public function logRead($format, $path, $callStartTime)
225
    {
226 14
        $callEndTime = microtime(true);
227 14
        $callTime = $callEndTime - $callStartTime;
228 14
        $message = "Read {$format} format from <code>{$path}</code>  in " . sprintf('%.4f', $callTime) . ' seconds';
229
230 14
        $this->log($message);
231 14
    }
232
}
233