Passed
Push — master ( 636760...d1dc62 )
by Guillaume
08:55
created

DocsetBuilder::new()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 12
ccs 0
cts 6
cp 0
crap 6
rs 10
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 $grabber;
15
    protected $packager;
16
    protected $archiver;
17
18
    protected $command;
19
20
21 18
    public function __construct(?Docset $docset = null, ?Command $command = null)
22
    {
23 18
        $this->docset = $docset;
24 18
        $this->command = $command ?? new LaravelCommand();
25
26 18
        $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

26
        $this->newer = new DocsetNewer(/** @scrutinizer ignore-type */ $this->command->argument('doc'));
Loading history...
Bug Best Practice introduced by
The property newer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
28 18
        if ($this->docset) {
29 18
            $this->grabber = new DocsetGrabber($this->docset);
30 18
            $this->packager = new DocsetPackager($this->docset);
31 18
            $this->archiver = new DocsetArchiver($this->docset);
32
        }
33 18
    }
34
35
    public function new()
36
    {
37
        $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

37
        $class = Str::studly(/** @scrutinizer ignore-type */ $this->command->argument('doc'));
Loading history...
38
39
        if (! $class) {
40
            return $this->command->task('  - Never gonna give you up. Generating the Rick Astley Docset class for you', function () {
41
                $this->newer->new();
42
            });
43
        }
44
45
        return $this->command->task("  - Generating $class.php", function () {
46
            $this->newer->new();
47
        });
48
    }
49
50 6
    public function build()
51
    {
52 6
        $this->grab();
53 6
        $this->package();
54 6
        $this->archive();
55 6
    }
56
57 6
    public function grab()
58
    {
59 6
        if (method_exists($this->docset, 'grab')) {
60
            return $this->grabFromDocset();
61
        }
62
63 6
        if ($this->grabber->sitemapExists()) {
64
            return $this->grabFromSitemap();
65
        }
66
67 6
        return $this->grabFromIndex();
68
    }
69
70
    protected function grabFromDocset()
71
    {
72
        return $this->command->task('  - Downloading doc from docset custom instructions', function () {
73
            return $this->docset->grab();
0 ignored issues
show
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

73
            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 null. ( Ignorable by Annotation )

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

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