src/Entity/Login/User.php line 13

Open in your IDE?
  1. <?php
  2. namespace Orioly\Entity\Login;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Orioly\Constants\Settings\OriolySettingsBundleConstants as OSBC;
  6. use Orioly\Entity\Login\User\BaseUser;
  7. use Orioly\Entity\Settings\UserAccount;
  8. use Orioly\Entity\Tracking\Event;
  9. use Orioly\Entity\Util\UtilCountry;
  10. class User extends BaseUser
  11. {
  12.     const ACCOUNT_TYPE_SUPPLIER 0;
  13.     const ACCOUNT_TYPE_AGENT 1;
  14.     const ACCOUNT_TYPES = [
  15.         'orioly.accountType.supplier' => self::ACCOUNT_TYPE_SUPPLIER,
  16.         'orioly.accountType.agent' => self::ACCOUNT_TYPE_AGENT,
  17.     ];
  18.     const TYPE_WEB 0;
  19.     const TYPE_BOOKER 1;
  20.     const TYPE_AFFILIATE 2;
  21.     const TYPE_EXPERIENCE_BANK 3;
  22.     const TYPE_API 4;
  23.     /**
  24.      * @var
  25.      */
  26.     protected $id;
  27.     /**
  28.      * @var UtilCountry
  29.      */
  30.     protected $country;
  31.     /**
  32.      * @var string
  33.      */
  34.     protected $agencyName;
  35.     /**
  36.      * @var UserAccount
  37.      */
  38.     protected $account;
  39.     /**
  40.      * @var
  41.      */
  42.     protected $username;
  43.     /**
  44.      * @var
  45.      */
  46.     protected $email;
  47.     /**
  48.      * @var
  49.      */
  50.     protected $locked;
  51.     /**
  52.      * @var
  53.      */
  54.     protected $expired;
  55.     /**
  56.      * @var string
  57.      */
  58.     protected $socialAccountTokenStatus;
  59.     /**
  60.      * @var string
  61.      */
  62.     private $hash;
  63.     /**
  64.      * @var \DateTime
  65.      */
  66.     private $created;
  67.     /**
  68.      * @var \DateTime
  69.      */
  70.     private $updated;
  71.     /**
  72.      * @var \DateTime
  73.      */
  74.     private $deleted;
  75.     /**
  76.      * @var string
  77.      */
  78.     private $timezone;
  79.     /**
  80.      * @var ArrayCollection|UserToken[]
  81.      */
  82.     private $tokens;
  83.     /**
  84.      * @var string
  85.      */
  86.     private $apiKey;
  87.     /**
  88.      * @var string
  89.      */
  90.     private $initialSubscription;
  91.     /**
  92.      * @var integer
  93.      */
  94.     private $type self::TYPE_WEB;
  95.     /**
  96.      * @var string
  97.      */
  98.     private $firstName;
  99.     /**
  100.      * @var string
  101.      */
  102.     private $lastName;
  103.     /**
  104.      * @var string
  105.      */
  106.     private $formReferal;
  107.     /**
  108.      * @var integer
  109.      */
  110.     private $demoData 0;
  111.     /**
  112.      * @var ArrayCollection|Event[]
  113.      */
  114.     private $events;
  115.     /**
  116.      * @var integer
  117.      */
  118.     private $accountType;
  119.     /**
  120.      * User constructor.
  121.      */
  122.     public function __construct()
  123.     {
  124.         parent::__construct();
  125.         $this->tokens = new ArrayCollection();
  126.         $this->events = new ArrayCollection();
  127.     }
  128.     /**
  129.      * @return UtilCountry
  130.      */
  131.     public function getCountry()
  132.     {
  133.         return $this->country;
  134.     }
  135.     /**
  136.      * @param UtilCountry $country
  137.      */
  138.     public function setCountry($country)
  139.     {
  140.         $this->country $country;
  141.     }
  142.     /**
  143.      * @return string
  144.      */
  145.     public function getAgencyName()
  146.     {
  147.         return $this->agencyName;
  148.     }
  149.     /**
  150.      * @param string $agencyName
  151.      * @return $this
  152.      */
  153.     public function setAgencyName($agencyName)
  154.     {
  155.         $this->agencyName $agencyName;
  156.         return $this;
  157.     }
  158.     /**
  159.      * Get account
  160.      *
  161.      * @return UserAccount
  162.      */
  163.     public function getAccount()
  164.     {
  165.         return $this->account;
  166.     }
  167.     /**
  168.      * Set account
  169.      *
  170.      * @param UserAccount $account
  171.      * @return $this
  172.      */
  173.     public function setAccount(UserAccount $account null)
  174.     {
  175.         $this->account $account;
  176.         return $this;
  177.     }
  178.     /**
  179.      * @return mixed
  180.      */
  181.     public function getEmail()
  182.     {
  183.         return $this->email;
  184.     }
  185.     /**
  186.      * @param string $email
  187.      *
  188.      * @return User|\FOS\UserBundle\Model\User
  189.      */
  190.     public function setEmail($email)
  191.     {
  192.         return parent::setEmail($email)->setUsername($email);
  193.     }
  194.     /**
  195.      * Get id
  196.      *
  197.      * @return integer
  198.      */
  199.     public function getId()
  200.     {
  201.         return $this->id;
  202.     }
  203.     /**
  204.      * @param mixed $id
  205.      * @return User
  206.      */
  207.     public function setId($id)
  208.     {
  209.         $this->id $id;
  210.         return $this;
  211.     }
  212.     /**
  213.      * @return mixed
  214.      */
  215.     public function getUsername()
  216.     {
  217.         return $this->username;
  218.     }
  219.     /**
  220.      * @param mixed $username
  221.      *
  222.      * @return $this
  223.      */
  224.     public function setUsername($username)
  225.     {
  226.         $this->username $username;
  227.         return $this;
  228.     }
  229.     /**
  230.      * @return mixed
  231.      */
  232.     public function getExpired()
  233.     {
  234.         return $this->expired;
  235.     }
  236.     /**
  237.      * @param mixed $expired
  238.      *
  239.      * @return User
  240.      */
  241.     public function setExpired($expired)
  242.     {
  243.         $this->expired $expired;
  244.         return $this;
  245.     }
  246.     /**
  247.      * @return mixed
  248.      */
  249.     public function getLocked()
  250.     {
  251.         return $this->locked;
  252.     }
  253.     /**
  254.      * @param mixed $locked
  255.      *
  256.      * @return $this
  257.      */
  258.     public function setLocked($locked)
  259.     {
  260.         $this->locked $locked;
  261.         return $this;
  262.     }
  263.     /**
  264.      * @return bool
  265.      */
  266.     public function isConfirmed()
  267.     {
  268.         return is_null($this->getConfirmationToken());
  269.     }
  270.     /**
  271.      * @return bool
  272.      */
  273.     public function getActive()
  274.     {
  275.         return $this->isEnabled();
  276.     }
  277.     /**
  278.      * @return string
  279.      */
  280.     public function getSocialAccountTokenStatus()
  281.     {
  282.         return $this->socialAccountTokenStatus;
  283.     }
  284.     /**
  285.      * @param string $socialAccountTokenStatus
  286.      */
  287.     public function setSocialAccountTokenStatus($socialAccountTokenStatus)
  288.     {
  289.         $this->socialAccountTokenStatus $socialAccountTokenStatus;
  290.     }
  291.     /**
  292.      * Get hash
  293.      *
  294.      * @return string
  295.      */
  296.     public function getHash()
  297.     {
  298.         return $this->hash;
  299.     }
  300.     /**
  301.      * Set hash
  302.      *
  303.      * @param string $hash
  304.      *
  305.      * @return User
  306.      */
  307.     public function setHash($hash)
  308.     {
  309.         $this->hash $hash;
  310.         return $this;
  311.     }
  312.     /**
  313.      * Get created
  314.      *
  315.      * @return \DateTime
  316.      */
  317.     public function getCreated()
  318.     {
  319.         return $this->created;
  320.     }
  321.     /**
  322.      * Get updated
  323.      *
  324.      * @return \DateTime
  325.      */
  326.     public function getUpdated()
  327.     {
  328.         return $this->updated;
  329.     }
  330.     /**
  331.      * Get deleted
  332.      *
  333.      * @return \DateTime
  334.      */
  335.     public function getDeleted()
  336.     {
  337.         return $this->deleted;
  338.     }
  339.     /**
  340.      * @return string
  341.      */
  342.     public function getTimezone()
  343.     {
  344.         return $this->timezone;
  345.     }
  346.     /**
  347.      * @param string $timezone
  348.      *
  349.      * @return User
  350.      */
  351.     public function setTimezone($timezone)
  352.     {
  353.         $this->timezone $timezone;
  354.         return $this;
  355.     }
  356.     /**
  357.      * Remove token
  358.      *
  359.      * @param UserToken $token
  360.      */
  361.     public function removeToken(UserToken $token)
  362.     {
  363.         $this->tokens->removeElement($token);
  364.     }
  365.     /**
  366.      * Get tokens
  367.      *
  368.      * @return ArrayCollection|UserToken[]
  369.      */
  370.     public function getTokens()
  371.     {
  372.         return $this->tokens;
  373.     }
  374.     /**
  375.      * @param $objectHash
  376.      * @return $this
  377.      * @throws \Exception
  378.      */
  379.     public function setTokenFromAccessingObject($objectHash)
  380.     {
  381.         $token $this->tokens->filter(function (UserToken $token) use ($objectHash) {
  382.             return $token->getObjectHash() == $objectHash && $token->getExpire() > new \DateTime();
  383.         }
  384.         );
  385.         if ($token->count() > 0) {
  386.             $token $token->first();
  387.         } else {
  388.             $token = (new UserToken())->setObjectHash($objectHash);
  389.             $this->addToken($token);
  390.         }
  391.         $this->apiKey $token->getApiKey();
  392.         return $this;
  393.     }
  394.     /**
  395.      * Add tokens
  396.      *
  397.      * @param UserToken $token
  398.      * @return User
  399.      */
  400.     public function addToken(UserToken $token)
  401.     {
  402.         $this->tokens->add($token->setUser($this));
  403.         return $this;
  404.     }
  405.     /**
  406.      * @return string
  407.      */
  408.     public function getApiKey()
  409.     {
  410.         return $this->apiKey;
  411.     }
  412.     /**
  413.      * @param string $apiKey
  414.      * @return User
  415.      */
  416.     public function setApiKey($apiKey)
  417.     {
  418.         $this->apiKey $apiKey;
  419.         return $this;
  420.     }
  421.     /**
  422.      * @return string
  423.      */
  424.     public function getInitialSubscription()
  425.     {
  426.         return $this->initialSubscription;
  427.     }
  428.     /**
  429.      * @param string $initialSubscription
  430.      * @return $this
  431.      */
  432.     public function setInitialSubscription($initialSubscription)
  433.     {
  434.         $this->initialSubscription $initialSubscription;
  435.         return $this;
  436.     }
  437.     /**
  438.      * Get type
  439.      *
  440.      * @return integer
  441.      */
  442.     public function getType()
  443.     {
  444.         return $this->type;
  445.     }
  446.     /**
  447.      * Set type
  448.      *
  449.      * @param integer $type
  450.      *
  451.      * @return User
  452.      */
  453.     public function setType($type)
  454.     {
  455.         $this->type $type;
  456.         return $this;
  457.     }
  458.     /**
  459.      * Set firstName
  460.      *
  461.      * @param string $firstName
  462.      *
  463.      * @return User
  464.      */
  465.     public function setFirstName($firstName)
  466.     {
  467.         $this->firstName $firstName;
  468.         return $this;
  469.     }
  470.     /**
  471.      * Get firstName
  472.      *
  473.      * @return string
  474.      */
  475.     public function getFirstName()
  476.     {
  477.         return $this->firstName;
  478.     }
  479.     /**
  480.      * Set lastName
  481.      *
  482.      * @param string $lastName
  483.      *
  484.      * @return User
  485.      */
  486.     public function setLastName($lastName)
  487.     {
  488.         $this->lastName $lastName;
  489.         return $this;
  490.     }
  491.     /**
  492.      * Get lastName
  493.      *
  494.      * @return string
  495.      */
  496.     public function getLastName()
  497.     {
  498.         return $this->lastName;
  499.     }
  500.     /**
  501.      * @return string
  502.      */
  503.     public function getFormReferal()
  504.     {
  505.         return $this->formReferal;
  506.     }
  507.     /**
  508.      * @param string $formReferal
  509.      * @return $this
  510.      */
  511.     public function setFormReferal(string $formReferal)
  512.     {
  513.         $this->formReferal $formReferal;
  514.         return $this;
  515.     }
  516.     /**
  517.      * @return string
  518.      */
  519.     public function getFullName()
  520.     {
  521.         return $this->getFirstName() ? sprintf('%s %s'$this->getFirstName(), $this->getLastName() ?? '') : $this->getEmail();
  522.     }
  523.     /**
  524.      * @param array $roles
  525.      * @return User
  526.      */
  527.     public function addRoles(array $roles)
  528.     {
  529.         foreach ($roles as $role) {
  530.             $this->addRole($role);
  531.         }
  532.         return $this;
  533.     }
  534.     /**
  535.      * @return int
  536.      */
  537.     public function getDemoData()
  538.     {
  539.         return $this->demoData;
  540.     }
  541.     /**
  542.      * @param int $demoData
  543.      * @return User
  544.      */
  545.     public function setDemoData($demoData)
  546.     {
  547.         $this->demoData $demoData;
  548.         return $this;
  549.     }
  550.     /**
  551.      * Get events
  552.      *
  553.      * @return Collection
  554.      */
  555.     public function getEvents()
  556.     {
  557.         return $this->events;
  558.     }
  559.     /**
  560.      * @return bool
  561.      */
  562.     public function isSupplier()
  563.     {
  564.         return
  565.             $this->hasRole(OSBC::ROLE_ADMIN) ||
  566.             $this->hasRole(OSBC::ROLE_USER) ||
  567.             $this->hasRole(OSBC::ROLE_ORIOLY_LIMITED);
  568.     }
  569.     /**
  570.      * @return bool
  571.      */
  572.     public function isSupplierAdmin()
  573.     {
  574.         return $this->hasRole(OSBC::ROLE_ADMIN);
  575.     }
  576.     /**
  577.      * @return bool
  578.      */
  579.     public function isAgent()
  580.     {
  581.         return $this->hasRole(OSBC::ROLE_AGENT) || $this->hasRole(OSBC::ROLE_AGENT_LIMITED);
  582.     }
  583.     /**
  584.      * @return bool
  585.      */
  586.     public function isOnlyAgent()
  587.     {
  588.         return $this->isAgent() && !$this->isSupplier();
  589.     }
  590.     /**
  591.      * @return bool
  592.      */
  593.     public function isAffiliate()
  594.     {
  595.         return $this->hasRole(OSBC::ROLE_AFFILIATE);
  596.     }
  597.     /**
  598.      * @return bool
  599.      */
  600.     private function isAffiliateOnly()
  601.     {
  602.         return $this->hasRole(OSBC::ROLE_AFFILIATE) && !$this->hasRole(OSBC::ROLE_ADMIN);
  603.     }
  604.     /**
  605.      * This user doesn't own any activities.
  606.      *
  607.      * @return bool
  608.      */
  609.     public function isAgentOrAffiliateOnly()
  610.     {
  611.         return $this->isOnlyAgent() || $this->isAffiliateOnly();
  612.     }
  613.     /**
  614.      * @return int
  615.      */
  616.     public function getAccountType()
  617.     {
  618.         return $this->accountType;
  619.     }
  620.     /**
  621.      * @param int $accountType
  622.      * @return User
  623.      */
  624.     public function setAccountType($accountType)
  625.     {
  626.         $this->accountType $accountType;
  627.         return $this;
  628.     }
  629.     public function hasLastLogin(): bool
  630.     {
  631.         return $this->lastLogin !== null;
  632.     }
  633. }