PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServerGrove/SGLiveChatBundle/Document/Operator.php

https://github.com/casoetan/ServerGroveLiveChat
PHP | 194 lines | 96 code | 27 blank | 71 comment | 2 complexity | 4aa9c42b107eed6f8aa51ed2ecc52c0c MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. namespace ServerGrove\SGLiveChatBundle\Document;
  3. use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
  4. use Symfony\Component\Security\Core\User\AccountInterface;
  5. use ServerGrove\SGLiveChatBundle\Document\Operator\Department;
  6. /**
  7. * Description of Operator
  8. *
  9. * @author Ismael Ambrosi<ismael@servergrove.com>
  10. * @mongodb:Document(
  11. * collection="operator",
  12. * repositoryClass="ServerGrove\SGLiveChatBundle\Document\OperatorRepository"
  13. * )
  14. * @mongodb:InheritanceType("SINGLE_COLLECTION")
  15. * @mongodb:DiscriminatorField(fieldName="type")
  16. * @mongodb:DiscriminatorMap({"admin"="Administrator", "operator"="Operator"})
  17. * @mongodb:HasLifecycleCallbacks
  18. */
  19. class Operator extends User implements AccountInterface, PasswordEncoderInterface
  20. {
  21. /**
  22. * @var boolean
  23. * @mongodb:Field(type="boolean")
  24. */
  25. private $isOnline;
  26. /**
  27. * @var boolean
  28. * @mongodb:Field(type="boolean")
  29. */
  30. private $isActive;
  31. /**
  32. * @var string
  33. * @mongodb:String
  34. */
  35. private $passwd;
  36. /**
  37. * @var ServerGrove\SGLiveChatBundle\Document\Operator\Rating
  38. * @mongodb:ReferenceMany(targetDocument="ServerGrove\SGLiveChatBundle\Document\Operator\Rating")
  39. */
  40. private $ratings = array();
  41. /**
  42. * @var Department[]
  43. * @mongodb:ReferenceMany(targetDocument="ServerGrove\SGLiveChatBundle\Document\Operator\Department")
  44. */
  45. private $departments;
  46. public function addRating(Operator\Rating $rating)
  47. {
  48. $this->ratings[] = $rating;
  49. }
  50. /**
  51. * @return boolean $isOnline
  52. */
  53. public function getIsOnline()
  54. {
  55. return $this->isOnline;
  56. }
  57. /**
  58. * @param boolean $isOnline
  59. * @return void
  60. */
  61. public function setIsOnline($isOnline)
  62. {
  63. $this->isOnline = $isOnline;
  64. }
  65. /**
  66. * @return boolean $isActive
  67. */
  68. public function getIsActive()
  69. {
  70. return $this->isActive;
  71. }
  72. /**
  73. * @param boolean $isActive
  74. * @return void
  75. */
  76. public function setIsActive($isActive)
  77. {
  78. $this->isActive = $isActive;
  79. }
  80. /**
  81. * @return string $passwd
  82. */
  83. public function getPasswd()
  84. {
  85. return $this->passwd;
  86. }
  87. /**
  88. * @param string $passwd
  89. * @return void
  90. */
  91. public function setPasswd($passwd)
  92. {
  93. $this->passwd = $this->encodePassword($passwd, $this->getSalt());
  94. }
  95. /**
  96. * @return Department[] $departments
  97. */
  98. public function getDepartments()
  99. {
  100. return $this->departments;
  101. }
  102. public function addDepartment(Department $department)
  103. {
  104. $this->departments[] = $department;
  105. }
  106. public function getKind()
  107. {
  108. return 'Operator';
  109. }
  110. # -- AccountInterface implementation ----------------
  111. /**
  112. * @return string
  113. */
  114. public function __toString()
  115. {
  116. return strtr('(:id) :name, :email', array(
  117. ':email' => $this->getEmail(),
  118. ':name' => $this->getName(),
  119. ':id' => $this->getId()));
  120. }
  121. /**
  122. * @param AccountInterface $account
  123. * @return boolean
  124. */
  125. public function equals(AccountInterface $account)
  126. {
  127. return $account instanceof Operator && $account->getId() == $this->getId();
  128. }
  129. public function eraseCredentials()
  130. {
  131. }
  132. /**
  133. * @return string
  134. */
  135. public function getPassword()
  136. {
  137. return $this->getPasswd();
  138. }
  139. /**
  140. * @return array
  141. */
  142. public function getRoles()
  143. {
  144. return array(
  145. 'ROLE_USER');
  146. }
  147. public function getSalt()
  148. {
  149. return __NAMESPACE__ . '\\' . __CLASS__;
  150. }
  151. public function getUsername()
  152. {
  153. return $this->getEmail();
  154. }
  155. public function encodePassword($raw, $salt)
  156. {
  157. return md5(md5($raw) . '-' . $salt);
  158. }
  159. public function isPasswordValid($encoded, $raw, $salt)
  160. {
  161. return $encoded == $this->encodePassword($raw, $salt);
  162. }
  163. }