Browse Source

Refactorisation de addTag

bastien 12 years ago
parent
commit
7bf9742322

+ 8 - 63
src/Muzich/CoreBundle/Controller/CoreController.php View File

@@ -13,8 +13,8 @@ use Muzich\CoreBundle\Entity\Element;
13 13
 use Symfony\Component\HttpFoundation\RedirectResponse;
14 14
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
15 15
 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
16
-use Muzich\CoreBundle\Util\StrictCanonicalizer;
17 16
 use Muzich\CoreBundle\Entity\Tag;
17
+use Muzich\CoreBundle\Managers\TagManager;
18 18
 
19 19
 class CoreController extends Controller
20 20
 {
@@ -370,69 +370,14 @@ class CoreController extends Controller
370 370
       }
371 371
     }
372 372
     
373
-    $canonicalizer = new StrictCanonicalizer();
374
-    $name_canonicalized = $canonicalizer->canonicalize($name);
373
+    $tagManager = new TagManager();
374
+    $tag = $tagManager->addTag($this->getDoctrine(), $name, $this->getUser(), $arguments);
375 375
     
376
-    // Check avant de commencer: On regarde si ce tag n'existe pas déjà en tag
377
-    // public (en cas de gruge)
378
-    if (($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
379
-      ->findOneBy(array(
380
-        'slug'       => $name_canonicalized,
381
-        'tomoderate' => false
382
-      ))))
383
-    {
384
-      // Si il existe déjà pas besoin de l'ajouter on retourne l'id et ne nom
385
-      return $this->jsonResponse(array(
386
-        'status'   => 'success',
387
-        'tag_id'   => $tag->getId(),
388
-        'tag_name' => $tag->getName()
389
-      ));
390
-    }
391
-    
392
-    // Première étape, on regarde en base si quelqu'un a pas déjà ajouté ce tag
393
-    if (($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
394
-      ->findOneBy(array(
395
-        'slug'       => $name_canonicalized,
396
-        'tomoderate' => true
397
-      ))))
398
-    {
399
-      // Si il existe déjà pas besoin de l'ajouter on retourne l'id et ne nom
400
-      // après avoir ajouté cet utilisateurs a la liste de ceux pouvant le voir
401
-      $privatesids = json_decode($tag->getPrivateids());
402
-      if (!in_array($this->getUserId(), $privatesids))
403
-      {
404
-        $privatesids[] = (string)$this->getUserId();
405
-      }
406
-      $tag->setPrivateids(json_encode($privatesids));
407
-      $tag->setArguments($tag->getArguments(). " ****" . $this->getUser()->getName()."****: " .$arguments);
408
-      
409
-      $this->getDoctrine()->getEntityManager()->persist($tag);
410
-      $this->getDoctrine()->getEntityManager()->flush();
411
-      
412
-      return $this->jsonResponse(array(
413
-        'status'   => 'success',
414
-        'tag_id'   => $tag->getId(),
415
-        'tag_name' => $tag->getName()
416
-      ));
417
-    }
418
-    else
419
-    {
420
-      // Sinon on l'ajoute en base
421
-      $tag = new Tag();
422
-      $tag->setName(ucfirst(strtolower($name)));
423
-      $tag->setTomoderate(true);
424
-      $tag->setPrivateids(json_encode(array((string)$this->getUserId())));
425
-      $tag->setArguments(" ****" . $this->getUser()->getName()."****: " .$arguments);
426
-      
427
-      $this->getDoctrine()->getEntityManager()->persist($tag);
428
-      $this->getDoctrine()->getEntityManager()->flush();
429
-      
430
-      return $this->jsonResponse(array(
431
-        'status'   => 'success',
432
-        'tag_id'   => $tag->getId(),
433
-        'tag_name' => $tag->getName()
434
-      ));
435
-    }
376
+    return $this->jsonResponse(array(
377
+      'status'   => 'success',
378
+      'tag_id'   => $tag->getId(),
379
+      'tag_name' => $tag->getName()
380
+    ));
436 381
   }
437 382
   
438 383
 }

+ 65 - 2
src/Muzich/CoreBundle/Managers/TagManager.php View File

@@ -6,6 +6,8 @@ use Muzich\CoreBundle\Entity\Tag;
6 6
 use Doctrine\ORM\EntityManager;
7 7
 use Symfony\Component\DependencyInjection\Container;
8 8
 use FOS\UserBundle\Util\CanonicalizerInterface;
9
+use Symfony\Bundle\DoctrineBundle\Registry;
10
+use Muzich\CoreBundle\Util\StrictCanonicalizer;
9 11
 
10 12
 /**
11 13
  * 
@@ -17,9 +19,16 @@ class TagManager
17 19
   
18 20
   protected $nameCanonicalizer;
19 21
   
20
-  public function __construct(CanonicalizerInterface $nameCanonicalizer)
22
+  public function __construct(CanonicalizerInterface $nameCanonicalizer = null)
21 23
   {
22
-    $this->nameCanonicalizer = $nameCanonicalizer;
24
+    if ($nameCanonicalizer)
25
+    {
26
+      $this->nameCanonicalizer = $nameCanonicalizer;
27
+    }
28
+    else
29
+    {
30
+      $this->nameCanonicalizer = new StrictCanonicalizer();
31
+    }
23 32
   }
24 33
   
25 34
   public function updateSlug(Tag $tag)
@@ -27,4 +36,58 @@ class TagManager
27 36
     $tag->setSlug($this->nameCanonicalizer->canonicalize($tag->getName()));
28 37
   }
29 38
   
39
+  public function addTag(Registry $doctrine, $name, $user, $arguments)
40
+  {
41
+    $name_canonicalized = $this->nameCanonicalizer->canonicalize(trim($name));
42
+    
43
+    // Check avant de commencer: On regarde si ce tag n'existe pas déjà en tag
44
+    // public (en cas de gruge)
45
+    if (($tag = $doctrine->getRepository('MuzichCoreBundle:Tag')
46
+      ->findOneBy(array(
47
+        'slug'       => $name_canonicalized,
48
+        'tomoderate' => false
49
+      ))))
50
+    {
51
+      // Si il existe déjà pas besoin de l'ajouter
52
+      return $tag;
53
+    }
54
+    
55
+    // Première étape, on regarde en base si quelqu'un a pas déjà ajouté ce tag
56
+    if (($tag = $doctrine->getRepository('MuzichCoreBundle:Tag')
57
+      ->findOneBy(array(
58
+        'slug'       => $name_canonicalized,
59
+        'tomoderate' => true
60
+      ))))
61
+    {
62
+      // Si il existe déjà pas besoin de l'ajouter ou le retourne
63
+      // après avoir ajouté cet utilisateurs a la liste de ceux pouvant le voir
64
+      $privatesids = json_decode($tag->getPrivateids());
65
+      if (!in_array($user->getId(), $privatesids))
66
+      {
67
+        $privatesids[] = (string)$user->getId();
68
+      }
69
+      $tag->setPrivateids(json_encode($privatesids));
70
+      $tag->setArguments($tag->getArguments(). " ****" . $user->getName()."****: " .$arguments);
71
+      
72
+      $doctrine->getEntityManager()->persist($tag);
73
+      $doctrine->getEntityManager()->flush();
74
+      
75
+      return $tag;
76
+    }
77
+    else
78
+    {
79
+      // Sinon on l'ajoute en base
80
+      $tag = new Tag();
81
+      $tag->setName(ucfirst(strtolower($name)));
82
+      $tag->setTomoderate(true);
83
+      $tag->setPrivateids(json_encode(array((string)$user->getId())));
84
+      $tag->setArguments(" ****" . $user->getName()."****: " .$arguments);
85
+      
86
+      $doctrine->getEntityManager()->persist($tag);
87
+      $doctrine->getEntityManager()->flush();
88
+      
89
+      return $tag;
90
+    }
91
+  }
92
+  
30 93
 }