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