ContextRepositorySql::getByUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
4
namespace App\Src\UseCases\Infra\Sql;
5
6
7
use App\Src\UseCases\Domain\Context\Dto\ContextDto;
8
use App\Src\UseCases\Domain\Context\Model\Context;
9
use App\Src\UseCases\Domain\Ports\ContextRepository;
10
use App\Src\UseCases\Infra\Sql\Model\ContextModel;
11
use App\User;
12
13
class ContextRepositorySql implements ContextRepository
14
{
15
    public function getByUser(string $userId):?Context
16
    {
17
        $user = User::where('uuid', $userId)->first();
18
        return $user->context !== null ? $user->context->toDomain() : null;
19
    }
20
21
    public function add(Context $context, string $userId)
22
    {
23
        $contextData = collect($context->toArray());
24
        $user = User::where('uuid', $userId)->first();
25
26
        $characteristics = $contextData->get('characteristics');
27
        $user->addCharacteristics($characteristics);
28
29
        $contextModel = (new ContextModel())->fill($contextData->except('characteristics')->toArray());
30
        $contextModel->save();
31
        $user->context_id = $contextModel->id;
32
        $user->save();
33
    }
34
35
    public function getByUserDto(string $userId): ?ContextDto
36
    {
37
        $user = User::where('uuid', $userId)->first();
38
        if($user === null){
39
            return null;
40
        }
41
        return $user->context !== null ? $user->context->toDto($user->uuid) : null;
0 ignored issues
show
Unused Code introduced by
The call to App\Src\UseCases\Infra\S...l\ContextModel::toDto() has too many arguments starting with $user->uuid. ( Ignorable by Annotation )

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

41
        return $user->context !== null ? $user->context->/** @scrutinizer ignore-call */ toDto($user->uuid) : null;

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42
    }
43
44
    public function update(Context $context, string $userId)
45
    {
46
        $user = User::where('uuid', $userId)->first();
47
        if($user === null){
48
            return null;
49
        }
50
51
        $contextModel = $user->context;
52
        if($contextModel === null){
53
            return null;
54
        }
55
        $contextData = collect($context->toArray());
56
        $contextModel->fill($contextData->except('characteristics')->toArray());
57
        $contextModel->save();
58
59
        $characteristics = $contextData->get('characteristics');
60
        $user->syncCharacteristics($characteristics);
61
    }
62
}
63