Passed
Push — master ( effdba...b9addd )
by Enjoys
01:32
created

Reader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 69.77%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
c 2
b 0
f 0
dl 0
loc 117
ccs 30
cts 43
cp 0.6977
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getContents() 0 18 4
A readFile() 0 20 3
A readUrl() 0 19 2
A getContent() 0 7 2
1
<?php
2
3
namespace Enjoys\AssetsCollector\Content;
4
5
use Enjoys\AssetsCollector\Asset;
6
use Enjoys\AssetsCollector\Content\Minify\MinifyFactory;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\ClientException;
9
use GuzzleHttp\Exception\GuzzleException;
10
use Psr\Log\LoggerAwareTrait;
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
    use LoggerAwareTrait;
21
22
    /**
23
     * @var Asset
24
     */
25
    private Asset $asset;
26
27
    /**
28
     * @var false|string
29
     */
30
    private $content;
31
32
    /**
33
     * @var array{css: array<mixed>, js: array<mixed>}
34
     */
35
    private array $minifyOptions;
36
37
    /**
38
     * Reader constructor.
39
     * @param Asset $asset
40
     * @param array{css: array<mixed>, js: array<mixed>} $minifyOptions
41
     * @param LoggerInterface|null $logger
42
     */
43 5
    public function __construct(Asset $asset, array $minifyOptions, LoggerInterface $logger = null)
44
    {
45 5
        $this->asset = $asset;
46 5
        $this->logger = $logger ?? new NullLogger();
47
48 5
        $this->content = $this->getContent();
49 5
        $this->minifyOptions = $minifyOptions;
50 5
    }
51
52 5
    public function getContents(): string
53
    {
54 5
        if (false === $this->content) {
55 2
            $this->logger->notice(sprintf('Nothing return: %s', $this->asset->getPath()));
56 2
            return '';
57
        }
58
59 3
        if ($this->asset->isUrl()) {
60
            $replaceRelativeUrls = new ReplaceRelativeUrls($this->content, $this->asset->getPath());
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...tiveUrls::__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

60
            $replaceRelativeUrls = new ReplaceRelativeUrls(/** @scrutinizer ignore-type */ $this->content, $this->asset->getPath());
Loading history...
61
            $replaceRelativeUrls->setLogger($this->logger);
62
            $this->content = $replaceRelativeUrls->getContent();
63
        }
64
65 3
        if ($this->asset->isMinify()) {
66 2
            $this->content = MinifyFactory::minify($this->content, $this->asset->getType(), $this->minifyOptions)->getContent() . "\n";
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

66
            $this->content = MinifyFactory::minify(/** @scrutinizer ignore-type */ $this->content, $this->asset->getType(), $this->minifyOptions)->getContent() . "\n";
Loading history...
67 2
            $this->logger->info(sprintf('Minify: %s', $this->asset->getPath()));
68
        }
69 3
        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...
70
    }
71
72
73
    /**
74
     * @return false|string
75
     */
76 5
    private function getContent()
77
    {
78 5
        if ($this->asset->isUrl()) {
79
            return $this->readUrl($this->asset->getPath());
80
        }
81
82 5
        return $this->readFile($this->asset->getPath());
83
    }
84
85
    /**
86
     * @param string $url
87
     * @return false|string
88
     */
89
    private function readUrl(string $url)
90
    {
91
        try {
92
            $client = new Client(
93
                [
94
                    'verify' => false,
95
                    'allow_redirects' => true,
96
                    'headers' => [
97
                        'User-Agent' =>
98
                            'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
99
                    ]
100
                ]
101
            );
102
            $response = $client->get($url);
103
            $this->logger->info(sprintf('Read: %s', $url));
104
            return $response->getBody()->getContents();
105
        } catch (ClientException | GuzzleException $e) {
106
            $this->logger->notice($e->getMessage());
107
            return false;
108
        }
109
    }
110
111
    /**
112
     * @param string $filename
113
     * @return false|string
114
     */
115 5
    private function readFile(string $filename)
116
    {
117
        //Clear the most recent error
118 5
        error_clear_last();
119
120 5
        if (!file_exists($filename)) {
121 1
            $this->logger->notice(sprintf("Файла по указанному пути нет: %s", $filename));
122 1
            return false;
123
        }
124 4
        $content = @file_get_contents($filename);
125
126
        /** @var null|string[] $error */
127 4
        $error = error_get_last();
128 4
        if ($error !== null) {
129 1
            $this->logger->notice(sprintf("Ошибка чтения содержимого файла: %s", $error['message']));
130 1
            return false;
131
        }
132
133 3
        $this->logger->info(sprintf('Read: %s', $filename));
134 3
        return $content;
135
    }
136
}
137