InkyView::_getCssFromFile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 16
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 10
1
<?php
2
declare(strict_types = 1);
0 ignored issues
show
Coding Style introduced by
Expected no space between directive and the equals sign in a declare statement
Loading history...
Coding Style introduced by
Expected no space between equal sign and the directive value in a declare statement
Loading history...
3
4
namespace Burzum\ZurbInky\View;
5
6
use Cake\View\View;
7
use Pelago\Emogrifier;
8
use Pinky;
0 ignored issues
show
Bug introduced by
The type Pinky was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use RuntimeException;
10
11
/**
12
 * InkyView
13
 *
14
 * @link https://github.com/zurb/inky
15
 * @link https://github.com/lorenzo/pinky
16
 * @link https://github.com/MyIntervals/emogrifier
17
 */
18
class InkyView extends View
19
{
20
    /**
21
     * CSS
22
     *
23
     * @var array
24
     */
25
    protected $_css;
26
27
    /**
28
     * Emogrifier CSS Parser Instance
29
     *
30
     * @link https://github.com/MyIntervals/emogrifier
31
     * @var \Pelago\Emogrifier
32
     */
33
    protected $_emogriefer;
34
35
    /**
36
     * @inheritDoc
37
     */
38 2
    public function initialize()
39
    {
40 2
        parent::initialize();
41
42 2
        $this->_emogriefer = new Emogrifier();
0 ignored issues
show
Deprecated Code introduced by
The class Pelago\Emogrifier has been deprecated: Will be removed for version 4.0.0. Please use the CssInliner class instead. ( Ignorable by Annotation )

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

42
        $this->_emogriefer = /** @scrutinizer ignore-deprecated */ new Emogrifier();
Loading history...
43 2
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 2
    public function render($view = null, $layout = null)
49
    {
50 2
        $output = parent::render($view, $layout);
51 2
        $output = Pinky\transformString($output)->saveHTML();
0 ignored issues
show
Bug introduced by
The function transformString was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

51
        $output = /** @scrutinizer ignore-call */ Pinky\transformString($output)->saveHTML();
Loading history...
52
53 2
        if (!empty($this->_css)) {
54 1
            $this->getEmogriefer()->setCss($this->_css);
0 ignored issues
show
Bug introduced by
$this->_css of type array is incompatible with the type string expected by parameter $css of Pelago\Emogrifier::setCss(). ( Ignorable by Annotation )

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

54
            $this->getEmogriefer()->setCss(/** @scrutinizer ignore-type */ $this->_css);
Loading history...
55 1
            $this->getEmogriefer()->setHtml($output);
56
57 1
            return $this->getEmogriefer()->emogrify();
58
        }
59
60 1
        return $output;
61
    }
62
63
    /**
64
     * Gets the Emogriefer instance
65
     *
66
     * @return \Pelago\Emogrifier
67
     */
68 1
    public function getEmogriefer()
69
    {
70 1
        return $this->_emogriefer;
71
    }
72
73
    /**
74
     * Sets multiple CSS files
75
     *
76
     * @param array $cssFiles A list of CSS files to read
77
     * @return $this
78
     */
79 1
    public function setCssFiles(array $cssFiles)
80
    {
81 1
        $css = '';
82 1
        foreach ($cssFiles as $file) {
83 1
            $css .= $this->_getCssFromFile($file);
84
        }
85
86 1
        $this->setCss($css);
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * Sets the CSS
93
     *
94
     * @param string $css CSS File
95
     * @return $this
96
     */
97 1
    public function setCss(string $css)
98
    {
99 1
        $this->_css .= $css;
100
101 1
        return $this;
102
    }
103
104
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$file" missing
Loading history...
105
     * Gets the CSS from a file
106
     *
107
     * @param string $css CSS File
0 ignored issues
show
introduced by
Doc comment for parameter $css does not match actual variable name $file
Loading history...
108
     * @return string
109
     */
110 1
    protected function _getCssFromFile(string $file) : string
0 ignored issues
show
Coding Style introduced by
There must not be a space before the colon in a return type declaration
Loading history...
111
    {
112 1
        $file = WWW_ROOT . $this->Url->css($file, ['timestamp' => false]);
113
114 1
        if (!file_exists($file)) {
115
            throw new RuntimeException(sprintf('The CSS file `%s` does not exist.', $file));
116
        }
117
118 1
        $css = file_get_contents($file);
119
120
        // Emogriefer works only with UTF-8!
121 1
        if (!mb_check_encoding($css, 'UTF-8')) {
122
            throw new RuntimeException(sprintf('The CSS file `$css` must be UTF-8 encoded!', $file));
123
        }
124
125 1
        return $css;
126
    }
127
}
128