Ploi   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 88.52%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 55
dl 0
loc 132
ccs 54
cts 61
cp 0.8852
rs 10
c 1
b 0
f 1
wmc 12

11 Methods

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