Passed
Push — master ( 6042d7...377078 )
by Mihail
04:38
created

Api::actionNews()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 29
rs 8.8571
1
<?php
2
3
namespace Apps\Controller\Api;
4
5
6
use Extend\Core\Arch\ApiController;
7
use Apps\ActiveRecord\Content as ContentRecord;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Helper\Date;
10
use Ffcms\Core\Helper\Text;
11
12
/**
13
 * Class Api. Ffcms official website public API
14
 * @package Apps\Controller\Api
15
 */
16
class Api extends ApiController
17
{
18
    private $newsCategories = [2, 4];
19
20
    /**
21
     * Show ffcms latest 10 news from special category as json data
22
     * @return string
23
     */
24
    public function actionNews()
25
    {
26
        $this->setJsonHeader();
27
        $data = [];
28
        if (App::$Cache->get('api.news.list.'.$this->lang) !== null) {
29
            $data = App::$Cache->get('api.news.list.'.$this->lang);
30
        } else {
31
            $records = ContentRecord::select(['contents.*', 'content_categories.path as cpath', 'content_categories.title as ctitle'])
0 ignored issues
show
Bug introduced by
The method select() does not exist on Apps\ActiveRecord\Content. Did you maybe mean addSelectsToQuery()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
32
                ->whereIn('contents.category_id', $this->newsCategories)
33
                ->join('content_categories', 'content_categories.id', '=', 'contents.category_id', 'left outer')
34
                ->orderBy('contents.created_at', 'DESC')
35
                ->take(10)->get()->toArray();
36
37
            foreach ($records as $item) {
38
                $data[] = [
39
                    'title' => App::$Translate->getLocaleText($item['title']),
40
                    'date' => Date::humanize($item['created_at']),
41
                    'url' => 'https://ffcms.org/' . $this->request->getLanguage() . '/content/read/' . $item['cpath'] . '/' . $item['path'],
42
                    'snippet' => Text::snippet(App::$Translate->getLocaleText($item['text'], $this->request->getLanguage()))
43
                ];
44
            }
45
            App::$Cache->set('api.news.list.'.$this->lang, $data, 3600);
0 ignored issues
show
Documentation introduced by
$data is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
        }
47
48
        return json_encode([
49
            'status' => 1,
50
            'data' => $data
51
        ]);
52
    }
53
}