44
55namespace Veeqtoh \DoorAccess \Classes ;
66
7+ use Illuminate \Support \Facades \Log ;
78use 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}
0 commit comments