FacebookRegistration.php 3.34 KB
<?php

namespace FootyRoom\Services\Registration;

use FootyRoom\Config;
use FootyRoom\User\AvatarCreator;
use Illuminate\Contracts\Auth\Guard;
use FootyRoom\Services\Auth\FacebookLogin;
use FootyRoom\Repositories\IUserRepository;
use Illuminate\Contracts\Events\Dispatcher;
use Facebook\Exceptions\FacebookSDKException;
use FootyRoom\Services\Registration\ActivationKeyMailer;

class FacebookRegistration extends Registration
{
    /**
     * @var string
     */
    public $fbtoken;

    /**
     * @var string
     */
    public $facebookId;

    /**
     * @var \FootyRoom\Services\Auth\FacebookLogin
     */
    protected $fbLogin;

    /**
     * @var \Facebook\GraphNodes\GraphUser
     */
    protected $fbMe;

    /**
     * @var \FootyRoom\Repositories\UserRepository
     */
    protected $userRepo;

    /**
     * @var \FootyRoom\Services\Registration\ActivationKeyMailer
     */
    protected $activationKeyMailer;

    /**
     * @var \Illuminate\Contracts\Auth\Guard
     */
    protected $auth;

    /**
     * @var \Illuminate\Contracts\Events\Dispatcher;
     */
    protected $events;

    /**
     * Constructor.
     *
     * @param array                                                $userData
     * @param \FootyRoom\Repositories\IUserRepository              $userRepo
     * @param \FootyRoom\Services\Registration\ActivationKeyMailer $activationKeyMailer
     * @param \Illuminate\Contracts\Auth\Guard                     $auth
     * @param \FootyRoom\Config                                    $config
     * @param \Illuminate\Contracts\Events\Dispatcher              $events
     */
    public function __construct(
        array $userData,
        IUserRepository $userRepo,
        ActivationKeyMailer $activationKeyMailer,
        Guard $auth,
        Config $config,
        Dispatcher $events
    ) {
        $this->userRepo = $userRepo;
        $this->activationKeyMailer = $activationKeyMailer;
        $this->auth = $auth;
        $this->config = $config;
        $this->events = $events;

        $this->fbtoken = $userData['fbtoken'];

        $this->fbLogin = new FacebookLogin($config, $auth, $userRepo);
        $this->fbLogin->setToken($this->fbtoken);

        try {
            $this->fbMe = $this->fbLogin->getMe();
        } catch (FacebookSDKException $e) {
            $this->errors = ['fbtoken' => ['auth']];

            return;
        }

        $this->facebookId = $this->fbMe->getId();
        $userData['email'] = $this->fbMe->getEmail();
        $userData['fullName'] = $this->fbMe->getName();
        $userData['status'] = 'newborn';

        $this->init($userData);
    }

    public function validate(): bool
    {
        if (isset($this->error['fbtoken'])) {
            return false;
        }

        $isValid = parent::validate();

        if (isset($this->errors['email'])) {
            $this->errors['fbtoken'] = $this->errors['email'];
            unset($this->errors['email']);
        }

        return $isValid;
    }

    public function register()
    {
        parent::register();

        $this->fbLogin->associateWith($this->userId);

        // Fetch avatar from Facebook.
        if (!$this->fbMe->getPicture()->isSilhouette()) {
            AvatarCreator::create($this->fbMe->getPicture()->getUrl(), $this->userId);
        }

        // Automatically login person who has signed up.
        $this->auth->loginUsingId($this->userId);
    }
}