log_service = $log_service; $this->repository = $repository; $this->user_repository = $user_repository; $this->scope_service = $scope_service; $this->scope_repository = $scope_repository; $this->tx_service = $tx_service; } public function update($id, array $params) { $repository = $this->repository; $scope_service = $this->scope_service; $user_repository = $this->user_repository; return $this->tx_service->transaction(function () use ($id, $params, $repository, $scope_service, $user_repository) { $group = $repository->get($id); if (is_null($group)) { throw new InvalidApiScopeGroup(sprintf('api scope group id %s does not exists!', $id)); } $allowed_update_params = array('name', 'active', 'description', 'users', 'scopes'); foreach ($allowed_update_params as $param) { if (array_key_exists($param, $params)) { if ($param == 'name') { $older_group = $repository->getByName($params[$param]); if(!is_null($older_group) && $older_group->id != $id) { throw new InvalidApiScopeGroup(sprintf('there is already another api scope group name (%s).', $params[$param])); } } if($param === 'scopes') { $ids = $group->scopes()->pluck('id')->all(); $group->scopes()->detach($ids); $scopes = explode(',', $params['scopes']); foreach($scopes as $scope_id) { $scope = $this->scope_repository->get(intval($scope_id)); if(is_null($scope)) throw new EntityNotFoundException(sprintf('scope %s not found.',$scope_id)); $group->addScope($scope); } } else if($param === 'users'){ $group->removeAllUsers(); $users = explode(',', $params['users']); foreach($users as $user_id) { $user = $user_repository->get(intval($user_id)); if(is_null($user)) throw new EntityNotFoundException(sprintf('user %s not found.',$user_id)); $group->addUser($user); } } else $group->{$param} = $params[$param]; } } $repository->add($group); return true; }); } /** * @param int $id * @param bool $status status (active/non active) * @return void */ public function setStatus($id, $status) { $this->tx_service->transaction(function() use($id, $status){ $group = $this->repository->get($id); if(is_null($group)) return; $group->active = $status; $this->repository->add($group); }); } /** * @param string $name * @param bool $active * @param string $scopes * @param string $users * @return IApiScopeGroup */ public function register($name, $active, $scopes, $users) { $repository = $this->repository; $scope_service = $this->scope_service; $user_repository = $this->user_repository; return $this->tx_service->transaction(function () use ( $name, $active, $scopes, $users, $repository, $scope_service, $user_repository ) { if (ApiScopeGroup::where('name', '=', trim($name))->count() > 0) { throw new InvalidApiScopeGroup(sprintf('there is already another group with that name (%s).', $name)); } // todo : move to factory $repository->add($instance = new ApiScopeGroup ( array ( 'name' => trim($name), 'active' => $active, 'description' => '' ) )); $scopes = explode(',', $scopes); $users = explode(',', $users); foreach($scopes as $scope_id) { $scope = $this->scope_repository->get(intval($scope_id)); if(is_null($scope)) throw new EntityNotFoundException(sprintf('scope %s not found.',$scope_id)); $instance->addScope($scope); } foreach($users as $user_id) { $user = $user_repository->get(intval($user_id)); if(is_null($user)) throw new EntityNotFoundException(sprintf('user %s not found.',$user_id)); $instance->addUser($user); } return $instance; }); } }