Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Widgets/Front/Newcomment/Newcomment.php (1 issue)

Severity
1
<?php
2
3
namespace Widgets\Front\Newcomment;
4
5
use Apps\Model\Api\Comments\EntityCommentData;
6
use Apps\ActiveRecord\App as AppRecord;
7
use Ffcms\Core\App;
8
use Extend\Core\Arch\FrontWidget as AbstractWidget;
9
use Ffcms\Core\Traits\ClassTools;
10
use Apps\ActiveRecord\CommentPost;
11
12
/**
13
 * Class Newcomment. New comments widget. Show new comments in system.
14
 * @package Widgets\Front\Newcomment
15
 */
16
class Newcomment extends AbstractWidget
17
{
18
    use ClassTools;
19
20
    public $snippet;
21
    public $count;
22
    public $cache;
23
    public $lang;
24
25
    private $_cacheName;
26
27
    /**
28
     * Set default configs if not passed
29
     * {@inheritDoc}
30
     * @see \Ffcms\Core\Arch\Widget::init()
31
     */
32
    public function init(): void
33
    {
34
        $cfg = $this->getConfigs();
35
        if (!$this->snippet) {
36
            $this->snippet = $cfg['snippet'];
37
        }
38
39
        if (!$this->count) {
40
            $this->count = (int)$cfg['count'];
41
        }
42
43
        if (!$this->cache) {
44
            $this->cache = (int)$cfg['cache'];
45
        }
46
47
        if (!$this->lang) {
48
            $this->lang = App::$Request->getLanguage();
49
        }
50
51
52
        $this->_cacheName = 'widget.newcomment.' . $this->createStringClassSnapshotHash();
53
    }
54
55
    /**
56
     * Show latest comments
57
     * @return string
58
     * @throws \Ffcms\Core\Exception\JsonException
59
     * @throws \Psr\Cache\InvalidArgumentException
60
     */
61
    public function display(): ?string
62
    {
63
        // work with cache and make query
64
        $records = null;
65
        if ((int)$this->cache > 0) {
66
            // process caching data
67
            $cache = App::$Cache->getItem($this->_cacheName);
68
            if (!$cache->isHit()) {
69
                $cache->set($this->makeQuery());
70
                $cache->expiresAfter((int)$this->cache);
71
                App::$Cache->save($cache);
72
            }
73
74
            $records = $cache->get();
75
        } else {
76
            $records = $this->makeQuery();
77
        }
78
79
        // check if records is found
80
        if (!$records) {
81
            return __('Comments not yet found');
82
        }
83
84
        $commentEntity = null;
85
        foreach ($records as $record) {
86
            $commentEntity[] = (new EntityCommentData($record, false))->make();
87
        }
88
89
        // render view
90
        return App::$View->render('widgets/newcomment/default', [
91
            'comments' => $commentEntity,
92
            'snippet' => $this->snippet
93
        ]);
94
    }
95
96
    /**
97
     * Make database query and return results
98
     * @return object
99
     */
100
    private function makeQuery()
101
    {
102
        $records = CommentPost::with(['user', 'user.profile', 'user.role'])
103
            ->where('moderate', false);
104
105
        $cfg = AppRecord::getConfigs('widget', 'Comments');
106
        if ((bool)$cfg['onlyLocale']) {
107
            $records->where('lang', $this->lang);
108
        }
109
110
        if (!$records || $records->count() < 1) {
0 ignored issues
show
$records is of type Illuminate\Database\Eloquent\Builder, thus it always evaluated to true.
Loading history...
111
            return null;
112
        }
113
114
        return $records->orderBy('id', 'DESC')
115
        ->take($this->count)
116
        ->get();
117
    }
118
}
119