aboutsummaryrefslogtreecommitdiffhomepage
path: root/api/Users/Services/UserService.php
diff options
context:
space:
mode:
Diffstat (limited to 'api/Users/Services/UserService.php')
-rw-r--r--api/Users/Services/UserService.php88
1 files changed, 88 insertions, 0 deletions
diff --git a/api/Users/Services/UserService.php b/api/Users/Services/UserService.php
new file mode 100644
index 0000000..923acd0
--- /dev/null
+++ b/api/Users/Services/UserService.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Api\Users\Services;
+
+use Exception;
+use Illuminate\Auth\AuthManager;
+use Illuminate\Database\DatabaseManager;
+use Illuminate\Events\Dispatcher;
+use Api\Users\Exceptions\UserNotFoundException;
+use Api\Users\Events\UserWasCreated;
+use Api\Users\Events\UserWasDeleted;
+use Api\Users\Events\UserWasUpdated;
+use Api\Users\Repositories\UserRepository;
+
+class UserService
+{
+ private $auth;
+
+ private $database;
+
+ private $dispatcher;
+
+ private $userRepository;
+
+ public function __construct(
+ AuthManager $auth,
+ DatabaseManager $database,
+ Dispatcher $dispatcher,
+ UserRepository $userRepository
+ ) {
+ $this->auth = $auth;
+ $this->database = $database;
+ $this->dispatcher = $dispatcher;
+ $this->userRepository = $userRepository;
+ }
+
+ public function getAll($options = [])
+ {
+ return $this->userRepository->get($options);
+ }
+
+ public function getById($userId, array $options = [])
+ {
+ $user = $this->getRequestedUser($userId);
+
+ return $user;
+ }
+
+ public function create($data)
+ {
+ $user = $this->userRepository->create($data);
+
+ $this->dispatcher->fire(new UserWasCreated($user));
+
+ return $user;
+ }
+
+ public function update($userId, array $data)
+ {
+ $user = $this->getRequestedUser($userId);
+
+ $this->userRepository->update($user, $data);
+
+ $this->dispatcher->fire(new UserWasUpdated($user));
+
+ return $user;
+ }
+
+ public function delete($userId)
+ {
+ $user = $this->getRequestedUser($userId);
+
+ $this->userRepository->delete($userId);
+
+ $this->dispatcher->fire(new UserWasDeleted($user));
+ }
+
+ private function getRequestedUser($userId)
+ {
+ $user = $this->userRepository->getById($userId);
+
+ if (is_null($user)) {
+ throw new UserNotFoundException();
+ }
+
+ return $user;
+ }
+}