Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
14 / 18
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
BranchContext
77.78% covered (warning)
77.78%
14 / 18
71.43% covered (warning)
71.43%
5 / 7
21.56
0.00% covered (danger)
0.00%
0 / 1
 id
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
4
 warehouses
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 accounts
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ensureWarehouse
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 ensureAccount
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
6.00
 ensureBranch
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
4.12
 scope
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Services\Location;
4
5use App\Models\Account;
6use App\Models\Warehouse;
7use Illuminate\Database\Eloquent\Builder;
8use Illuminate\Validation\ValidationException;
9
10class BranchContext
11{
12    public function id(): ?int
13    {
14        if (! feature('branch_management') || ! auth()->check() || ! auth()->user()->branch_id) {
15            return null;
16        }
17
18        return (int) auth()->user()->branch_id;
19    }
20
21    public function warehouses(): Builder
22    {
23        return Warehouse::query()->when($this->id(), fn ($query, $branchId) => $query->where('branch_id', $branchId));
24    }
25
26    public function accounts(): Builder
27    {
28        return Account::query()->when($this->id(), fn ($query, $branchId) => $query->where('branch_id', $branchId));
29    }
30
31    public function ensureWarehouse(?int $warehouseId): void
32    {
33        if (! $warehouseId || ! $this->id()) {
34            return;
35        }
36        if (! $this->warehouses()->whereKey($warehouseId)->exists()) {
37            throw ValidationException::withMessages(['warehouse_id' => 'You cannot use a warehouse from another branch.']);
38        }
39    }
40
41    public function ensureAccount(?int $accountId, string $field = 'account_id'): void
42    {
43        if (! $accountId || ! $this->id()) {
44            return;
45        }
46
47        if (! $this->accounts()->whereKey($accountId)->exists()) {
48            throw ValidationException::withMessages([$field => 'You cannot use an account from another branch.']);
49        }
50    }
51
52    public function ensureBranch(?int $branchId, string $field = 'branch_id'): void
53    {
54        if (! $this->id()) {
55            return;
56        }
57
58        if ((int) $branchId !== $this->id()) {
59            throw ValidationException::withMessages([$field => 'You cannot access data from another branch.']);
60        }
61    }
62
63    public function scope(Builder $query, string $column = 'branch_id'): Builder
64    {
65        return $query->when($this->id(), fn ($builder, $branchId) => $builder->where($column, $branchId));
66    }
67}