Passed
Push — dev ( d209c3...c1a5f7 )
by Enjoys
03:20
created

Reader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 3
b 1
f 0
nc 1
nop 3
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 1
rs 10
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 7
    public function __construct(
47
        Asset $asset,
48
        Environment $environment,
49
        LoggerInterface $logger = null
50
    ) {
51 7
        $this->environment = $environment;
52 7
        $this->asset = $asset;
53 7
        $this->logger = $logger ?? new NullLogger();
54 7
        $this->path = $this->asset->getPath();
55 7
        $this->content = $this->getContent();
56 7
    }
57
58
    /**
59
     * @throws \Exception
60
     */
61 7
    public function getContents(): string
62
    {
63 7
        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 5
        if ($this->asset->isReplaceRelativeUrls()) {
69 4
            $replaceRelativeUrls = new ReplaceRelative($this->content, $this->path, $this->asset, $this->environment);
0 ignored issues
show
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

69
            $replaceRelativeUrls = new ReplaceRelative(/** @scrutinizer ignore-type */ $this->content, $this->path, $this->asset, $this->environment);
Loading history...
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

69
            $replaceRelativeUrls = new ReplaceRelative($this->content, /** @scrutinizer ignore-type */ $this->path, $this->asset, $this->environment);
Loading history...
70 4
            $replaceRelativeUrls->setLogger($this->logger);
71 4
            $this->content = $replaceRelativeUrls->getContent();
72
        }
73
74 5
        if ($this->asset->isMinify()) {
75 2
            $this->content = MinifyFactory::minify(
76 2
                    $this->content,
0 ignored issues
show
Bug introduced by
It seems like $this->content can also be of type true; however, parameter $content of Enjoys\AssetsCollector\C...MinifyFactory::minify() 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

76
                    /** @scrutinizer ignore-type */ $this->content,
Loading history...
77 2
                    $this->asset->getType(),
78 2
                    $this->environment
79 2
                )->getContent() . "\n";
80 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

80
            $this->logger->info(sprintf('Minify: %s', /** @scrutinizer ignore-type */ $this->path));
Loading history...
81
        }
82 5
        return $this->content;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->content could return the type true which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
83
    }
84
85
86
    /**
87
     * @return false|string
88
     */
89 7
    private function getContent()
90
    {
91 7
        if ($this->path === false) {
92 1
            return false;
93
        }
94
95 6
        if ($this->asset->isUrl()) {
96
            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

96
            return $this->readUrl(/** @scrutinizer ignore-type */ $this->path);
Loading history...
97
        }
98 6
        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

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