Passed
Push — master ( 1f758d...de7676 )
by Ioannes
01:50
created

XlsCommand::getXlsFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace App\BxConsole\Format;
3
4
use Psr\Log\LoggerInterface;
5
6
trait XlsCommand {
7
8
    public function getXlsFile() {
9
10
        return $this->getDocumentRoot() . static::FINAL_FOLDER . static::XLS_FILE;
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XlsCommand::FINAL_FOLDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
It seems like getDocumentRoot() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

10
        return $this->/** @scrutinizer ignore-call */ getDocumentRoot() . static::FINAL_FOLDER . static::XLS_FILE;
Loading history...
Bug introduced by
The constant App\BxConsole\Format\XlsCommand::XLS_FILE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
11
    }
12
13
    protected function getTmpXlsFile() {
14
15
        return $this->getDocumentRoot() . static::TMP_FOLDER . static::XLS_FILE;
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XlsCommand::TMP_FOLDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant App\BxConsole\Format\XlsCommand::XLS_FILE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
    }
17
18
    public function writeXls($html) {
19
20
        $htmlFile = str_replace('.xls', '.html', static::XLS_FILE);
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XlsCommand::XLS_FILE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
        $htmlRes = file_put_contents($this->getDocumentRoot() . static::TMP_FOLDER . $htmlFile, $html);
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XlsCommand::TMP_FOLDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
23
        if($htmlRes) {
24
25
            //TODO: check size !!!
26
            chdir($this->getDocumentRoot() . static::TMP_FOLDER);
27
            $command = sprintf("libreoffice --calc --convert-to xls %s", $htmlFile);
28
            exec($command);
29
            if(file_exists($this->getTmpXlsFile())) {
30
                unlink($this->getDocumentRoot() . static::TMP_FOLDER . $htmlFile);
31
                $command = sprintf("mv %s %s", $this->getTmpXlsFile(), $this->getXlsFile());
32
                exec($command);
33
                return filesize($this->getXlsFile());
34
            }
35
36
        } else {
37
            /** @var LoggerInterface $logger */
38
            $logger = $this->getLogger();
0 ignored issues
show
Bug introduced by
It seems like getLogger() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
            /** @scrutinizer ignore-call */ 
39
            $logger = $this->getLogger();
Loading history...
39
            if($logger) {
0 ignored issues
show
introduced by
$logger is of type Psr\Log\LoggerInterface, thus it always evaluated to true.
Loading history...
40
                $logger->error($this->getName() . ' ERROR: unable write file!');
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

40
                $logger->error($this->/** @scrutinizer ignore-call */ getName() . ' ERROR: unable write file!');
Loading history...
41
            }
42
        }
43
44
        return 0;
45
    }
46
}
47