<?php
/**
 * Webkul Software.
 * 
 * PHP version 7.0+
 *
 * @category  Webkul
 * @package   Webkul_MobikulGraphQL
 * @author    Webkul <support@webkul.com>
 * @copyright 2010-2019 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html ASL Licence
 * @link      https://store.webkul.com/license.html
 */
namespace Webkul\MobikulGraphQL\Helper;

/**
 * Helper Data Class
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    protected $tokenFactory;
    protected $customerFactory;

    /**
     * Constructor function for Helper Class
     * 
     * @param \Magento\Framework\App\Helper\Context         $context      context
     * @param \Magento\Integration\Model\Oauth\TokenFactory $tokenFactory tokenFactory
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Integration\Model\Oauth\TokenFactory $tokenFactory
    ) {
        $this->tokenFactory = $tokenFactory;
        $this->customerFactory = $customerFactory;
        parent::__construct($context);
    }
    
        /**
     * Function to get customer id by provided token
     *
     * @param string $token customer token
     *
     * @return integer
     */
    public function getCustomerByToken($token)
    {
        if (!$token) {
            return null;
        }
        $column = $this->tokenFactory->create()
            ->getCollection()
            ->addFieldToFilter("token", $token)
            ->getFirstItem();
        return $this->customerFactory->create()->load($column->getCustomerId());
    }

}