Passed
Branch master (cbed4f)
by Enjoys
28:49
created

ReplaceRelativeUrls::getDomain()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\AssetsCollector\Content;
6
7
use Psr\Http\Message\UriInterface;
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 3
    private string $url;
24
25 3
    public function __construct(string $content, string $url)
26 3
    {
27 3
        $this->content = $content;
28 3
        $this->logger = new NullLogger();
29 3
        $this->url = $url;
30
    }
31
32
33
    /**
34 3
     * @return string
35
     */
36 3
    public function getContent(): string
37
    {
38 3
        $result = preg_replace_callback('/(url\([\'"]?)(?!["\'a-z]+:|[\'"]?\/{2})(.+[^\'"])([\'"]?\))/i', function ($m){
39
            $urlConverter = new UrlConverter();
40
            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
        }, $this->content);
42 3
43 3
        if ($result === null) {
44
            $this->logger->notice(sprintf('Regex return null value. Returned empty string: %s', $this->url));
45
            return '';
46 3
        }
47
        $this->logger->info(sprintf('ReplaceRelativeUrls: %s', $this->url));
48 3
        return $result;
49
    }
50
51 3
52
53 3
54
}
55