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

ActionWallAnswerCount   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 14
dl 0
loc 35
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A wallAnswerCount() 0 26 5
1
<?php
2
3
4
namespace Apps\Controller\Api\Profile;
5
6
use Apps\ActiveRecord\WallAnswer;
7
use Ffcms\Core\Exception\NativeException;
8
use Ffcms\Core\Helper\Type\Any;
9
use Ffcms\Core\Network\Request;
10
use Ffcms\Core\Network\Response;
11
12
/**
13
 * Trait ActionWallAnswerCount
14
 * @package Apps\Controller\Api\Profile
15
 * @property Response $response
16
 * @property Request $request
17
 * @method void setJsonHeader()
18
 */
19
trait ActionWallAnswerCount
20
{
21
22
    /**
23
     * Get wall answer's count by post-ids list
24
     * @param string $postIds
25
     * @throws NativeException
26
     * @return string
27
     */
28
    public function wallAnswerCount(string $postIds): ?string
29
    {
30
        // set header
31
        $this->setJsonHeader();
32
        // check query length
33
        if (Any::isEmpty($postIds)) {
34
            throw new NativeException('Wrong input count');
35
        }
36
37
        $list = explode(',', $postIds);
38
        $itemCount = count($list);
39
        // empty or is biggest then limit?
40
        if ($itemCount < 1 || $itemCount > self::ITEM_PER_PAGE) {
0 ignored issues
show
Bug introduced by zenn
The constant Apps\Controller\Api\Prof...werCount::ITEM_PER_PAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
41
            throw new NativeException('Wrong input count');
42
        }
43
44
        // prepare response
45
        $response = [];
46
        foreach ($list as $post) {
47
            $response[$post] = WallAnswer::where('post_id', $post)->count();
48
        }
49
50
        // display json data
51
        return json_encode([
52
            'status' => 1,
53
            'data' => $response
54
        ]);
55
    }
56
}
57