ReportingCharacteristicSql   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 54
c 2
b 0
f 0
dl 0
loc 75
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacteristicsByUserPage() 0 39 2
A getStatsByDepartment() 0 32 3
1
<?php
2
3
4
namespace App\Src\UseCases\Infra\Sql;
5
6
7
use App\Src\UseCases\Infra\Sql\Model\CharacteristicsModel;
8
use App\Src\UseCases\Infra\Sql\Model\InteractionModel;
9
10
class ReportingCharacteristicSql
11
{
12
    public function getStatsByDepartment(int $pageId, string $type = 'follow')
13
    {
14
        $interactions = InteractionModel::query()
15
            ->selectRaw('count(*) AS count, contexts.department_number as department_number')
16
            ->join('users', 'users.id', 'interactions.user_id')
17
            ->join('contexts', 'users.context_id', 'contexts.id')
18
            ->where(function ($query) use ($type){
19
                $query->when($type === 'follow', function ($query) {
20
                    $query->where('follow', true);
21
                    $query->orWhere('done', true);
22
                })
23
                ->when($type === 'do', function ($query) {
24
                    $query->where('done', true);
25
                });
26
            })
27
            ->where('interactions.page_id', $pageId)
28
            ->where('users.email', 'NOT LIKE', '%@neayi.com')
29
            ->whereNotNull('interactions.user_id')
30
            ->groupBy('department_number')
31
            ->get();
32
33
        $interactionsToReturn = [];
34
        foreach($interactions as $interaction){
35
            $characteristicsModel = CharacteristicsModel::query()->where('code', $interaction->department_number)->first();
36
            if(!isset($characteristicsModel)){
37
                continue;
38
            }
39
            $characteristicsModel->icon = route('api.icon.serve', ['id' => $characteristicsModel->uuid]);
40
            $interaction->departmentData = $characteristicsModel;
41
            $interactionsToReturn[] = $interaction->toArray();
42
        }
43
        return $interactionsToReturn;
44
    }
45
46
    public function getCharacteristicsByUserPage(int $pageId, string $type = 'follow', string $characteristicType = null)
47
    {
48
        $characteristicsCount = InteractionModel::query()
49
            ->selectRaw('
50
                count(*) as count,
51
                user_characteristics.characteristic_id
52
            ')
53
            ->join('users', 'users.id', 'interactions.user_id')
54
            ->join('contexts', 'users.context_id', 'contexts.id')
55
            ->join('user_characteristics', 'user_characteristics.user_id', 'users.id')
56
            ->join('characteristics', 'characteristics.id', 'user_characteristics.characteristic_id')
57
            ->where(function ($query) use ($type){
58
                $query->when($type === 'follow', function ($query) {
59
                    $query->where('follow', true);
60
                    $query->orWhere('done', true);
61
                })
62
                ->when($type === 'do', function ($query) {
63
                    $query->where('done', true);
64
                });
65
            })
66
            ->where('interactions.page_id', $pageId)
67
            ->whereNotNull('interactions.user_id')
68
            ->where('characteristics.type', $characteristicType)
69
            ->where('users.email', 'NOT LIKE', '%@neayi.com')
70
            ->groupBy('user_characteristics.characteristic_id')
71
            ->orderBy('count', 'desc')
72
            ->limit(10)
73
            ->get();
74
75
        $characteristicsToReturn = [];
76
        foreach($characteristicsCount as $characteristicCount){
77
            $characteristic = CharacteristicsModel::query()->find($characteristicCount->characteristic_id);
78
            $characteristic->count = $characteristicCount->count;
0 ignored issues
show
Bug introduced by
The property count does not seem to exist on App\Src\UseCases\Infra\S...el\CharacteristicsModel. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
79
            $c = $characteristic->toArray();
80
            $c['pretty_page_label'] = str_replace('Catégorie:', '', $c['pretty_page_label']);
81
            $c['icon'] = route('api.icon.serve', ['id' => $c['uuid']]);
82
            $characteristicsToReturn[] = $c;
83
        }
84
        return $characteristicsToReturn;
85
    }
86
}
87