DumpCommand.php ➔ file_put_contents()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 2
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Symfony\Bundle\AsseticBundle\Command;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
7
/**
8
 * Class GzipProperties for wrapping variables to file_put_contents
9
 *
10
 * @author KonstantinKuklin <[email protected]>
11
 */
12
class GzipProperties
13
{
14
    /**
15
     * Is compression enabled
16
     *
17
     * @var bool
18
     */
19
    public static $use = false;
20
21
    /**
22
     * Level of compression
23
     *
24
     * @var int
25
     */
26
    public static $level = 9;
27
28
    /**
29
     * Output flow interface
30
     *
31
     * @var OutputInterface
32
     */
33
    public static $output = null;
34
}
35
36
/**
37
 * Override standard function file_put_contents for
38
 * Symfony\Bundle\AsseticBundle\Command namespace
39
 *
40
 * @param string $filePath Assetic file path
41
 * @param string $content  Assetic file content
42
 */
43
function file_put_contents($filePath, $content)
44
{
45
    // save assetic file
46
    @\file_put_contents($filePath, $content);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
47
48
    // check gzipping
49
    if (GzipProperties::$use) {
50
51
        // check gzip extension
52
        if (!function_exists("gzencode")) {
53
            throw new \RuntimeException('Unable to find Zlib library');
54
        }
55
56
        $filePath .= '.gz';
57
        GzipProperties::$output->writeln(
58
            sprintf(
59
                '<comment>%s</comment> <info>[gzipped file+]</info> %s',
60
                date('H:i:s'),
61
                $filePath
62
            )
63
        );
64
65
        if (false === @\file_put_contents($filePath, gzencode($content, GzipProperties::$level))) {
66
            throw new \RuntimeException('Unable to write file ' . $filePath);
67
        }
68
    }
69
}
70
71
namespace KonstantinKuklin\AsseticStaticGzipBundle\Command;
72
73
use Symfony\Bundle\AsseticBundle\Command\DumpCommand as BaseCommand;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
74
use Symfony\Bundle\AsseticBundle\Command\GzipProperties;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
75
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
76
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Coding Style introduced by
USE declarations must go after the first namespace declaration
Loading history...
77
78
/**
79
 * Override assetic bundle command for gzip purposes.
80
 *
81
 * @author Konstantin Kuklin <[email protected]>
82
 */
83
class DumpCommand extends BaseCommand
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
84
{
85
    /**
86
     * {@inheritdoc}
87
     */
88
    protected function initialize(InputInterface $input, OutputInterface $output)
89
    {
90
        // save properties
91
        GzipProperties::$use = $this->getContainer()->getParameter('assetic_static_gzip.use');
92
        GzipProperties::$level = $this->getContainer()->getParameter('assetic_static_gzip.level');
93
        GzipProperties::$output = $output;
94
95
        parent::initialize($input, $output);
96
    }
97
}
98