Passed
Push — master ( cbed4f...9c1969 )
by Enjoys
05:31
created

ReplaceRelativeUrls::getContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 13
ccs 8
cts 10
cp 0.8
crap 2.032
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\AssetsCollector\Content;
6
7
use Enjoys\UrlConverter;
8
use Psr\Log\LoggerAwareTrait;
9
use Psr\Log\NullLogger;
10
11
/**
12
 * Class ReplaceRelativeUrls
13
 * @package Enjoys\AssetsCollector\Content
14
 */
15
class ReplaceRelativeUrls
16
{
17
    use LoggerAwareTrait;
18
19
    private string $content;
20
21
    private string $domain;
0 ignored issues
show
introduced by
The private property $domain is not used, and could be removed.
Loading history...
22
  //  private UriInterface $uri;
23
    private string $url;
24
25 3
    public function __construct(string $content, string $url)
26
    {
27 3
        $this->content = $content;
28 3
        $this->logger = new NullLogger();
29 3
        $this->url = $url;
30 3
    }
31
32
33
    /**
34
     * @return string
35
     */
36 3
    public function getContent(): string
37
    {
38 3
        $result = preg_replace_callback('/(url\([\'"]?)(?!["\'a-z]+:|[\'"]?\/{2})(.+[^\'"])([\'"]?\))/i', function ($m){
39 3
            $urlConverter = new UrlConverter();
40 3
            return $m[1] . $urlConverter->relativeToAbsolute($this->url, $m[2]) . $m[3];
0 ignored issues
show
Bug introduced by
Are you sure $urlConverter->relativeT...lute($this->url, $m[2]) of type false can be used in concatenation? ( Ignorable by Annotation )

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

40
            return $m[1] . /** @scrutinizer ignore-type */ $urlConverter->relativeToAbsolute($this->url, $m[2]) . $m[3];
Loading history...
41 3
        }, $this->content);
42
43 3
        if ($result === null) {
44
            $this->logger->notice(sprintf('Regex return null value. Returned empty string: %s', $this->url));
45
            return '';
46
        }
47 3
        $this->logger->info(sprintf('ReplaceRelativeUrls: %s', $this->url));
48 3
        return $result;
49
    }
50
51
52
53
54
}
55