Chartjs::entries()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 17
ccs 0
cts 10
cp 0
rs 9.9666
cc 2
nc 2
nop 1
crap 6
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 Chartjs extends BaseDocset
12
{
13
    public const CODE = 'chartjs';
14
    public const NAME = 'Chart.js';
15
    public const URL = 'www.chartjs.org';
16
    public const INDEX = 'index.html';
17
    public const PLAYGROUND = 'https://codepen.io/lucusc/pen/pgQRdd';
18
    public const ICON_16 = '../../icons/icon.png';
19
    public const ICON_32 = '../../icons/[email protected]';
20
    public const EXTERNAL_DOMAINS = [
21
        'use.typekit.net',
22
    ];
23
24
25
    public function grab(): bool
26
    {
27
        $toIgnore = implode('|', [
0 ignored issues
show
Unused Code introduced by
The assignment to $toIgnore is dead and can be removed.
Loading history...
28
            '/cdn-cgi',
29
            '/docs/2.9.3',
30
            '/docs/master',
31
            '/samples/master'
32
        ]);
33
34
        $toGet = implode('|', [
35
            '\.css',
36
            '\.ico',
37
            '\.js',
38
            '\.svg',
39
            '/docs',
40
            '/samples'
41
        ]);
42
43
        system(
44
            "echo; wget www.chartjs.org \
45
                --mirror \
46
                --trust-server-names \
47
                --accept-regex='{$toGet}' \
48
                --ignore-case \
49
                --page-requisites \
50
                --adjust-extension \
51
                --convert-links \
52
                --span-hosts \
53
                --domains={$this->externalDomains()} \
54
                --directory-prefix=storage/{$this->downloadedDirectory()} \
55
                -e robots=off \
56
                --quiet \
57
                --show-progress",
58
            $result
59
        );
60
61
        return $result === 0;
62
    }
63
64
    public function entries(string $file): Collection
65
    {
66
        $crawler = HtmlPageCrawler::create(Storage::get($file));
67
68
        $entries = collect();
69
70
        if (Str::contains($file, "{$this->url()}/docs/latest/index.html")) {
71
            $crawler->filter('.summary a')->each(function (HtmlPageCrawler $node) use ($entries) {
72
                $entries->push([
73
                    'name' => $node->text(),
74
                    'type' => 'Guide',
75
                    'path' => $this->url() . '/docs/latest/' . $node->attr('href'),
76
                ]);
77
            });
78
        }
79
80
        return $entries;
81
    }
82
83
    public function format(string $file): string
84
    {
85
        $crawler = HtmlPageCrawler::create(Storage::get($file));
86
87
        $this->removeTopHeader($crawler);
88
        $this->removeLeftSidebar($crawler);
89
        $this->makeContentFullWidth($crawler);
90
91
        // $this->removeUnwantedJavaScript($crawler);
92
        // $this->insertDashTableOfContents($crawler);
93
94
        return $crawler->saveHTML();
95
    }
96
97
    protected function removeTopHeader(HtmlPageCrawler $crawler)
98
    {
99
        $crawler->filter('.book-header')->remove();
100
    }
101
102
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
103
    {
104
        $crawler->filter('.book-summary')->remove();
105
    }
106
107
    protected function makeContentFullWidth(HtmlPageCrawler $crawler)
108
    {
109
        $crawler->filter('.book')->removeClass('with-summary');
110
    }
111
}
112