Skip to content

Commit 01abf14

Browse files
committed
Implement code manager feature
1 parent cb95bad commit 01abf14

File tree

2 files changed

+102
-30
lines changed

2 files changed

+102
-30
lines changed

src/Classes/CodeManager.php

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace Veeqtoh\DoorAccess\Classes;
66

7+
use Illuminate\Support\Facades\Log;
78
use Veeqtoh\DoorAccess\Classes\Traits\ConfigTrait;
8-
use Veeqtoh\DoorAccess\CodeGenerator;
9-
use Veeqtoh\DoorAccess\Database\DatabaseInterface;
9+
use Veeqtoh\DoorAccess\Exceptions\InvalidCodeException;
10+
use Veeqtoh\DoorAccess\Models\AccessCode;
1011

1112
/**
1213
* Class CodeManager
@@ -18,55 +19,81 @@ class CodeManager
1819
{
1920
use ConfigTrait;
2021

21-
private CodeGenerator $codeGenerator;
22-
private DatabaseInterface $database;
23-
24-
public function __construct(CodeGenerator $codeGenerator, DatabaseInterface $database)
22+
/**
23+
* Save the access code in the database.
24+
*
25+
* @param string $code The access code to be saved.
26+
*
27+
* @return AccessCode The newly created AccessCode model.
28+
*/
29+
public function saveCode(string $code): ?AccessCode
2530
{
26-
$this->codeGenerator = $codeGenerator;
27-
$this->database = $database;
31+
return AccessCode::create(['code' => $code]);
2832
}
2933

3034
/**
31-
* Allocate a code to a team member.
35+
* Allocate a code to an owner.
3236
*
33-
* @param string $teamMemberId
37+
* @param string $code The generated code to be allocated.
38+
* @param string $ownerId The owner to be allocated the code.
3439
*
35-
* @return ?string
40+
* @return AccessCode The AccessCode model with the new allocation.
3641
*/
37-
public function allocateCode(string $teamMemberId): ?string
42+
public function allocateCode(string $code, string $ownerId): AccessCode
3843
{
39-
// Check if a code is already allocated to the team member in the database
40-
$existingCode = $this->database->retrieve($teamMemberId);
44+
$existingCode = AccessCode::whereCode($code)->first();
4145

4246
if ($existingCode) {
43-
return $existingCode;
47+
if ($existingCode->isAllocated()) {
48+
$existingCode->reset();
49+
}
50+
51+
return $existingCode->allocate($ownerId);
4452
}
4553

46-
$code = $this->codeGenerator->generateCode();
54+
return AccessCode::create([
55+
'code' => $code,
56+
'owner_id' => $ownerId
57+
]);
58+
}
59+
60+
/**
61+
* Reset a code and make it available for reallocation.
62+
*
63+
* @param string $code,
64+
*
65+
* @return AccessCode The AccessCode model with the new allocation.
66+
*
67+
* @throws InvalidCodeException When code to be reset is not found.
68+
*/
69+
public function resetCode(string $code): AccessCode
70+
{
71+
$existingCode = AccessCode::whereCode($code)->first();
4772

48-
// Store the generated code for the team member in the database
49-
$this->database->store($teamMemberId, $code);
73+
if (!$existingCode) {
74+
Log::error("The code ({$code}) you are trying to reset does not exist");
75+
throw new InvalidCodeException("The code ({$code}) you are trying to reset does not exist", 1);
76+
}
5077

51-
return $code;
78+
return $existingCode->reset();
5279
}
5380

5481
/**
55-
* Reset a code and make it available for reallocation.
82+
* Destroy the access code in the database.
83+
*
84+
* @param string $code The access code to be destroyed.
5685
*
57-
* @param string $code
58-
* @return bool
86+
* @return bool Returns true if the access code was successfully destroyed, false otherwise.
5987
*/
60-
public function resetCode(string $code): bool
88+
public function destroyCode(string $code): bool
6189
{
62-
// Find the team member ID associated with the code in the database
63-
$teamMemberId = $this->database->retrieveTeamMemberId($code);
90+
$existingCode = AccessCode::whereCode($code)->first();
6491

65-
if ($teamMemberId) {
66-
// Remove the code from the database
67-
return $this->database->delete($code);
92+
if (!$existingCode) {
93+
Log::error("The code ({$code}) you are trying to destroy does not exist");
94+
throw new InvalidCodeException("The code ({$code}) you are trying to destroy does not exist", 1);
6895
}
6996

70-
return false;
97+
return $existingCode->delete();
7198
}
7299
}

src/Models/AccessCode.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class AccessCode extends Model
3535
* @var array<int,string>
3636
*/
3737
protected $fillable = [
38+
'code',
3839
'owner_id',
3940
'allocated_at',
4041
'reset_at',
@@ -77,7 +78,7 @@ protected static function newFactory()
7778
*/
7879
public static function findByCode(string $code): ?self
7980
{
80-
return self::where('code', $code)->first();
81+
return self::whereCode($code)->first();
8182
}
8283

8384
/**
@@ -90,4 +91,48 @@ public static function findByOwnerId(string $ownerId): Collection
9091
{
9192
return self::where('owner_id', $ownerId)->get();
9293
}
94+
95+
/**
96+
* Check if the access code is currently allocated.
97+
*
98+
* @return bool Returns true if the access code is allocated, false otherwise.
99+
*/
100+
public function isAllocated(): bool
101+
{
102+
return isset($this->allocated_at);
103+
}
104+
105+
/**
106+
* Reset the access code, making it available for reallocation.
107+
*
108+
* @return self Returns the AccessCode model with the new allocation.
109+
*/
110+
public function reset(): self
111+
{
112+
$this->update([
113+
'owner_id' => null,
114+
'allocated_at' => null,
115+
'reset_at' => now(),
116+
]);
117+
118+
return $this->fresh();
119+
}
120+
121+
/**
122+
* Allocate the access code to an owner.
123+
*
124+
* @param string $ownerId The id of the owner to whom the access code will be allocated.
125+
*
126+
* @return self Returns the AccessCode model with the new allocation.
127+
*/
128+
public function allocate(string $ownerId): self
129+
{
130+
$this->update([
131+
'owner_id' => $ownerId,
132+
'allocated_at' => now(),
133+
]);
134+
135+
return $this->fresh();
136+
}
137+
93138
}

0 commit comments

Comments
 (0)