InteractionPageRepositorySql   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 104
c 1
b 0
f 0
dl 0
loc 174
rs 10
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A transfer() 0 8 1
A save() 0 17 2
A getByInteractUser() 0 17 3
A getDoneByUser() 0 16 2
A getCountInteractionsOnPage() 0 23 1
A getFollowersPage() 0 45 3
A getInteractionsByUser() 0 22 5
1
<?php
2
3
4
namespace App\Src\UseCases\Infra\Sql;
5
6
7
use App\Src\UseCases\Domain\Context\Dto\FollowerDto;
8
use App\Src\UseCases\Domain\Context\Dto\PractiseVo;
9
use App\Src\UseCases\Domain\Context\Model\CanInteract;
10
use App\Src\UseCases\Domain\Context\Model\Interaction;
11
use App\Src\UseCases\Domain\Ports\InteractionRepository;
12
use App\Src\UseCases\Infra\Sql\Model\CharacteristicsModel;
13
use App\Src\UseCases\Infra\Sql\Model\InteractionModel;
14
use App\Src\UseCases\Infra\Sql\Model\PageModel;
15
use App\User;
16
use Illuminate\Contracts\Pagination\Paginator;
17
18
class InteractionPageRepositorySql implements InteractionRepository
19
{
20
    public function save(CanInteract $canInteract, Interaction $interaction)
21
    {
22
        $user = User::query()->where('uuid', $canInteract->identifier())->first();
23
24
        $interactionModel = InteractionModel::query()
25
            ->where($canInteract->key(), $user->id ?? $canInteract->identifier())
26
            ->where('page_id', $interaction->pageId())
27
            ->first();
28
29
        if($interactionModel === null) {
30
            $interactionModel = new InteractionModel();
31
            $interactionModel->{$canInteract->key()} = $user->id ?? $canInteract->identifier();
32
        }
33
34
        $interactionModel->fill($interaction->toArray());
35
        $interactionModel->start_done_at = $interaction->toArray()['value']['start_at'] ?? null;
36
        $interactionModel->save();
37
    }
38
39
    public function getByInteractUser(CanInteract $canInteract, int $pageId): ?Interaction
40
    {
41
        if($canInteract->key() == 'user_id') {
42
            $user = User::query()->where('uuid', $canInteract->identifier())->first();
43
        }
44
45
        $interactionModel = InteractionModel::query()
46
            ->where($canInteract->key(), $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...
47
            ->where('page_id', $pageId)
48
            ->first();
49
50
        if(!isset($interactionModel)){
51
            return null;
52
        }
53
54
        $value = $interactionModel->value ?? [];
55
        return new Interaction($pageId, $interactionModel->follow, $interactionModel->applause, $interactionModel->done, $value);
56
    }
57
58
    public function transfer(CanInteract $anonymous, CanInteract $registered)
59
    {
60
        $user = User::query()->where('uuid', $registered->identifier())->first();
61
62
        InteractionModel::query()->where($anonymous->key(), $anonymous->identifier())
63
            ->update([
64
                $anonymous->key() => null,
65
                $registered->key() => $user->id
66
            ]);
67
    }
68
69
    public function getCountInteractionsOnPage(int $pageId):array
70
    {
71
        $applause = InteractionModel::query()
72
            ->where('page_id', $pageId)
73
            ->where('applause', true)
74
            ->count();
75
76
        // For follows and done, don't include Neayi people - we tend to spoil the results with our faces
77
        $follow = InteractionModel::query()
78
            ->where('page_id', $pageId)
79
            ->where('follow', true)
80
            ->join('users', 'users.id', 'interactions.user_id')
81
            ->where('users.email', 'NOT LIKE', '%@neayi.com')
82
            ->count();
83
84
        $done = InteractionModel::query()
85
            ->where('page_id', $pageId)
86
            ->where('done', true)
87
            ->join('users', 'users.id', 'interactions.user_id')
88
            ->where('users.email', 'NOT LIKE', '%@neayi.com')
89
            ->count();
90
91
        return ['follow' => $follow, 'done' => $done, 'applause' => $applause];
92
    }
93
94
    public function getInteractionsByUser(string $userId): array
95
    {
96
        $user = User::query()->where('uuid', $userId)->first();
97
        $interactionsModel = InteractionModel::query()
98
            ->where('user_id', $user->id)
99
            ->orderBy('updated_at', 'desc')
100
            ->get();
101
102
        foreach($interactionsModel as $interaction) {
103
            $page = PageModel::query()->where('page_id', $interaction->page_id)->first();
104
            if(!isset($page)){
105
                continue;
106
            }
107
            $applause = InteractionModel::query()->where('page_id', $interaction->page_id)->where('applause', true)->count();
108
            if ($interaction->follow) {
109
                $interactions['follow'][] = array_merge($page->toArray(), ['applause' => $applause]);
110
            }
111
            if ($interaction->applause) {
112
                $interactions['applause'][] = array_merge($page->toArray(), ['applause' => $applause]);
113
            }
114
        }
115
        return $interactions ?? [];
116
    }
117
118
119
    public function getDoneByUser(string $userId): array
120
    {
121
        $user = User::query()->where('uuid', $userId)->first();
122
        $records = InteractionModel::query()
123
            ->with('page')
124
            ->where('user_id', $user->id)
125
            ->where('done', true)
126
            ->get();
127
        foreach ($records as $record){
128
            $practises[] = new PractiseVo(
129
                $record->page_id,
130
                $record->page->title ?? '',
131
                $record->start_done_at ?? null
0 ignored issues
show
Bug introduced by
It seems like $record->start_done_at ?? null can also be of type string; however, parameter $doneAt of App\Src\UseCases\Domain\...actiseVo::__construct() does only seem to accept DateTime|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
                /** @scrutinizer ignore-type */ $record->start_done_at ?? null
Loading history...
132
            );
133
        }
134
        return $practises ?? [];
135
    }
136
137
138
    /**
139
     * @param int $pageId
140
     * @param string $type
141
     * @param string|null $cp
142
     * @param string|null $characteristicId
143
     * @param string|null $characteristicIdCroppingSystem
144
     * @return Paginator
145
     *
146
    */
147
    public function getFollowersPage(int $pageId, string $type = 'follow', ?string $departmentNumber = null, ?string $characteristicId = null, ?string $characteristicIdCroppingSystem = null): Paginator
148
    {
149
        return  InteractionModel::query()
150
            ->with('user.context')
151
            ->where(function ($query) use ($type){
152
                $query->when($type === 'follow', function ($query) {
153
                    $query->where('follow', true);
154
                    $query->orWhere('done', true);
155
                })
156
                ->when($type === 'do', function ($query) {
157
                    $query->where('done', true);
158
                });
159
            })
160
            ->when($characteristicId !== null, function ($query) use($characteristicId) {
161
                $characteristic = CharacteristicsModel::query()->where('uuid', $characteristicId)->first();
162
                if(!isset($characteristic)){
163
                    return;
164
                }
165
                $query->whereRaw(
166
                    'exists(SELECT * FROM user_characteristics where characteristic_id = ? AND user_characteristics.user_id = interactions.user_id)',
167
                    $characteristic->id
168
                );
169
            })
170
            ->when($characteristicIdCroppingSystem !== null, function ($query) use($characteristicIdCroppingSystem) {
171
                $characteristic = CharacteristicsModel::query()->where('uuid', $characteristicIdCroppingSystem)->first();
172
                if(!isset($characteristic)){
173
                    return;
174
                }
175
                $query->whereRaw(
176
                    'exists(SELECT * FROM user_characteristics where characteristic_id = ? AND user_characteristics.user_id = interactions.user_id)',
177
                    $characteristic->id
178
                );
179
            })
180
            ->when($departmentNumber !== null, function ($query) use($departmentNumber) {
181
                $query
182
                    ->join('users', 'users.id', 'interactions.user_id')
183
                    ->join('contexts', 'users.context_id', 'contexts.id')
184
                    ->where('contexts.department_number', $departmentNumber);
185
            })
186
            ->where('page_id', $pageId)
187
            ->whereNotNull('interactions.user_id')
188
            ->orderBy('interactions.updated_at', 'desc')
189
            ->paginate()
190
            ->through(function ($item){
191
                return new FollowerDto($item->user->toDto(), $item->user->context->toDto(), $item->toDto());
192
            })
193
        ;
194
    }
195
}
196