Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17002 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php

declare(strict_types=1);

namespace LeadersLinked\Plugin;

use Laminas\Mvc\Controller\Plugin\AbstractPlugin;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Authentication\AuthenticationService;
use LeadersLinked\Model\Device;
use LeadersLinked\Mapper\DeviceMapper;
use LeadersLinked\Model\UserType;
use LeadersLinked\Mapper\CompanyMapper;
use LeadersLinked\Library\Storage;


class CurrentUserPlugin extends AbstractPlugin
{
    
    /**
     * 
     * @var \LeadersLinked\Plugin\CurrentUserPlugin
     */
    private static $_instance;
    
    /**
     * 
     * @var AuthenticationService
     */
    private $auth;
    
    /**
     *
     * @var boolean
     */
    private $hasIdentity;
    
    
    /**
     *
     * @var boolean
     */
    private $hasImpersonate;
    
    
    /**
     *
     * @return \LeadersLinked\Model\User
     */
    private $user;
    
    
    
    /**
     *
     * @return \LeadersLinked\Model\User
     */
    private $impersonateUser;
    
    
    /***
     * 
     * @var \LeadersLinked\Model\Company
     */
    private $company;
    
    /**
     * 
     * @var array
     */
    private $config;
    
    /**
     * 
     * @var \LeadersLinked\Plugin\CurrentUserPlugin
     */
    private  $adapter;
    
    /**
     *
     * @param array $config
     * @param AdapterInterface $adapter
     * @return \LeadersLinked\Plugin\CurrentUserPlugin
     */
    public static function getInstance($config, $adapter) 
    {
        if(self::$_instance == null) {
            self::$_instance = new CurrentUserPlugin($config, $adapter);
        }
        
        return self::$_instance;
    }
    
    
    /**
     *
     * @param array $config 
     * @param AdapterInterface $adapter
     */
    private function __construct($config, $adapter) 
    {
        $this->config = $config;
        $this->adapter = $adapter;
        
        $this->auth = new \Laminas\Authentication\AuthenticationService();
        $this->impersonateUser = null;
        $this->hasIdentity = false;
        $this->hasImpersonate = false;
        
        if($this->auth->hasIdentity()) {
            $identity = $this->auth->getIdentity();

            $userMapper     = \LeadersLinked\Mapper\UserMapper::getInstance($adapter);
            $this->user     = $userMapper->fetchOne($identity['user_id']);
     
            if($this->user) {
                $this->hasIdentity = true;
                
                if($this->user->impersonate_user_id) {
                    $this->hasImpersonate = true;
                    $this->impersonateUser = $userMapper->fetchOne($this->user->impersonate_user_id);
                }
                
                
                if($identity['company_id']) {
                    $companyMapper = CompanyMapper::getInstance($adapter);
                    $this->company = $companyMapper->fetchOne($identity['company_id']);
                }
            }
        }
    }


    /**
     * 
     * @return \LeadersLinked\Model\User
     */
    public function getUser() 
    {
        
        return $this->user;
    }
    
    
    
    /**
     *
     * @return \LeadersLinked\Model\User
     */
    public function getImpersonateUser()
    {
        
        return $this->impersonateUser;
    }
    

    
     /**
     * 
     * @return int
     */
    public function getUserId()
    {
        if($this->hasIdentity) {
            if($this->hasImpersonate) {
                return $this->impersonateUser->id;
            } else {
                return $this->user->id;
            }
        } else {
            return 0;
        }
    }
    

    /**
     * @return string
     */
    public function getUserTypeId()
    {
        if($this->hasIdentity) {
            if($this->hasImpersonate) {
                return $this->impersonateUser->usertype_id;
            } else {
                return $this->user->usertype_id;
            }
        } else {
            return UserType::GUEST;
        }
    }
    
    /**
     *
     * @return boolean
     */
    public function hasImpersonate()
    {
        return $this->hasImpersonate;
    }
    
    
   
    /**
     * 
     * @return boolean
     */
    public function hasIdentity() 
    {
        return $this->hasIdentity;
    }
    
    
    /**
     * 
     * @return \LeadersLinked\Model\Company
     */
    public function getCompany()
    {
        return $this->company;    
    }
    
    public function getCompanyId()
    {
        if($this->company) {
            return $this->company->id;
        } else {
            return 0;
        }
    }

    
    public function clearIdentity()
    {
        $this->auth->clearIdentity();
    }
    
    
    public function getCompanyImage()
    {
        if($this->company) {
          
            $storage = Storage::getInstance($this->config, $this->adapter);
            return $storage->getCompanyImage($this->company);
        } 
        
    }
    
    public function getCompanyCover()
    {
        if($this->company) {
            
            $storage = Storage::getInstance($this->config, $this->adapter);
            return $storage->getCompanyCover($this->company);
        }
        
    }
    
    public function getUserImage()
    {
        if($this->user) {
            
            $storage = Storage::getInstance($this->config, $this->adapter);
            return $storage->getUserImage($this->user);
        }
        
    }

   
}