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

XmlCommand::writeXml()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 25
rs 9.7998
cc 4
nc 6
nop 1
1
<?php
2
namespace App\BxConsole\Format;
3
4
use Psr\Log\LoggerInterface;
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
trait XmlCommand {
8
9
    public function getXmlFile($archive = true) {
10
11
        $path = $this->getDocumentRoot() . static::FINAL_FOLDER . static::XML_FILE;
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XmlCommand::XML_FILE 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

11
        $path = $this->/** @scrutinizer ignore-call */ getDocumentRoot() . static::FINAL_FOLDER . static::XML_FILE;
Loading history...
Bug introduced by
The constant App\BxConsole\Format\XmlCommand::FINAL_FOLDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
12
        if(static::GZIP === true && $archive) {
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XmlCommand::GZIP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
13
            $path .= '.gz';
14
        }
15
        return $path;
16
    }
17
18
    protected function getTmpXmlFile($archive = true) {
19
20
        $path = $this->getDocumentRoot() . static::TMP_FOLDER . static::XML_FILE;
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XmlCommand::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\XmlCommand::XML_FILE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
        if(static::GZIP === true && $archive) {
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XmlCommand::GZIP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
            $path .= '.gz';
23
        }
24
        return $path;
25
    }
26
27
    public function writeXml($xml) {
28
29
        @unlink($this->getTmpXmlFile(false));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

29
        /** @scrutinizer ignore-unhandled */ @unlink($this->getTmpXmlFile(false));

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
30
        $res = file_put_contents($this->getTmpXmlFile(false), $xml);
31
        if(static::GZIP) {
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XmlCommand::GZIP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
32
            unlink($this->getTmpXmlFile());
33
            chdir(pathinfo($this->getTmpXmlFile(), PATHINFO_DIRNAME));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($this->getTmpXm...ormat\PATHINFO_DIRNAME) can also be of type array; however, parameter $directory of chdir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

33
            chdir(/** @scrutinizer ignore-type */ pathinfo($this->getTmpXmlFile(), PATHINFO_DIRNAME));
Loading history...
34
            exec("gzip " . self::XML_FILE);
0 ignored issues
show
Bug introduced by
The constant App\BxConsole\Format\XmlCommand::XML_FILE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
        }
36
37
        if($res) {
38
39
            //TODO: check size !!!
40
            $command = sprintf("mv %s %s", $this->getTmpXmlFile(), $this->getXmlFile());
41
            exec($command);
42
43
        } else {
44
            /** @var LoggerInterface $logger */
45
            $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

45
            /** @scrutinizer ignore-call */ 
46
            $logger = $this->getLogger();
Loading history...
46
            if($logger) {
0 ignored issues
show
introduced by
$logger is of type Psr\Log\LoggerInterface, thus it always evaluated to true.
Loading history...
47
                $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

47
                $logger->error($this->/** @scrutinizer ignore-call */ getName() . ' ERROR: unable write file!');
Loading history...
48
            }
49
        }
50
51
        return $res;
52
    }
53
}
54