Passed
Push — master ( d1dc62...a1972e )
by Guillaume
08:28
created

DocsetBuilder::grabFromDocset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Godbout\DashDocsetBuilder\Services;
4
5
use Godbout\DashDocsetBuilder\Contracts\Docset;
6
use Illuminate\Console\Command as LaravelCommand;
7
use Illuminate\Support\Str;
8
use LaravelZero\Framework\Commands\Command;
9
10
class DocsetBuilder
11
{
12
    protected $docset;
13
14
    protected $newer;
15
    protected $grabber;
16
    protected $packager;
17
    protected $archiver;
18
19
    protected $command;
20
21
22 36
    public function __construct(?Docset $docset = null, ?Command $command = null)
23
    {
24 36
        $this->docset = $docset;
25 36
        $this->command = $command ?? new LaravelCommand();
26
27 36
        $this->newer = new DocsetNewer($this->command->argument('doc'));
0 ignored issues
show
Bug introduced by
It seems like $this->command->argument('doc') can also be of type string[]; however, parameter $docsetName of Godbout\DashDocsetBuilde...setNewer::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        $this->newer = new DocsetNewer(/** @scrutinizer ignore-type */ $this->command->argument('doc'));
Loading history...
28
29 36
        if ($this->docset) {
30 18
            $this->grabber = new DocsetGrabber($this->docset);
31 18
            $this->packager = new DocsetPackager($this->docset);
32 18
            $this->archiver = new DocsetArchiver($this->docset);
33
        }
34 36
    }
35
36 18
    public function new()
37
    {
38 18
        $class = Str::studly($this->command->argument('doc'));
0 ignored issues
show
Bug introduced by
It seems like $this->command->argument('doc') can also be of type string[]; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $class = Str::studly(/** @scrutinizer ignore-type */ $this->command->argument('doc'));
Loading history...
39
40 18
        if (! $class) {
41
            return $this->command->task('  - Never gonna give you up. Generating app/Docsets/RickAstley.php', function () {
42 12
                $this->newer->new();
43 12
            });
44
        }
45
46
        return $this->command->task("  - Generating app/Docsets/$class.php", function () {
47 12
            $this->newer->new();
48 12
        });
49
    }
50
51 6
    public function build()
52
    {
53 6
        $this->grab();
54 6
        $this->package();
55 6
        $this->archive();
56 6
    }
57
58 6
    public function grab()
59
    {
60 6
        if (method_exists($this->docset, 'grab')) {
61
            return $this->grabFromDocset();
62
        }
63
64 6
        if ($this->grabber->sitemapExists()) {
65
            return $this->grabFromSitemap();
66
        }
67
68 6
        return $this->grabFromIndex();
69
    }
70
71
    protected function grabFromDocset()
72
    {
73
        return $this->command->task('  - Downloading doc from docset custom instructions', function () {
74
            return $this->docset->grab();
0 ignored issues
show
Bug introduced by
The method grab() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            return $this->docset->/** @scrutinizer ignore-call */ grab();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method grab() does not exist on Godbout\DashDocsetBuilder\Contracts\Docset. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            return $this->docset->/** @scrutinizer ignore-call */ grab();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
        });
76
    }
77
78
    protected function grabFromSitemap()
79
    {
80
        return $this->command->task('  - Downloading doc from sitemap', function () {
81
            return $this->grabber->grabFromSitemap();
82
        });
83
    }
84
85 6
    protected function grabFromIndex()
86
    {
87
        return $this->command->task('  - Downloading doc from index', function () {
88 6
            return $this->grabber->grabFromIndex();
89 6
        });
90
    }
91
92 12
    public function package()
93
    {
94
        $this->command->task('  - Remove previous .docset', function () {
95 12
            $this->packager->removePreviousDocsetFile();
96 12
        });
97
98
        $this->command->task('  - Create new .docset', function () {
99 12
            return $this->packager->createDocsetFile();
100 12
        });
101
102
        $this->command->task('  - Copy original doc files', function () {
103 12
            return $this->packager->copyDocFiles();
104 12
        });
105
106
        $this->command->task('  - Create Info.plist', function () {
107 12
            return $this->packager->createInfoPlist();
108 12
        });
109
110
        $this->command->task('  - Populate SQLiteIndex', function () {
111 12
            return $this->packager->createAndPopulateSQLiteIndex();
112 12
        });
113
114
        $this->command->task('  - Format doc files for Dash', function () {
115 12
            return $this->packager->formatDocFiles();
116 12
        });
117
118
        $this->command->task('  - Copy icons', function () {
119 12
            $this->packager->copyIcons();
120 12
        });
121 12
    }
122
123 12
    public function archive()
124
    {
125
        $this->command->task('  - Archiving package', function () {
126 12
            return $this->archiver->archive();
127 12
        });
128 12
    }
129
}
130