Passed
Branch develop (eac79f)
by Guillaume
10:13
created

Alpinejs::removeRepositoryNavigation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Docsets;
4
5
use Godbout\DashDocsetBuilder\Docsets\BaseDocset;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Storage;
8
use Wa72\HtmlPageDom\HtmlPageCrawler;
9
10
class Alpinejs extends BaseDocset
11
{
12
    public const CODE = 'alpinejs';
13
    public const NAME = 'AlpineJS';
14
    public const URL = 'github.com';
15
    public const INDEX = 'alpinejs/alpine.html';
16
    public const PLAYGROUND = '';
17
    public const ICON_16 = '../../icons/icon.png';
18
    public const ICON_32 = '../../icons/[email protected]';
19
    public const EXTERNAL_DOMAINS = [
20
    ];
21
22
23
    public function grab(): bool
24
    {
25
        $toIgnore = implode('|', [
26
            '/blame',
27
            '/blob',
28
        ]);
29
30
        $toGet = implode('|', [
31
            '.*\.githubusercontent\.com',
32
           '\.css',
33
            '\.ico',
34
            '\.js',
35
            '\.png',
36
            '\.svg',
37
            '/css',
38
        ]);
39
40
        system(
41
            "echo; wget github.com/alpinejs/alpine \
42
                --mirror \
43
                --trust-server-names \
44
                --reject-regex='{$toIgnore}' \
45
                --accept-regex='{$toGet}' \
46
                --ignore-case \
47
                --page-requisites \
48
                --adjust-extension \
49
                --convert-links \
50
                --span-hosts \
51
                --domains={$this->externalDomains()} \
52
                --directory-prefix=storage/{$this->downloadedDirectory()} \
53
                -e robots=off \
54
                --quiet \
55
                --show-progress",
56
            $result
57
        );
58
59
        return $result === 0;
60
    }
61
62 8
    public function entries(string $file): Collection
63
    {
64 8
        $crawler = HtmlPageCrawler::create(Storage::get($file));
65
66 8
        $entries = collect();
67
68 8
        $entries = $entries->merge($this->guideEntries($crawler, $file));
69 8
        $entries = $entries->merge($this->sectionEntries($crawler, $file));
70
71 8
        return $entries;
72
    }
73
74 8
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed. ( Ignorable by Annotation )

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

74
    protected function guideEntries(HtmlPageCrawler $crawler, /** @scrutinizer ignore-unused */ string $file)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76 8
        $entries = collect();
77
78
        $crawler->filter('.entry-content h2')->each(function (HtmlPageCrawler $node) use ($entries) {
79 8
            $entries->push([
80 8
                'name' => trim($node->text()),
81 8
                'type' => 'Guide',
82 8
                'path' => $this->url() . '/alpinejs/' . $node->children('a')->attr('href')
83
            ]);
84 8
        });
85
86 8
        return $entries;
87
    }
88
89 8
    protected function sectionEntries(HtmlPageCrawler $crawler, string $file)
90
    {
91 8
        $entries = collect();
92
93
        $crawler->filter('.entry-content h3')->each(function (HtmlPageCrawler $node) use ($entries, $file) {
0 ignored issues
show
Unused Code introduced by
The import $file is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
94 8
            $entries->push([
95 8
                'name' => trim($node->text()),
96 8
                'type' => 'Section',
97 8
                'path' => $this->url() . '/alpinejs/' . $node->children('a')->attr('href')
98
            ]);
99 8
        });
100
101 8
        return $entries;
102
    }
103
104 4
    public function format(string $file): string
105
    {
106 4
        $crawler = HtmlPageCrawler::create(Storage::get($file));
107
108 4
        $this->removeGitHubNavbar($crawler);
109 4
        $this->removeRepositoryNavigation($crawler);
110 4
        $this->removeSignupPrompt($crawler);
111 4
        $this->removeFileNavigation($crawler);
112 4
        $this->removeRepositoryDetails($crawler);
113 4
        $this->removeRightSidebar($crawler);
114 4
        $this->removeWeirdBoxHeader($crawler);
115 4
        $this->removeContentBorder($crawler);
116
117 4
        $this->removeFooter($crawler);
118
119 4
        $this->removeCrossOriginAndIntegrity($crawler);
120
121 4
        return $crawler->saveHTML();
122
    }
123
124 4
    protected function removeGitHubNavbar(HtmlPageCrawler $crawler)
125
    {
126 4
        $crawler->filter('.js-header-wrapper')->remove();
127 4
    }
128
129 4
    protected function removeRepositoryNavigation(HtmlPageCrawler $crawler)
130
    {
131 4
        $crawler->filter('.bg-gray-light.pt-3.hide-full-screen.mb-5')->remove();
132 4
    }
133
134 4
    protected function removeSignupPrompt(HtmlPageCrawler $crawler)
135
    {
136 4
        $crawler->filter('signup-prompt')->remove();
137 4
    }
138
139 4
    protected function removeFileNavigation(HtmlPageCrawler $crawler)
140
    {
141 4
        $crawler->filter('.file-navigation')->remove();
142 4
    }
143
144 4
    protected function removeRepositoryDetails(HtmlPageCrawler $crawler)
145
    {
146 4
        $crawler->filter('.Box.mb-3')->remove();
147 4
    }
148
149 4
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
150
    {
151 4
        $crawler->filter('.flex-shrink-0.col-12.col-md-3')->remove();
152 4
    }
153
154 4
    protected function removeWeirdBoxHeader(HtmlPageCrawler $crawler)
155
    {
156 4
        $crawler->filter('.Box-header')->remove();
157 4
    }
158
159 4
    protected function removeContentBorder(HtmlPageCrawler $crawler)
160
    {
161 4
        $crawler->filter('#readme')->addClass('border-0 !important');
162 4
    }
163
164 4
    protected function removeFooter(HtmlPageCrawler $crawler)
165
    {
166 4
        $crawler->filter('.footer')->remove();
167 4
    }
168
169 4
    protected function removeCrossOriginAndIntegrity(HtmlPageCrawler $crawler)
170
    {
171 4
        $crawler->filter('script, link')
172 4
            ->removeAttribute('integrity')
173 4
            ->removeAttribute('crossorigin');
174 4
    }
175
}
176