<?php
namespace Orioly\Entity\Login;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Orioly\Constants\Settings\OriolySettingsBundleConstants as OSBC;
use Orioly\Entity\Login\User\BaseUser;
use Orioly\Entity\Settings\UserAccount;
use Orioly\Entity\Tracking\Event;
use Orioly\Entity\Util\UtilCountry;
class User extends BaseUser
{
const ACCOUNT_TYPE_SUPPLIER = 0;
const ACCOUNT_TYPE_AGENT = 1;
const ACCOUNT_TYPES = [
'orioly.accountType.supplier' => self::ACCOUNT_TYPE_SUPPLIER,
'orioly.accountType.agent' => self::ACCOUNT_TYPE_AGENT,
];
const TYPE_WEB = 0;
const TYPE_BOOKER = 1;
const TYPE_AFFILIATE = 2;
const TYPE_EXPERIENCE_BANK = 3;
const TYPE_API = 4;
/**
* @var
*/
protected $id;
/**
* @var UtilCountry
*/
protected $country;
/**
* @var string
*/
protected $agencyName;
/**
* @var UserAccount
*/
protected $account;
/**
* @var
*/
protected $username;
/**
* @var
*/
protected $email;
/**
* @var
*/
protected $locked;
/**
* @var
*/
protected $expired;
/**
* @var string
*/
protected $socialAccountTokenStatus;
/**
* @var string
*/
private $hash;
/**
* @var \DateTime
*/
private $created;
/**
* @var \DateTime
*/
private $updated;
/**
* @var \DateTime
*/
private $deleted;
/**
* @var string
*/
private $timezone;
/**
* @var ArrayCollection|UserToken[]
*/
private $tokens;
/**
* @var string
*/
private $apiKey;
/**
* @var string
*/
private $initialSubscription;
/**
* @var integer
*/
private $type = self::TYPE_WEB;
/**
* @var string
*/
private $firstName;
/**
* @var string
*/
private $lastName;
/**
* @var string
*/
private $formReferal;
/**
* @var integer
*/
private $demoData = 0;
/**
* @var ArrayCollection|Event[]
*/
private $events;
/**
* @var integer
*/
private $accountType;
/**
* User constructor.
*/
public function __construct()
{
parent::__construct();
$this->tokens = new ArrayCollection();
$this->events = new ArrayCollection();
}
/**
* @return UtilCountry
*/
public function getCountry()
{
return $this->country;
}
/**
* @param UtilCountry $country
*/
public function setCountry($country)
{
$this->country = $country;
}
/**
* @return string
*/
public function getAgencyName()
{
return $this->agencyName;
}
/**
* @param string $agencyName
* @return $this
*/
public function setAgencyName($agencyName)
{
$this->agencyName = $agencyName;
return $this;
}
/**
* Get account
*
* @return UserAccount
*/
public function getAccount()
{
return $this->account;
}
/**
* Set account
*
* @param UserAccount $account
* @return $this
*/
public function setAccount(UserAccount $account = null)
{
$this->account = $account;
return $this;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
*
* @return User|\FOS\UserBundle\Model\User
*/
public function setEmail($email)
{
return parent::setEmail($email)->setUsername($email);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
* @return User
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return mixed
*/
public function getUsername()
{
return $this->username;
}
/**
* @param mixed $username
*
* @return $this
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* @return mixed
*/
public function getExpired()
{
return $this->expired;
}
/**
* @param mixed $expired
*
* @return User
*/
public function setExpired($expired)
{
$this->expired = $expired;
return $this;
}
/**
* @return mixed
*/
public function getLocked()
{
return $this->locked;
}
/**
* @param mixed $locked
*
* @return $this
*/
public function setLocked($locked)
{
$this->locked = $locked;
return $this;
}
/**
* @return bool
*/
public function isConfirmed()
{
return is_null($this->getConfirmationToken());
}
/**
* @return bool
*/
public function getActive()
{
return $this->isEnabled();
}
/**
* @return string
*/
public function getSocialAccountTokenStatus()
{
return $this->socialAccountTokenStatus;
}
/**
* @param string $socialAccountTokenStatus
*/
public function setSocialAccountTokenStatus($socialAccountTokenStatus)
{
$this->socialAccountTokenStatus = $socialAccountTokenStatus;
}
/**
* Get hash
*
* @return string
*/
public function getHash()
{
return $this->hash;
}
/**
* Set hash
*
* @param string $hash
*
* @return User
*/
public function setHash($hash)
{
$this->hash = $hash;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Get updated
*
* @return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Get deleted
*
* @return \DateTime
*/
public function getDeleted()
{
return $this->deleted;
}
/**
* @return string
*/
public function getTimezone()
{
return $this->timezone;
}
/**
* @param string $timezone
*
* @return User
*/
public function setTimezone($timezone)
{
$this->timezone = $timezone;
return $this;
}
/**
* Remove token
*
* @param UserToken $token
*/
public function removeToken(UserToken $token)
{
$this->tokens->removeElement($token);
}
/**
* Get tokens
*
* @return ArrayCollection|UserToken[]
*/
public function getTokens()
{
return $this->tokens;
}
/**
* @param $objectHash
* @return $this
* @throws \Exception
*/
public function setTokenFromAccessingObject($objectHash)
{
$token = $this->tokens->filter(function (UserToken $token) use ($objectHash) {
return $token->getObjectHash() == $objectHash && $token->getExpire() > new \DateTime();
}
);
if ($token->count() > 0) {
$token = $token->first();
} else {
$token = (new UserToken())->setObjectHash($objectHash);
$this->addToken($token);
}
$this->apiKey = $token->getApiKey();
return $this;
}
/**
* Add tokens
*
* @param UserToken $token
* @return User
*/
public function addToken(UserToken $token)
{
$this->tokens->add($token->setUser($this));
return $this;
}
/**
* @return string
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* @param string $apiKey
* @return User
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
return $this;
}
/**
* @return string
*/
public function getInitialSubscription()
{
return $this->initialSubscription;
}
/**
* @param string $initialSubscription
* @return $this
*/
public function setInitialSubscription($initialSubscription)
{
$this->initialSubscription = $initialSubscription;
return $this;
}
/**
* Get type
*
* @return integer
*/
public function getType()
{
return $this->type;
}
/**
* Set type
*
* @param integer $type
*
* @return User
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Set firstName
*
* @param string $firstName
*
* @return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* @param string $lastName
*
* @return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* @return string
*/
public function getFormReferal()
{
return $this->formReferal;
}
/**
* @param string $formReferal
* @return $this
*/
public function setFormReferal(string $formReferal)
{
$this->formReferal = $formReferal;
return $this;
}
/**
* @return string
*/
public function getFullName()
{
return $this->getFirstName() ? sprintf('%s %s', $this->getFirstName(), $this->getLastName() ?? '') : $this->getEmail();
}
/**
* @param array $roles
* @return User
*/
public function addRoles(array $roles)
{
foreach ($roles as $role) {
$this->addRole($role);
}
return $this;
}
/**
* @return int
*/
public function getDemoData()
{
return $this->demoData;
}
/**
* @param int $demoData
* @return User
*/
public function setDemoData($demoData)
{
$this->demoData = $demoData;
return $this;
}
/**
* Get events
*
* @return Collection
*/
public function getEvents()
{
return $this->events;
}
/**
* @return bool
*/
public function isSupplier()
{
return
$this->hasRole(OSBC::ROLE_ADMIN) ||
$this->hasRole(OSBC::ROLE_USER) ||
$this->hasRole(OSBC::ROLE_ORIOLY_LIMITED);
}
/**
* @return bool
*/
public function isSupplierAdmin()
{
return $this->hasRole(OSBC::ROLE_ADMIN);
}
/**
* @return bool
*/
public function isAgent()
{
return $this->hasRole(OSBC::ROLE_AGENT) || $this->hasRole(OSBC::ROLE_AGENT_LIMITED);
}
/**
* @return bool
*/
public function isOnlyAgent()
{
return $this->isAgent() && !$this->isSupplier();
}
/**
* @return bool
*/
public function isAffiliate()
{
return $this->hasRole(OSBC::ROLE_AFFILIATE);
}
/**
* @return bool
*/
private function isAffiliateOnly()
{
return $this->hasRole(OSBC::ROLE_AFFILIATE) && !$this->hasRole(OSBC::ROLE_ADMIN);
}
/**
* This user doesn't own any activities.
*
* @return bool
*/
public function isAgentOrAffiliateOnly()
{
return $this->isOnlyAgent() || $this->isAffiliateOnly();
}
/**
* @return int
*/
public function getAccountType()
{
return $this->accountType;
}
/**
* @param int $accountType
* @return User
*/
public function setAccountType($accountType)
{
$this->accountType = $accountType;
return $this;
}
public function hasLastLogin(): bool
{
return $this->lastLogin !== null;
}
}