Selaa lähdekoodia

Evolution #348: Stockage tags favoris dans user

Bastien Sevajol 12 vuotta sitten
vanhempi
commit
f1b46a0c38

+ 58 - 0
src/Muzich/CoreBundle/Entity/User.php Näytä tiedosto

@@ -11,6 +11,7 @@ use Muzich\CoreBundle\Entity\UsersTagsFavorites;
11 11
 use Symfony\Component\Validator\Constraints as Assert;
12 12
 use Muzich\CoreBundle\Validator as MuzichAssert;
13 13
 use Muzich\CoreBundle\Entity\ElementTagsProposition;
14
+use Muzich\CoreBundle\Entity\Tag;
14 15
 
15 16
 /**
16 17
  * Cet entité est l'utilisateur ayant effectué la requête.
@@ -173,6 +174,16 @@ class User extends BaseUser
173 174
   protected $live_datas = array();
174 175
   
175 176
   /**
177
+   * Tableau contenant les id => name des tags favoris
178
+   * de l'user. Ces donnée sont faites pour optimiser les calculs.
179
+   * Ce chamsp est mis ajour a chaque fois qu'un UsersTagsFavorite est manipulé.
180
+   * 
181
+   * @ORM\Column(type="text", unique=false, nullable=true)
182
+   * @var array 
183
+   */
184
+  private $tags_favorites_quick;
185
+  
186
+  /**
176 187
    * 
177 188
    */
178 189
   public function __construct()
@@ -757,4 +768,51 @@ class User extends BaseUser
757 768
     return false;
758 769
   }
759 770
   
771
+  public function getTagsFavoritesQuick()
772
+  {
773
+    if ($this->tags_favorites_quick == null)
774
+    {
775
+      return array();
776
+    }
777
+    
778
+    return json_decode($this->tags_favorites_quick, true);
779
+  }
780
+  
781
+  /**
782
+   * 
783
+   * @param array $tags_favorites_quick (id => name)
784
+   */
785
+  public function setTagsFavoritesQuick($tags_favorites_quick)
786
+  {
787
+    $this->tags_favorites_quick = json_encode($tags_favorites_quick);
788
+  }
789
+  
790
+  /**
791
+   * 
792
+   * @param \Muzich\CoreBundle\Entity\Tag $tag
793
+   */
794
+  public function addTagFavoriteQuick(Tag $tag)
795
+  {
796
+    $tags_favorites_quick = $this->getTagsFavoritesQuick();
797
+    if (!array_key_exists($tag->getId(), $tags_favorites_quick))
798
+    {
799
+      $tags_favorites_quick[$tag->getId()] = $tag->getName();
800
+    }
801
+    $this->setTagsFavoritesQuick($tags_favorites_quick);
802
+  }
803
+  
804
+  /**
805
+   * 
806
+   * @param \Muzich\CoreBundle\Entity\Tag $tag
807
+   */
808
+  public function removeTagFavoriteQuick(Tag $tag)
809
+  {
810
+    $tags_favorites_quick = $this->getTagsFavoritesQuick();
811
+    if (array_key_exists($tag->getId(), $tags_favorites_quick))
812
+    {
813
+      unset($tags_favorites_quick[$tag->getId()]);
814
+    }
815
+    $this->setTagsFavoritesQuick($tags_favorites_quick);
816
+  }
817
+  
760 818
 }

+ 19 - 1
src/Muzich/CoreBundle/Entity/UsersTagsFavorites.php Näytä tiedosto

@@ -10,6 +10,7 @@ use Doctrine\ORM\Mapping as ORM;
10 10
  * 
11 11
  * @ORM\Entity
12 12
  * @ORM\Table(name="users_tags_favorites")
13
+ * @ORM\HasLifecycleCallbacks
13 14
  */
14 15
 class UsersTagsFavorites
15 16
 {
@@ -42,7 +43,7 @@ class UsersTagsFavorites
42 43
    * L'attribut position permet de connaitre l'ordre de préfèrence de 
43 44
    * l'utilisateur.
44 45
    * 
45
-   * @ORM\Column(type="integer")
46
+   * @ORM\Column(type="integer", nullable=true)
46 47
    * @var type int
47 48
    */
48 49
   protected $position;
@@ -118,4 +119,21 @@ class UsersTagsFavorites
118 119
   {
119 120
     return $this->tag;
120 121
   }
122
+  
123
+  /**
124
+   * @ORM\prePersist
125
+   */
126
+  public function prePersist()
127
+  {
128
+    $this->getUser()->addTagFavoriteQuick($this->getTag());
129
+  }
130
+  
131
+  /**
132
+   * @ORM\preRemove
133
+   */
134
+  public function preRemove()
135
+  {
136
+    $this->getUser()->removeTagFavoriteQuick($this->getTag());
137
+  }
138
+  
121 139
 }

+ 83 - 0
src/Muzich/CoreBundle/Tests/User/UserTest.php Näytä tiedosto

@@ -0,0 +1,83 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\User;
4
+
5
+use Muzich\CoreBundle\lib\UnitTest;
6
+use Muzich\CoreBundle\Entity\UsersTagsFavorites;
7
+
8
+class TagReadTest extends UnitTest
9
+{  
10
+  
11
+  public function testTagsFavoritesQuick()
12
+  {
13
+    // On vérifie en premier lieu que les donnée en base corresponde bien a 
14
+    // ce que l'on veut avoir en fonction des fixtures (c'est une donnée calculé
15
+    // lors de la manipulation de UsersTagsFavorite
16
+    
17
+    $tribe   = $this->getTag('Tribe');
18
+    $hardtek   = $this->getTag('Hardtek');
19
+    $electro = $this->getTag('Electro');
20
+    $metal   = $this->getTag('Metal');
21
+    $metalco = $this->getTag('Metalcore');
22
+    $minimal = $this->getTag('Minimal');
23
+    $jungle  = $this->getTag('Jungle');
24
+    $melanco = $this->getTag('Melancolique');
25
+    $mellow  = $this->getTag('Mellow');
26
+    $melodiq = $this->getTag('Melodique');
27
+    
28
+    $bux = $this->getUser('bux');
29
+    $this->assertEquals(array(
30
+      $tribe->getId() => 'Tribe',
31
+      $electro->getId() => 'Electro',
32
+      $metal->getId() => 'Metal',
33
+      $minimal->getId() => 'Minimal',
34
+      $jungle->getId() => 'Jungle',
35
+      $hardtek->getId()  => 'Hardtek'
36
+    ), $bux->getTagsFavoritesQuick());
37
+    
38
+    $jean = $this->getUser('jean');
39
+    $this->assertEquals(array(
40
+      $melanco->getId() => 'Melancolique',
41
+      $mellow->getId() => 'Mellow',
42
+      $melodiq->getId() => 'Melodique',
43
+      $metal->getId() => 'Metal',
44
+      $metalco->getId() => 'Metalcore',
45
+      $minimal->getId() => 'Minimal'
46
+    ), $jean->getTagsFavoritesQuick());
47
+    
48
+    /*
49
+     * Si on effectue des modifs dans les tags favoris
50
+     */
51
+    
52
+    $bux_melodique = new UsersTagsFavorites();
53
+    $bux_melodique->setUser($bux);
54
+    $bux_melodique->setTag($melodiq);
55
+    $this->persist($bux_melodique);
56
+    $this->flush();
57
+    
58
+    $bux = $this->getUser('bux');
59
+    $this->assertEquals(array(
60
+      $tribe->getId() => 'Tribe',
61
+      $electro->getId() => 'Electro',
62
+      $metal->getId() => 'Metal',
63
+      $minimal->getId() => 'Minimal',
64
+      $jungle->getId() => 'Jungle',
65
+      $hardtek->getId()  => 'Hardtek',
66
+      $melodiq->getId()  => 'Melodique'
67
+    ), $bux->getTagsFavoritesQuick());
68
+    
69
+    $this->getDoctrine()->getEntityManager()->remove($bux_melodique);
70
+    $this->flush();
71
+    
72
+    $bux = $this->getUser('bux');
73
+    $this->assertEquals(array(
74
+      $tribe->getId() => 'Tribe',
75
+      $electro->getId() => 'Electro',
76
+      $metal->getId() => 'Metal',
77
+      $minimal->getId() => 'Minimal',
78
+      $jungle->getId() => 'Jungle',
79
+      $hardtek->getId()  => 'Hardtek'
80
+    ), $bux->getTagsFavoritesQuick());
81
+  }
82
+  
83
+}

+ 18 - 0
src/Muzich/CoreBundle/lib/UnitTest.php Näytä tiedosto

@@ -84,5 +84,23 @@ class UnitTest extends \PHPUnit_Framework_TestCase
84 84
       ->findOneByUsername($username)
85 85
     ;
86 86
   }
87
+
88
+
89
+  protected function getTag($name)
90
+  {
91
+    return $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
92
+      ->findOneByName($name)
93
+    ;
94
+  }
95
+  
96
+  protected function persist($entity)
97
+  {
98
+    $this->getDoctrine()->getEntityManager()->persist($entity);
99
+  }
100
+  
101
+  protected function flush()
102
+  {
103
+    $this->getDoctrine()->getEntityManager()->flush();
104
+  }
87 105
   
88 106
 }