You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0 KiB

7 years ago
<?php
namespace core\services\auth;
use core\entities\user\User;
use core\repositories\user\UserRepository;
3 years ago
use DomainException;
use yii\base\Exception;
7 years ago
class NetworkService
{
3 years ago
private UserRepository $users;
7 years ago
public function __construct(UserRepository $users)
{
$this->users = $users;
}
3 years ago
/**
* @param $network
* @param $identity
* @return User
* @throws Exception
*/
7 years ago
public function auth($network, $identity): User
{
if ($user = $this->users->findByNetworkIdentity($network, $identity)) {
return $user;
}
$user = User::signupByNetwork($network, $identity);
$this->users->save($user);
return $user;
}
public function attach($id, $network, $identity): void
{
if ($this->users->findByNetworkIdentity($network, $identity)) {
3 years ago
throw new DomainException('Network is already signed up.');
7 years ago
}
$user = $this->users->get($id);
$user->attachNetwork($network, $identity);
$this->users->save($user);
}
3 years ago
}