Passed
Push — master ( 02d42a...eb71ce )
by Albin
59s
created

LoggableGenerator::setOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Knp\Bundle\SnappyBundle\Snappy\Generator;
4
5
use Knp\Snappy\GeneratorInterface;
6
use Psr\Log\LoggerInterface;
7
8
/**
9
 * Wraps a GeneratorInterface instance to log the media generations using the
10
 * configured logger.
11
 */
12
class LoggableGenerator implements GeneratorInterface
13
{
14
    private $generator;
15
    private $logger;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param GeneratorInterface $generator
21
     * @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...
22
     */
23
    public function __construct(GeneratorInterface $generator, LoggerInterface $logger = null)
24
    {
25
        $this->generator = $generator;
26
        $this->logger = $logger;
27
    }
28
29
    /**
30
     * Returns the underlying generator instance.
31
     *
32
     * @return GeneratorInterface
33
     */
34
    public function getInternalGenerator()
35
    {
36
        return $this->generator;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function generate($input, $output, array $options = [], $overwrite = false)
43
    {
44
        if (is_array($input)) {
45
            $debug_input = implode(', ', $input);
46
        } else {
47
            $debug_input = $input;
48
        }
49
        $this->logDebug(sprintf('Generate from file (%s) to file (%s).', $debug_input, $output));
50
51
        $this->generator->generate($input, $output, $options, $overwrite);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function generateFromHtml($html, $output, array $options = [], $overwrite = false)
58
    {
59
        $debugHtml = is_array($html) ? implode(', ', $html) : $html;
60
61
        $this->logDebug(sprintf('Generate from HTML (%s) to file (%s).', substr($debugHtml, 0, 100), $output));
62
63
        $this->generator->generateFromHtml($html, $output, $options, $overwrite);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getOutput($input, array $options = [])
70
    {
71
        if (is_array($input)) {
72
            $debug_input = implode(', ', $input);
73
        } else {
74
            $debug_input = $input;
75
        }
76
        $this->logDebug(sprintf('Output from file (%s).', $debug_input));
77
78
        return $this->generator->getOutput($input, $options);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getOutputFromHtml($html, array $options = [])
85
    {
86
        $debugHtml = is_array($html) ? implode(', ', $html) : $html;
87
88
        $this->logDebug(sprintf('Output from HTML (%s).', substr($debugHtml, 0, 100)));
89
90
        return $this->generator->getOutputFromHtml($html, $options);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    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...
97
    {
98
        $this->logDebug(sprintf('Set option %s = %s.', $name, var_export($value, true)));
99
100
        return $this->generator->setOption($name, $value);
101
    }
102
103
    /**
104
     * Logs the given debug message if the logger is configured or do nothing
105
     * otherwise.
106
     *
107
     * @param string $message
108
     */
109
    private function logDebug($message)
110
    {
111
        if (null === $this->logger) {
112
            return;
113
        }
114
115
        $this->logger->debug($message);
116
    }
117
}
118