Completed
Push — master ( eee1b0...63765d )
by Vitaly
04:35 queued 02:12
created

Module::renderer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 3
crap 12
1
<?php
2
namespace samsonphp\less;
3
4
use samson\resourcer\ResourceRouter;
5
use samsonframework\core\LoadableInterface;
6
use samsonphp\event\Event;
7
use Symfony\Component\Config\Definition\Exception\Exception;
8
9
/**
10
 * SamsonPHP LESS compiler module.
11
 *
12
 * @author Vitaly Iegorov <[email protected]>
13
 * @author Nikita Kotenko <[email protected]>
14
 */
15
class Module implements LoadableInterface
16
{
17
    /** @var \lessc LESS compiler */
18
    protected $less;
19
20
    /** SamsonFramework load preparation stage handler */
21 1
    public function prepare()
22
    {
23 1
        Event::subscribe(ResourceRouter::EVENT_CREATED, array($this, 'renderer'));
24
25 1
        $this->less = new \lessc;
26
27 1
        return true;
28
    }
29
30
    /**
31
     * New resource file update handler.
32
     *
33
     * @param string $type    Resource type(extension)
34
     * @param string $content Resource content
35
     * @param string $file    LESS file path
36
     *
37
     * @throws \Exception
38
     */
39
    public function renderer($type, &$content, $file)
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        // If CSS resource has been updated
42
        if ($type === 'css') {
43
            try {
44
                // Read updated CSS resource file and compile it
45
                $content = $this->less->compile($content);
46
            } catch (\Exception $e) {
47
                $errorFile = 'cache/error_resourcer'.microtime(true).'.less';
48
                file_put_contents($errorFile, $content);
49
                throw new \Exception('Failed compiling LESS['.$errorFile.']:'."\n".$e->getMessage());
50
            }
51
        }
52
    }
53
}
54