Passed
Push — master ( 636076...0895b6 )
by Guillaume
17:28 queued 01:29
created

Ploi   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 119
ccs 49
cts 56
cp 0.875
rs 10
c 0
b 0
f 0
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 12 1
A removePreviousAndNextNavigation() 0 3 1
A entries() 0 8 1
A removeUnwantedJavaScript() 0 5 1
A updateTopPadding() 0 4 1
A removeHeader() 0 3 1
A guideEntries() 0 15 2
A removeSidebar() 0 3 1
A insertDashTableOfContents() 0 14 1
A grab() 0 19 1
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 12
    public function entries(string $file): Collection
46
    {
47 12
        $crawler = HtmlPageCrawler::create(Storage::get($file));
48
49 12
        $entries = collect();
50 12
        $entries = $entries->merge($this->guideEntries($crawler, $file));
51
52 12
        return $entries;
53
    }
54
55 12
    protected function guideEntries(HtmlPageCrawler $crawler, string $file)
56
    {
57 12
        $entries = collect();
58
59 12
        if (Str::contains($file, "{$this->url()}/index.html")) {
60
            $crawler->filter('aside a.ml-2')->each(function (HtmlPageCrawler $node) use ($entries) {
61 12
                $entries->push([
62 12
                    'name' => trim($node->text()),
63 12
                    'type' => 'Guide',
64 12
                    'path' => $this->url() . '/' . $node->attr('href'),
65
                ]);
66 12
            });
67
        }
68
69 12
        return $entries;
70
    }
71
72 12
    public function format(string $html): string
73
    {
74 12
        $crawler = HtmlPageCrawler::create($html);
75
76 12
        $this->removeHeader($crawler);
77 12
        $this->removeSidebar($crawler);
78 12
        $this->removePreviousAndNextNavigation($crawler);
79 12
        $this->updateTopPadding($crawler);
80 12
        $this->removeUnwantedJavaScript($crawler);
81 12
        $this->insertDashTableOfContents($crawler);
82
83 12
        return $crawler->saveHTML();
84
    }
85
86 12
    protected function removeHeader(HtmlPageCrawler $crawler)
87
    {
88 12
        $crawler->filter('header')->remove();
89 12
    }
90
91 12
    protected function removeSidebar(HtmlPageCrawler $crawler)
92
    {
93 12
        $crawler->filter('aside')->remove();
94 12
    }
95
96 12
    protected function removePreviousAndNextNavigation(HtmlPageCrawler $crawler)
97
    {
98 12
        $crawler->filter('#previous-and-next')->remove();
99 12
    }
100
101 12
    protected function updateTopPadding(HtmlPageCrawler $crawler)
102
    {
103 12
        $crawler->filter('main')
104 12
            ->addClass('md:pt-12')
105
        ;
106 12
    }
107
108 12
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
109
    {
110 12
        $crawler->filter("link[href*='analytics.dennissmink.com']")->remove();
111 12
        $crawler->filterXPath("//script[text()[contains(.,'analytics.dennissmink.com')]]")->remove();
112 12
        $crawler->filter('noscript')->remove();
113 12
    }
114
115 12
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
116
    {
117 12
        $crawler->filter('h1')
118 12
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
119
120
        $crawler->filter('h2, h3, h4')->each(static function (HtmlPageCrawler $node) {
121 12
            $node->before(
122 12
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
123
            );
124 12
        });
125
126
        $crawler->filterXPath('//p[not(descendant::code)]')->each(static function (HtmlPageCrawler $node) {
127 12
            $node->before(
128 12
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
129
            );
130 12
        });
131 12
    }
132
}
133