Passed
Push — master ( d3fd5d...81f334 )
by Mihail
03:51
created

ActionWallAnswerCount::wallAnswerCount()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 4
nop 1
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
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\Helper\Type\Str;
10
use Ffcms\Core\Network\Request;
11
use Ffcms\Core\Network\Response;
12
13
/**
14
 * Trait ActionWallAnswerCount
15
 * @package Apps\Controller\Api\Profile
16
 * @property Response $response
17
 * @property Request $request
18
 * @method void setJsonHeader()
19
 */
20
trait ActionWallAnswerCount
21
{
22
23
    /**
24
     * Get wall answer's count by post-ids list
25
     * @param string $postIds
26
     * @throws NativeException
27
     * @return string
28
     */
29
    public function wallAnswerCount(string $postIds): ?string
30
    {
31
        // set header
32
        $this->setJsonHeader();
33
        // check query length
34
        if (Any::isEmpty($postIds)) {
35
            throw new NativeException('Wrong input count');
36
        }
37
38
        $list = explode(',', $postIds);
39
        $itemCount = count($list);
40
        // empty or is biggest then limit?
41
        if ($itemCount < 1 || $itemCount > self::ITEM_PER_PAGE) {
42
            throw new NativeException('Wrong input count');
43
        }
44
45
        // prepare response
46
        $response = [];
47
        foreach ($list as $post) {
48
            $response[$post] = WallAnswer::where('post_id', $post)->count();
49
        }
50
51
        // display json data
52
        return json_encode([
53
            'status' => 1,
54
            'data' => $response
55
        ]);
56
    }
57
}
58