Passed
Push — master ( 1ffd02...b93f0b )
by Bertrand
06:21
created

ContextRepositorySql::getByUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
4
namespace App\Src\UseCases\Infra\Sql;
5
6
7
use App\Src\UseCases\Domain\Agricultural\Model\Context;
8
use App\Src\UseCases\Domain\Ports\ContextRepository;
9
use App\User;
10
use Illuminate\Support\Facades\DB;
11
12
class ContextRepositorySql implements ContextRepository
13
{
14
    public function getByUser(string $userId):?Context
15
    {
16
        $user = User::where('uuid', $userId)->first();
17
        $context = DB::table('contexts')->where('id', $user->context_id)->first();
0 ignored issues
show
Bug Best Practice introduced by
The property context_id does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
18
        if($context == null){
19
            return null;
20
        }
21
        return new Context($context->uuid, $context->postal_code, json_decode($context->farmings, true));
22
    }
23
24
    public function add(Context $exploitation, string $userId)
25
    {
26
        $contextData = $exploitation->toArray();
27
        $contextId = DB::table('contexts')->insert($contextData);
28
29
        $user = User::where('uuid', $userId)->first();
30
        $user->context_id = $contextId;
0 ignored issues
show
Bug Best Practice introduced by
The property context_id does not exist on App\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
31
        $user->save();
32
    }
33
}
34