Compiler::getCssFilesContent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Lanin\Laravel\EmailTemplatesOptimization;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\View\Compilers\BladeCompiler;
7
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
8
9
class Compiler extends BladeCompiler
10
{
11
    /**
12
     * @var array
13
     */
14
    private $cssFiles = [];
15
16
    /**
17
     * @var string|null
18
     */
19
    protected static $cssFilesContent = null;
20
21
    /**
22
     * EmailCompiler constructor.
23
     *
24
     * @param Filesystem $files
25
     * @param string $cachePath
26
     * @param array $cssFiles
27
     */
28 2
    public function __construct(Filesystem $files, $cachePath, array $cssFiles)
29
    {
30 2
        parent::__construct($files, $cachePath);
31
32 2
        $this->cssFiles = $cssFiles;
33 2
    }
34
35
    /**
36
     * Concat all css files.
37
     *
38
     * @param  array $files
39
     * @return string
40
     */
41 1
    protected function getCssFilesContent(array $files)
42
    {
43 1
        $css = '';
44 1
        foreach ($files as $file) {
45 1
            $css .= $this->files->get($file);
46 1
        }
47
48 1
        return $css;
49
    }
50
51
    /**
52
     * Compile the given Blade template contents.
53
     *
54
     * @param  string  $value
55
     * @return string
56
     */
57 1
    public function compileString($value)
58
    {
59 1
        if (is_null(static::$cssFilesContent)) {
60 1
            static::$cssFilesContent = $this->getCssFilesContent($this->cssFiles);
61 1
        }
62
63 1
        return parent::compileString($this->convertStyles($value));
64
    }
65
66
    /**
67
     * Convert css to inline styles.
68
     *
69
     * @param  string $value
70
     * @return string
71
     */
72 1
    protected function convertStyles($value)
73
    {
74 1
        $converter = new CssToInlineStyles();
75
76 1
        return urldecode($converter->convert($value, static::$cssFilesContent));
77
    }
78
}