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

InteractionPageRepositorySql::save()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 17
rs 9.6111
1
<?php
2
3
4
namespace App\Src\UseCases\Infra\Sql;
5
6
7
use App\Src\UseCases\Domain\Agricultural\Model\CanInteract;
8
use App\Src\UseCases\Domain\Agricultural\Model\Interaction;
9
use App\Src\UseCases\Domain\Ports\InteractionRepository;
10
use App\Src\UseCases\Infra\Sql\Model\InteractionModel;
11
use App\User;
12
13
class InteractionPageRepositorySql implements InteractionRepository
14
{
15
    public function save(CanInteract $canInteract, Interaction $interaction)
16
    {
17
        $user = User::query()->where('uuid', $canInteract->identifier())->first();
18
19
        $interactionModel = InteractionModel::query()
20
            ->where($canInteract->key(), isset($user->id) ? $user->id : $canInteract->identifier())
21
            ->where('page_id', $interaction->pageId())
22
            ->first();
23
24
        if($interactionModel === null) {
25
            $interactionModel = new InteractionModel();
26
            $interactionModel->{$canInteract->key()} = isset($user->id) ? $user->id : $canInteract->identifier();
27
        }
28
29
        $interactionModel->fill($interaction->toArray());
30
        $interactionModel->start_done_at = isset($interaction->toArray()['value']['start_at']) ? $interaction->toArray()['value']['start_at'] : null;
31
        $interactionModel->save();
32
    }
33
34
    public function getByInteractUser(CanInteract $canInteract, int $pageId): ?Interaction
35
    {
36
        if($canInteract->key() == 'user_id') {
37
            $user = User::query()->where('uuid', $canInteract->identifier())->first();
38
        }
39
40
        $interactionModel = InteractionModel::query()
41
            ->where($canInteract->key(), isset($user->id) ? $user->id : $canInteract->identifier())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user does not seem to be defined for all execution paths leading up to this point.
Loading history...
42
            ->where('page_id', $pageId)
43
            ->first();
44
45
        if(!isset($interactionModel)){
46
            return null;
47
        }
48
49
        $value = $interactionModel->value ?? [];
50
        return new Interaction($pageId, $interactionModel->follow, $interactionModel->applause, $interactionModel->done, $value);
51
    }
52
53
    public function transfer(CanInteract $anonymous, CanInteract $registered)
54
    {
55
        $user = User::query()->where('uuid', $registered->identifier())->first();
56
57
        InteractionModel::query()->where($anonymous->key(), $anonymous->identifier())
58
            ->update([
59
                $anonymous->key() => null,
60
                $registered->key() => $user->id
61
            ]);
62
    }
63
64
    public function getCountInteractionsOnPage(int $pageId):array
65
    {
66
        $follow = InteractionModel::query()->where('page_id', $pageId)->where('follow', true)->count();
67
        $done = InteractionModel::query()->where('page_id', $pageId)->where('done', true)->count();
68
        $applause = InteractionModel::query()->where('page_id', $pageId)->where('applause', true)->count();
69
70
        return ['follow' => $follow, 'done' => $done, 'applause' => $applause];
71
    }
72
73
74
}
75