Passed
Branch master (249862)
by Adam
07:51
created

ShowController::getRelated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Controllers\Wiki;
4
5
use Coyote\Http\Factories\CacheFactory;
6
use Coyote\Http\Factories\FlagFactory;
7
use Coyote\Http\Forms\Wiki\CommentForm;
8
use Coyote\Repositories\Criteria\Wiki\DirectAncestor;
9
use Coyote\Repositories\Criteria\Wiki\OnlyWithChildren;
10
use Coyote\Services\Elasticsearch\Builders\Wiki\MoreLikeThisBuilder;
11
use Coyote\Wiki;
12
use Illuminate\Http\Request;
13
14
class ShowController extends BaseController
15
{
16
    use FlagFactory, CacheFactory;
17
18
    /**
19
     * @param Request $request
20
     * @return \Illuminate\View\View
21
     */
22
    public function index(Request $request)
23
    {
24
        /** @var \Coyote\Wiki $wiki */
25
        $wiki = $request->attributes->get('wiki');
26
27
        $author = $wiki->logs()->exists() ? $wiki->logs()->orderBy('id')->first()->user : null;
28
        $wiki->text = $this->getParser()->parse((string) $wiki->text);
29
30
        $parser = app('parser.wiki');
31
        $wiki->load('comments.user');
32
33
        foreach ($wiki->comments as &$comment) {
34
            /** @var \Coyote\Wiki\Comment $comment */
35
            $comment->html = $parser->parse($comment->text);
36
        }
37
38
        $view = $this->view('wiki.' . $wiki->template, [
39
            'wiki' => $wiki,
40
            'author' => $author,
41
            'authors' => $wiki->authors()->get(),
42
            'categories' => $this->wiki->getAllCategories($wiki->wiki_id),
43
            'parents' => $this->parents->slice(1)->reverse(), // we skip current page
44
            'subscribed' => $wiki->subscribers()->forUser($this->userId)->exists(),
45
            'flag' => $this->getGateFactory()->allows('wiki-admin') ? $this->getFlagFactory()->takeForWiki($wiki->id) : '',
46
            'form' => $this->createForm(CommentForm::class, [], [
47
                'url' => route('wiki.comment.save', [$wiki->id])
48
            ]),
49
            'children' => $this->getCatalog($wiki->id)
50
        ]);
51
52
        if (method_exists($this, $wiki->template)) {
53
            $view->with($this->{$wiki->template}($wiki));
54
        }
55
56
        return $view;
57
    }
58
59
    /**
60
     * @param Wiki $wiki
61
     * @return array
62
     */
63
    public function show(Wiki $wiki)
64
    {
65
        return [
66
            'mlt' => $this->getMoreLikeThis($wiki),
67
            'related' => $this->getRelated($wiki->id)
68
        ];
69
    }
70
71
    /**
72
     * @param Wiki $wiki
73
     * @return array
74
     */
75
    public function category(Wiki $wiki)
76
    {
77
        return [
78
            'folders' => $this->getFolders($wiki->id)
79
        ];
80
    }
81
82
    /**
83
     * @param int $parentId
84
     * @return mixed
85
     */
86
    private function getFolders($parentId)
87
    {
88
        $this->wiki->pushCriteria(new OnlyWithChildren());
89
        $this->wiki->pushCriteria(new DirectAncestor($parentId));
90
91
        $result = $this->wiki->children($parentId);
92
        $this->wiki->resetCriteria();
93
94
        return $result;
95
    }
96
97
    /**
98
     * @param int $parentId
99
     * @return mixed
100
     */
101
    private function getCatalog($parentId)
102
    {
103
        return $this->wiki->getCatalog($parentId);
104
    }
105
106
    /**
107
     * @param Wiki $wiki
108
     * @return mixed
109
     */
110
    private function getMoreLikeThis(Wiki $wiki)
111
    {
112
        return $this->getCacheFactory()->remember('wiki:mlt', 60 * 24, function () use ($wiki) {
113
            return $this->wiki->search(new MoreLikeThisBuilder($wiki))->getSource();
0 ignored issues
show
Bug introduced by
The method search() does not exist on Coyote\Repositories\Cont...WikiRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Coyote\Repositories\Cont...WikiRepositoryInterface. ( Ignorable by Annotation )

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

113
            return $this->wiki->/** @scrutinizer ignore-call */ search(new MoreLikeThisBuilder($wiki))->getSource();
Loading history...
114
        });
115
    }
116
117
    /**
118
     * @param $wikiId
119
     * @return \Coyote\Wiki[]
120
     */
121
    private function getRelated($wikiId)
122
    {
123
        return $this->wiki->getRelatedPages($wikiId);
124
    }
125
}
126