Completed
Push — master ( 174ea9...d0877c )
by Vitaly
05:06
created

CSS::rewriteUrls()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 11

Duplication

Lines 6
Ratio 24 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 4
eloc 11
c 2
b 0
f 2
nc 8
nop 1
dl 6
loc 25
ccs 12
cts 12
cp 1
crap 4
rs 8.5806
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 07.07.16 at 18:19
5
 */
6
namespace samsonphp\resource;
7
8
use samsonphp\resource\exception\ResourceNotFound;
9
10
/**
11
 * CSS assets handling class
12
 *
13
 * @author Vitaly Iegorov <[email protected]>
14
 * @package samsonphp\resource
15
 */
16
class CSS
17
{
18
    /** Pattern for matching CSS url */
19
    const P_URL = '/url\s*\(\s*(\'|\")?([^\)\s\'\"]+)(\'|\")?\s*\)/i';
20
21
    /** @var string Path to current resource file */
22
    protected $currentResource;
23
24
    /**
25
     * LESS resource compiler.
26
     *
27
     * @param string $resource  Resource full path
28
     * @param string $extension Resource extension
29
     * @param string $content   Compiled output resource content
30
     */
31 5
    public function compile($resource, $extension, &$content)
32
    {
33 5
        if ($extension === 'css') {
34 5
            $this->currentResource = $resource;
35
36
            // Rewrite Urls
37 5
            $content = preg_replace_callback(self::P_URL, [$this, 'rewriteUrls'], $content);
38 4
        }
39 4
    }
40
41
    /**
42
     * Callback for CSS url(...) rewriting.
43
     *
44
     * @param array $matches Regular expression matches collection
45
     *
46
     * @return string Rewritten url(..) with static resource handler url
47
     * @throws ResourceNotFound
48
     */
49 4
    public function rewriteUrls($matches)
50
    {
51
        // Store static resource path
52 4
        $url = $matches[2];
53
54
        // Remove possible GET parameters from resource path
55 4 View Code Duplication
        if (($getStart = strpos($url, '?')) !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56 1
            $url = substr($url, 0, $getStart);
57 1
        }
58
59
        // Remove possible HASH parameters from resource path
60 4 View Code Duplication
        if (($getStart = strpos($url, '#')) !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61 2
            $url = substr($url, 0, $getStart);
62 2
        }
63
64
        // Try to find resource and output full error
65
        try {
66 4
            $path = ResourceValidator::getProjectRelativePath($url, dirname($this->currentResource));
67 4
        } catch (ResourceNotFound $e) {
68 1
            throw new ResourceNotFound('Cannot find resource "' . $url . '" in "' . $this->currentResource . '"');
69
        }
70
71
        // Build path to static resource handler
72 3
        return 'url("/' . STATIC_RESOURCE_HANDLER . '/?p=' . $path . '")';
73
    }
74
}
75