Passed
Push — master ( bcbcca...8fb468 )
by Guillaume
08:29
created

DocsetPackager::populateSQLiteIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Godbout\DashDocsetBuilder\Services;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Support\Facades\File;
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Support\Facades\Artisan;
9
use Illuminate\Support\Facades\Storage;
10
use Godbout\DashDocsetBuilder\Contracts\Docset;
11
12
final class DocsetPackager
13
{
14
    protected $docset;
15
16
17 18
    public function __construct(Docset $docset)
18
    {
19 18
        $this->docset = $docset;
20 18
    }
21
22 12
    public function removePreviousDocsetFile()
23
    {
24 12
        return Storage::deleteDirectory(
25 12
            $this->docset->file()
26
        );
27
    }
28
29 12
    public function createDocsetFile()
30
    {
31 12
        return Storage::makeDirectory(
32 12
            $this->docset->innerDirectory()
33
        );
34
    }
35
36 12
    public function copyDocFiles()
37
    {
38 12
        return File::copyDirectory(
39 12
            "storage/{$this->docset->downloadedDirectory()}",
40 12
            "storage/{$this->docset->innerDirectory()}"
41
        );
42
    }
43
44 12
    public function createInfoPlist()
45
    {
46
        $infoPlist = <<<EOT
47 4
<?xml version="1.0" encoding="UTF-8"?>
48
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
49
<plist version="1.0">
50
<dict>
51
    <key>CFBundleIdentifier</key>
52 12
    <string>{$this->docset->code()}</string>
53
    <key>CFBundleName</key>
54 12
    <string>{$this->docset->name()}</string>
55
    <key>DocSetPlatformFamily</key>
56 12
    <string>{$this->docset->code()}</string>
57
    <key>dashIndexFilePath</key>
58 12
    <string>{$this->docset->index()}</string>
59
    <key>DashDocSetFallbackURL</key>
60
    <string>https://</string>
61
    <key>DashDocSetPlayURL</key>
62 12
    <string>{$this->docset->playground()}</string>
63
    <key>isJavaScriptEnabled</key>
64
    <true/>
65
    <key>isDashDocset</key>
66
    <true/>
67
    <key>DashDocSetFamily</key>
68
    <string>dashtoc</string>
69
    <key>DashDocSetDeclaredInStyle</key>
70
    <string>originalName</string>
71
</dict>
72
</plist>
73
EOT;
74
75 12
        return Storage::put(
76 12
            $this->docset->infoPlistFile(),
77 4
            $infoPlist
78
        );
79
    }
80
81 12
    public function createAndPopulateSQLiteIndex()
82
    {
83 12
        Config::set(
84 12
            'database.connections.sqlite.database',
85 12
            "storage/{$this->docset->databaseFile()}"
86
        );
87
88 12
        $this->createSQLiteIndex();
89
90 12
        $this->populateSQLiteIndex();
91 12
    }
92
93 12
    protected function createSQLiteIndex()
94
    {
95 12
        Storage::put(
96 12
            $this->docset->databaseFile(),
97 12
            null
98
        );
99
100 12
        Artisan::call('migrate');
101 12
    }
102
103 12
    protected function populateSQLiteIndex()
104
    {
105 12
        $entries = $this->docsetEntries();
106
107
        $entries->each(static function ($entry) {
108 12
            DB::table('searchIndex')->insert([
109 12
                'name' => $entry['name'],
110 12
                'type' => $entry['type'],
111 12
                'path' => $entry['path'],
112
            ]);
113 12
        });
114 12
    }
115
116 12
    protected function docsetEntries()
117
    {
118 12
        $files = $this->docset->htmlFiles();
119
120 12
        $entries = collect();
121
122
        $files->each(function ($file) use (&$entries) {
123
            $entries = $entries
124 12
                ->merge($this->docset->entries($file))
125
                ->unique(function ($entry) {
126 12
                    return $entry['name'] . $entry['type'];
127 12
                });
128 12
        });
129
130 12
        return $entries;
131
    }
132
133 12
    public function formatDocFiles()
134
    {
135 12
        $files = $this->docset->htmlFiles();
136
137
        $files->each(function ($file) {
138 12
            $formattedContent = $this->docset->format(Storage::get($file));
139 12
            Storage::put($file, $formattedContent);
140 12
        });
141 12
    }
142
143 12
    public function copyIcons()
144
    {
145 12
        if ($this->docset->icon16()) {
146 12
            Storage::copy(
147 12
                "{$this->docset->downloadedDirectory()}/{$this->docset->icon16()}",
148 12
                "{$this->docset->file()}/icon.png"
149
            );
150
        }
151
152 12
        if ($this->docset->icon32()) {
153 12
            Storage::copy(
154 12
                "{$this->docset->downloadedDirectory()}/{$this->docset->icon32()}",
155 12
                "{$this->docset->file()}/[email protected]"
156
            );
157
        }
158 12
    }
159
}
160