Completed
Push — master ( 33476c...427ebd )
by Bertrand
09:14
created

InteractionController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A countsInteractionOnPage() 0 3 1
A getInteractionsOnPageByUser() 0 10 2
A handle() 0 15 2
1
<?php
2
3
4
namespace App\Http\Controllers\Api;
5
6
7
use App\Http\Controllers\Controller;
8
use App\Src\UseCases\Domain\Agricultural\Queries\CountInteractionsOnPageQuery;
9
use App\Src\UseCases\Domain\Agricultural\Queries\InteractionsQueryByPageAndUser;
10
use App\Src\UseCases\Domain\Users\Interactions\HandleInteractions;
11
use Illuminate\Http\Request;
12
13
/**
14
 * @group Interaction management
15
 *
16
 * APIs for interaction on pages
17
 */
18
class InteractionController extends Controller
19
{
20
21
    /**
22
     * Add a interaction (follow, unfollow, done, undone, applause, unapplause) of the user authenticated to the page given
23
     * @urlParam pageId integer required The wiki page id Example:1
24
     * @queryParam wiki_session_id string required The wiki session id Example:abc
25
     * @bodyParam interactions string[] required The user's interactions on the page. Example: unapplause, done
26
     */
27
    public function handle($pageId, Request $request,
28
                           HandleInteractions $handleInteractions,
29
                           InteractionsQueryByPageAndUser $interactionsQueryByPageAndUser,
30
                           CountInteractionsOnPageQuery $countInteractionsOnPage
31
    )
32
    {
33
        $interactions = $request->input('interactions');
34
        $doneValue = $request->input('done_value', []);
35
        $handleInteractions->execute($pageId, $interactions, $doneValue);
36
37
        $interaction = $interactionsQueryByPageAndUser->execute($pageId);
38
        $counts = $countInteractionsOnPage->execute($pageId);
39
        return [
40
            'state' => isset($interaction) ? $interaction->toArray() : [],
41
            'counts' => $counts
42
        ];
43
    }
44
45
    /**
46
     * Get the number of interactions for the page given
47
     * @urlParam pageId integer required The wiki page id Example:1
48
     * @queryParam wiki_session_id string required The wiki session id Example:abc
49
     */
50
    public function countsInteractionOnPage($pageId, CountInteractionsOnPageQuery $countInteractionsOnPage):array
51
    {
52
        return $countInteractionsOnPage->execute($pageId);
53
    }
54
55
    /**
56
     * Get the state of interaction for the user authenticated on the page given
57
     * @urlParam pageId integer required The wiki page id Example:1
58
     * @queryParam wiki_session_id string required The wiki session id Example:abc
59
     */
60
    public function getInteractionsOnPageByUser($pageId,
61
                                                InteractionsQueryByPageAndUser $interactionsQueryByPageAndUser,
62
                                                CountInteractionsOnPageQuery $countInteractionsOnPage
63
    )
64
    {
65
        $interaction = $interactionsQueryByPageAndUser->execute($pageId);
66
        $counts = $countInteractionsOnPage->execute($pageId);
67
        return [
68
            'state' => isset($interaction) ? $interaction->toArray() : [],
69
            'counts' => $counts
70
        ];
71
    }
72
}
73