Passed
Branch dev (8bc649)
by Enjoys
02:24 queued 17s
created

Reader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 72.55%

Importance

Changes 7
Bugs 2 Features 1
Metric Value
eloc 55
c 7
b 2
f 1
dl 0
loc 140
ccs 37
cts 51
cp 0.7255
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getContents() 0 21 4
A readFile() 0 20 3
A readUrl() 0 19 2
A setLogger() 0 3 1
A getContent() 0 10 3
A __construct() 0 10 1
1
<?php
2
3
namespace Enjoys\AssetsCollector\Content;
4
5
use Enjoys\AssetsCollector\Asset;
6
use Enjoys\AssetsCollector\Content\Minify\MinifyFactory;
7
use Enjoys\AssetsCollector\Environment;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Exception\ClientException;
10
use GuzzleHttp\Exception\GuzzleException;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\NullLogger;
13
14
/**
15
 * Class Reader
16
 * @package Enjoys\AssetsCollector\Content
17
 */
18
class Reader
19
{
20
    /**
21
     * @var Asset
22
     */
23
    private Asset $asset;
24
25
    /**
26
     * @var false|string
27
     */
28
    private $content;
29
    /**
30
     * @var false|string
31
     */
32
    private $path;
33
34
    private Environment $environment;
35
    /**
36
     * @var LoggerInterface|NullLogger
37
     */
38
    private LoggerInterface $logger;
39
40
    /**
41
     * Reader constructor.
42
     * @param Asset $asset
43
     * @param Environment $environment
44
     * @param LoggerInterface|null $logger
45
     */
46 6
    public function __construct(
47
        Asset $asset,
48
        Environment $environment,
49
        LoggerInterface $logger = null
50
    ) {
51 6
        $this->environment = $environment;
52 6
        $this->asset = $asset;
53 6
        $this->logger = $logger ?? new NullLogger();
54 6
        $this->path = $this->asset->getPath();
55 6
        $this->content = $this->getContent();
56 6
    }
57
58
    /**
59
     * @throws \Exception
60
     */
61 6
    public function getContents(): string
62
    {
63 6
        if ($this->content === false || $this->path === false) {
64 2
            $this->logger->notice(sprintf('Nothing return: path is `%s`', $this->asset->getOrigPath()));
65 2
            return '';
66
        }
67
68 4
        $replaceRelativeUrls = new ReplaceRelative($this->content, $this->path, $this->asset, $this->environment);
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type true; however, parameter $path of Enjoys\AssetsCollector\C...Relative::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

68
        $replaceRelativeUrls = new ReplaceRelative($this->content, /** @scrutinizer ignore-type */ $this->path, $this->asset, $this->environment);
Loading history...
Bug introduced by
It seems like $this->content can also be of type true; however, parameter $content of Enjoys\AssetsCollector\C...Relative::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

68
        $replaceRelativeUrls = new ReplaceRelative(/** @scrutinizer ignore-type */ $this->content, $this->path, $this->asset, $this->environment);
Loading history...
69 4
        $replaceRelativeUrls->setLogger($this->logger);
70 4
        $this->content = $replaceRelativeUrls->getContent();
71
72
73 4
        if ($this->asset->isMinify()) {
74 2
            $this->content = MinifyFactory::minify(
75 2
                $this->content,
76 2
                $this->asset->getType(),
77 2
                $this->environment
78 2
            )->getContent() . "\n";
79 2
            $this->logger->info(sprintf('Minify: %s', $this->path));
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type true; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

79
            $this->logger->info(sprintf('Minify: %s', /** @scrutinizer ignore-type */ $this->path));
Loading history...
80
        }
81 4
        return $this->content;
82
    }
83
84
85
    /**
86
     * @return false|string
87
     */
88 6
    private function getContent()
89
    {
90 6
        if ($this->path === false) {
91 1
            return false;
92
        }
93
94 5
        if ($this->asset->isUrl()) {
95
            return $this->readUrl($this->path);
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type true; however, parameter $url of Enjoys\AssetsCollector\Content\Reader::readUrl() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

95
            return $this->readUrl(/** @scrutinizer ignore-type */ $this->path);
Loading history...
96
        }
97 5
        return $this->readFile($this->path);
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type true; however, parameter $filename of Enjoys\AssetsCollector\Content\Reader::readFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

97
        return $this->readFile(/** @scrutinizer ignore-type */ $this->path);
Loading history...
98
    }
99
100
    /**
101
     * @param string $url
102
     * @return false|string
103
     */
104
    private function readUrl(string $url)
105
    {
106
        try {
107
            $client = new Client(
108
                [
109
                    'verify' => false,
110
                    'allow_redirects' => true,
111
                    'headers' => [
112
                        'User-Agent' =>
113
                            'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
114
                    ]
115
                ]
116
            );
117
            $response = $client->get($url);
118
            $this->logger->info(sprintf('Read: %s', $url));
119
            return $response->getBody()->getContents();
120
        } catch (ClientException | GuzzleException $e) {
121
            $this->logger->notice($e->getMessage());
122
            return false;
123
        }
124
    }
125
126
    /**
127
     * @param string $filename
128
     * @return false|string
129
     */
130 5
    private function readFile(string $filename)
131
    {
132
        //Clear the most recent error
133 5
        error_clear_last();
134
135 5
        if (!file_exists($filename)) {
136
            $this->logger->notice(sprintf("Файла по указанному пути нет: %s", $filename));
137
            return false;
138
        }
139 5
        $content = @file_get_contents($filename);
140
141
        /** @var null|string[] $error */
142 5
        $error = error_get_last();
143 5
        if ($error !== null) {
144 1
            $this->logger->notice(sprintf("Ошибка чтения содержимого файла: %s", $error['message']));
145 1
            return false;
146
        }
147
148 4
        $this->logger->info(sprintf('Read: %s', $filename));
149 4
        return $content;
150
    }
151
152
    /**
153
     * @param LoggerInterface|NullLogger $logger
154
     */
155
    public function setLogger($logger): void
156
    {
157
        $this->logger = $logger;
158
    }
159
}
160