]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/UserBundle/Entity/User.php
CS
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Entity / User.php
index 1ff3046a8c798a226e5964d8ebdde28b170a3040..53c327f9b92baad663aea08adaf80f4e5f4993f5 100644 (file)
@@ -4,11 +4,12 @@ namespace Wallabag\UserBundle\Entity;
 
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
+use FOS\UserBundle\Model\User as BaseUser;
+use JMS\Serializer\Annotation\Accessor;
 use JMS\Serializer\Annotation\Groups;
 use JMS\Serializer\Annotation\XmlRoot;
 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
-use FOS\UserBundle\Model\User as BaseUser;
 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
 use Symfony\Component\Security\Core\User\UserInterface;
 use Wallabag\ApiBundle\Entity\Client;
@@ -36,7 +37,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      *
-     * @Groups({"user_api"})
+     * @Groups({"user_api", "user_api_with_client"})
      */
     protected $id;
 
@@ -45,39 +46,39 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      *
      * @ORM\Column(name="name", type="text", nullable=true)
      *
-     * @Groups({"user_api"})
+     * @Groups({"user_api", "user_api_with_client"})
      */
     protected $name;
 
     /**
      * @var string
      *
-     * @Groups({"user_api"})
+     * @Groups({"user_api", "user_api_with_client"})
      */
     protected $username;
 
     /**
      * @var string
      *
-     * @Groups({"user_api"})
+     * @Groups({"user_api", "user_api_with_client"})
      */
     protected $email;
 
     /**
-     * @var date
+     * @var \DateTime
      *
      * @ORM\Column(name="created_at", type="datetime")
      *
-     * @Groups({"user_api"})
+     * @Groups({"user_api", "user_api_with_client"})
      */
     protected $createdAt;
 
     /**
-     * @var date
+     * @var \DateTime
      *
      * @ORM\Column(name="updated_at", type="datetime")
      *
-     * @Groups({"user_api"})
+     * @Groups({"user_api", "user_api_with_client"})
      */
     protected $updatedAt;
 
@@ -91,13 +92,36 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      */
     protected $config;
 
+    /**
+     * @var ArrayCollection
+     *
+     * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
+     */
+    protected $siteCredentials;
+
+    /**
+     * @var ArrayCollection
+     *
+     * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
+     */
+    protected $clients;
+
+    /**
+     * @see getFirstClient() below
+     *
+     * @Groups({"user_api_with_client"})
+     * @Accessor(getter="getFirstClient")
+     */
+    protected $default_client;
+
     /**
      * @ORM\Column(type="integer", nullable=true)
      */
     private $authCode;
 
     /**
-     * @var bool Enabled yes/no
+     * @var bool
+     *
      * @ORM\Column(type="boolean")
      */
     private $twoFactorAuthentication = false;
@@ -107,11 +131,6 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      */
     private $trusted;
 
-    /**
-     * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
-     */
-    protected $clients;
-
     public function __construct()
     {
         parent::__construct();
@@ -125,7 +144,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      */
     public function timestamps()
     {
-        if (is_null($this->createdAt)) {
+        if (null === $this->createdAt) {
             $this->createdAt = new \DateTime();
         }
 
@@ -157,7 +176,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
     }
 
     /**
-     * @return string
+     * @return \DateTime
      */
     public function getCreatedAt()
     {
@@ -165,7 +184,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
     }
 
     /**
-     * @return string
+     * @return \DateTime
      */
     public function getUpdatedAt()
     {
@@ -288,4 +307,16 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
     {
         return $this->clients;
     }
+
+    /**
+     * Only used by the API when creating a new user it'll also return the first client (which was also created at the same time).
+     *
+     * @return Client
+     */
+    public function getFirstClient()
+    {
+        if (!empty($this->clients)) {
+            return $this->clients->first();
+        }
+    }
 }