SqlOrganizationRepository::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace App\Src\UseCases\Infra\Sql;
5
6
7
use App\Src\UseCases\Domain\Organizations\Model\Address;
8
use App\Src\UseCases\Domain\Organizations\Model\Organization;
9
use App\Src\UseCases\Domain\Ports\OrganizationRepository;
10
use Illuminate\Support\Facades\DB;
11
12
class SqlOrganizationRepository implements OrganizationRepository
13
{
14
    public function get(string $id): ?Organization
15
    {
16
        $record = DB::table('organizations')
17
            ->select()
18
            ->where('uuid', $id)
19
            ->first();
20
        if(!isset($record)){
21
            return null;
22
        }
23
        $address = new Address($record->city, $record->address1, $record->address2, $record->postal_code);
24
        return new Organization($id, $record->name, $record->path_picture, $address);
25
    }
26
27
    public function add(Organization $o)
28
    {
29
        $data = $o->toArray();
30
        unset($data['url_picture']);
31
        DB::table('organizations')->insert($data);
32
    }
33
34
    public function search(int $page, int $perPage = 10): array
35
    {
36
        $records = DB::table('organizations')
37
            ->select();
38
        $count = $records->count();
39
40
        $records = DB::table('organizations')
41
            ->select()
42
            ->offset(($page-1)*$perPage)
43
            ->limit($perPage)
44
            ->get();
45
        if(empty($records)){
46
            return [];
47
        }
48
        $organizations = [];
49
        foreach($records as $record){
50
            $address = new Address($record->city, $record->address1, $record->address2, $record->postal_code);
51
            $organizations[] = new Organization($record->uuid, $record->name, $record->path_picture, $address);
52
        }
53
        return [
54
            'list' => $organizations,
55
            'total' => $count
56
        ];
57
    }
58
59
    public function update(Organization $o)
60
    {
61
        $data = $o->toArray();
62
        unset($data['url_picture']);
63
        DB::table('organizations')->where('uuid', $o->id())->update($data);
64
    }
65
66
67
}
68