LoggableGenerator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 106
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getInternalGenerator() 0 4 1
A generate() 0 11 2
A generateFromHtml() 0 8 2
A getOutput() 0 11 2
A getOutputFromHtml() 0 8 2
A setOption() 0 6 1
A logDebug() 0 8 2
1
<?php
2
3
namespace Knp\Bundle\SnappyBundle\Snappy\Generator;
4
5
@trigger_error('Logging capability is now directly integrated in Snappy. You should call setLogger on your generator rather than using this decorator.', E_USER_DEPRECATED);
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...
6
7
use Knp\Snappy\GeneratorInterface;
8
use Psr\Log\LoggerInterface;
9
10
/**
11
 * Wraps a GeneratorInterface instance to log the media generations using the
12
 * configured logger.
13
 *
14
 * @deprecated Logging capability is now directly integrated in Snappy. You should use it rather than this Decorator.
15
 */
16
class LoggableGenerator implements GeneratorInterface
17
{
18
    private $generator;
19
    private $logger;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param GeneratorInterface $generator
25
     * @param LoggerInterface    $logger
0 ignored issues
show
Documentation introduced by
Should the type for parameter $logger not be null|LoggerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
26
     */
27
    public function __construct(GeneratorInterface $generator, LoggerInterface $logger = null)
28
    {
29
        $this->generator = $generator;
30
        $this->logger = $logger;
31
    }
32
33
    /**
34
     * Returns the underlying generator instance.
35
     *
36
     * @return GeneratorInterface
37
     */
38
    public function getInternalGenerator()
39
    {
40
        return $this->generator;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function generate($input, $output, array $options = [], $overwrite = false)
47
    {
48
        if (is_array($input)) {
49
            $debug_input = implode(', ', $input);
50
        } else {
51
            $debug_input = $input;
52
        }
53
        $this->logDebug(sprintf('Generate from file (%s) to file (%s).', $debug_input, $output));
54
55
        $this->generator->generate($input, $output, $options, $overwrite);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function generateFromHtml($html, $output, array $options = [], $overwrite = false)
62
    {
63
        $debugHtml = is_array($html) ? implode(', ', $html) : $html;
64
65
        $this->logDebug(sprintf('Generate from HTML (%s) to file (%s).', substr($debugHtml, 0, 100), $output));
66
67
        $this->generator->generateFromHtml($html, $output, $options, $overwrite);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getOutput($input, array $options = [])
74
    {
75
        if (is_array($input)) {
76
            $debug_input = implode(', ', $input);
77
        } else {
78
            $debug_input = $input;
79
        }
80
        $this->logDebug(sprintf('Output from file (%s).', $debug_input));
81
82
        return $this->generator->getOutput($input, $options);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getOutputFromHtml($html, array $options = [])
89
    {
90
        $debugHtml = is_array($html) ? implode(', ', $html) : $html;
91
92
        $this->logDebug(sprintf('Output from HTML (%s).', substr($debugHtml, 0, 100)));
93
94
        return $this->generator->getOutputFromHtml($html, $options);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function setOption($name, $value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
101
    {
102
        $this->logDebug(sprintf('Set option %s = %s.', $name, var_export($value, true)));
103
104
        return $this->generator->setOption($name, $value);
105
    }
106
107
    /**
108
     * Logs the given debug message if the logger is configured or do nothing
109
     * otherwise.
110
     *
111
     * @param string $message
112
     */
113
    private function logDebug($message)
114
    {
115
        if (null === $this->logger) {
116
            return;
117
        }
118
119
        $this->logger->debug($message);
120
    }
121
}
122