Passed
Push — master ( 77f2dd...e348f5 )
by Adam
12:12
created

FixPagesCommand::addMissingTopics()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 24
rs 9.7998
c 1
b 0
f 0
1
<?php
2
3
namespace Coyote\Console\Commands;
4
5
use Coyote\Microblog;
6
use Coyote\Page;
7
use Coyote\Services\UrlBuilder;
8
use Coyote\Topic;
9
use Illuminate\Console\Command;
10
11
class FixPagesCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'fix:pages';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Fix page';
26
27
    public function handle()
28
    {
29
        $this->fixTopics();
30
        $this->fixMicroblogs();
31
        $this->addMissingTopics();
32
    }
33
34
    private function fixTopics()
35
    {
36
        $count = Topic::count();
37
38
        $bar = $this->output->createProgressBar($count);
0 ignored issues
show
Bug introduced by
It seems like $count can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $max of Symfony\Component\Consol...le::createProgressBar() does only seem to accept integer, 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
        $bar = $this->output->createProgressBar(/** @scrutinizer ignore-type */ $count);
Loading history...
39
        $bar->start();
40
41
        Topic::chunk(1000, function ($topics) use ($bar) {
42
            foreach ($topics as $topic) {
43
                Page::where('content_id', $topic->id)->where('content_type', Topic::class)->update([
44
                    'title'          => $topic->title,
45
                    'tags'           => $topic->tags->pluck('name'),
46
                    'path'           => UrlBuilder::topic($topic),
47
                    'allow_sitemap'  => !$topic->forum->access()->exists()
48
                ]);
49
50
                $bar->advance();
51
            }
52
        });
53
54
        $bar->finish();
55
    }
56
57
    private function fixMicroblogs()
58
    {
59
        $count = Microblog::whereNull('parent_id')->count();
60
61
        $bar = $this->output->createProgressBar($count);
0 ignored issues
show
Bug introduced by
It seems like $count can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Builder; however, parameter $max of Symfony\Component\Consol...le::createProgressBar() does only seem to accept integer, 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

61
        $bar = $this->output->createProgressBar(/** @scrutinizer ignore-type */ $count);
Loading history...
62
        $bar->start();
63
64
        Microblog::whereNull('parent_id')->chunkById(1000, function ($microblogs) use ($bar) {
65
            foreach ($microblogs as $microblog) {
66
                $microblog->page()->updateOrCreate([
67
                    'content_id'    => $microblog->id,
68
                    'content_type'  => Microblog::class,
69
                ], [
70
                    'title'         => excerpt($microblog->html, 28),
71
                    'path'          => UrlBuilder::microblog($microblog),
72
                    'tags'          => $microblog->tags->pluck('name')
73
                ]);
74
75
                $bar->advance();
76
            }
77
        });
78
79
        $bar->finish();
80
    }
81
82
    private function addMissingTopics()
83
    {
84
        $topics = Topic::whereRaw("topics.id not in (select content_id from pages where content_type = 'Coyote\Topic')")->get();
85
86
        $count = count($topics);
87
88
        $bar = $this->output->createProgressBar($count);
89
        $bar->start();
90
91
        foreach ($topics as $topic) {
92
            $topic->page()->updateOrCreate([
93
                'content_id'     => $topic->id,
94
                'content_type'   => Topic::class
95
            ], [
96
                'title'          => $topic->title,
97
                'tags'           => $topic->tags->pluck('name'),
98
                'path'           => UrlBuilder::topic($topic),
99
                'allow_sitemap'  => !$topic->forum->access()->exists()
100
            ]);
101
102
            $bar->advance();
103
        }
104
105
        $bar->finish();
106
    }
107
}
108