Passed
Branch master (a15932)
by Guillaume
09:38
created

Ploi::guideEntries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace App\Docsets;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use Wa72\HtmlPageDom\HtmlPageCrawler;
8
use Illuminate\Support\Facades\Storage;
9
10
class Ploi extends BaseDocset
11
{
12
    public const CODE = 'ploi-api';
13
    public const NAME = 'Ploi API';
14
    public const URL = 'developers.ploi.io';
15
    public const INDEX = 'index.html';
16
    public const PLAYGROUND = '';
17
    public const ICON_16 = '../documentator.s3.eu-west-3.amazonaws.com/11/conversions/favicon-favicon-16.png';
18
    public const ICON_32 = '../documentator.s3.eu-west-3.amazonaws.com/11/conversions/favicon-favicon-32.png';
19
    public const EXTERNAL_DOMAINS = [
20
        'documentator.s3.eu-west-3.amazonaws.com'
21
    ];
22
23
24
    public function grab(): bool
25
    {
26
        system(
27
            "echo; wget {$this->url()} \
28
                --mirror \
29
                --trust-server-names \
30
                --page-requisites \
31
                --adjust-extension \
32
                --convert-links \
33
                --span-hosts \
34
                --domains={$this->externalDomains()} \
35
                --directory-prefix=storage/{$this->downloadedDirectory()} \
36
                -e robots=off \
37
                --quiet \
38
                --show-progress",
39
            $result
40
        );
41
42
        return $result === 0;
43
    }
44
45 2
    public function entries(string $file): Collection
46
    {
47 2
        $crawler = HtmlPageCrawler::create(Storage::get($file));
48
49 2
        $entries = collect();
50 2
        $entries = $entries->merge($this->guideEntries($crawler, $file));
51
52 2
        return $entries;
53
    }
54
55 2
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
56
    {
57 2
        $entries = collect();
58
59 2
        if (Str::contains($file, "{$this->url()}/index.html")) {
60
            $crawler->filter('aside a.ml-2')->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...
61 2
                $entries->push([
62 2
                    'name' => trim($node->text()),
63 2
                    'type' => 'Guide',
64 2
                    'path' => $this->url() . '/' . $node->attr('href'),
65
                ]);
66 2
            });
67
        }
68
69 2
        return $entries;
70
    }
71
72 2
    public function format(string $html): string
73
    {
74 2
        $crawler = HtmlPageCrawler::create($html);
75
76 2
        $this->removeHeader($crawler);
77 2
        $this->removeSidebar($crawler);
78 2
        $this->removePreviousAndNextNavigation($crawler);
79 2
        $this->updateTopPadding($crawler);
80 2
        $this->removeUnwantedJavaScript($crawler);
81 2
        $this->insertDashTableOfContents($crawler);
82
83 2
        return $crawler->saveHTML();
84
    }
85
86 2
    protected function removeHeader(HtmlPageCrawler $crawler)
87
    {
88 2
        $crawler->filter('header')->remove();
89 2
    }
90
91 2
    protected function removeSidebar(HtmlPageCrawler $crawler)
92
    {
93 2
        $crawler->filter('aside')->remove();
94 2
    }
95
96 2
    protected function removePreviousAndNextNavigation(HtmlPageCrawler $crawler)
97
    {
98 2
        $crawler->filter('#previous-and-next')->remove();
99 2
    }
100
101 2
    protected function updateTopPadding(HtmlPageCrawler $crawler)
102
    {
103 2
        $crawler->filter('main')
104 2
            ->addClass('md:pt-12')
105
        ;
106 2
    }
107
108 2
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
109
    {
110 2
        $crawler->filter('script[src*=gtag]')->remove();
111 2
        $crawler->filterXPath("//script[text()[contains(.,'gtag')]]")->remove();
112 2
    }
113
114 2
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
115
    {
116 2
        $crawler->filter('h1')
117 2
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
118
119
        $crawler->filter('h2, h3, h4')->each(static function (HtmlPageCrawler $node) {
120 2
            $node->before(
121 2
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
122
            );
123 2
        });
124
125
        $crawler->filterXPath('//p[not(descendant::code)]')->each(static function (HtmlPageCrawler $node) {
126 2
            $node->before(
127 2
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
128
            );
129 2
        });
130 2
    }
131
}
132