From 5312744f7ea2a2900b3ff633854f913371050998 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Thu, 3 Sep 2015 10:56:06 +0200
Subject: [PATCH] #26993 refacto

---
 .../php/classes/classe_notice_integration.php | 90 ++++++++++---------
 1 file changed, 50 insertions(+), 40 deletions(-)

diff --git a/cosmogramme/php/classes/classe_notice_integration.php b/cosmogramme/php/classes/classe_notice_integration.php
index 50630811d48..19383f681d2 100644
--- a/cosmogramme/php/classes/classe_notice_integration.php
+++ b/cosmogramme/php/classes/classe_notice_integration.php
@@ -943,51 +943,61 @@ class notice_integration {
 		if( 1 == $this->notice["statut"])
       return $this->deleteRecordArticles();
 
-		$clef_chapeau = $this->notice["clef_chapeau"];
-		$clef_numero = $this->notice["clef_numero"];
-		$clef_article = $this->notice["clef_article"];
-		$clef_unimarc = $this->notice["clef_unimarc"];
-
-    $titre_principal = $this->notice['titre_princ'];
-    $clef_titre = hash('crc32b', $titre_principal);
-
-		$enreg["clef_chapeau"] = $clef_chapeau;
-		$enreg["clef_numero"] = $clef_numero;
-		$enreg["clef_unimarc"] = $clef_unimarc ? $clef_unimarc : $clef_titre;
-		$enreg["unimarc"] = $this->notice["unimarc"];
-		$enreg["date_maj"] = dateDuJour(0);
-		$enreg["qualite"] = $this->qualite_bib;
-		$enreg["clef_article"] = $clef_article;
-
-		if($clef_unimarc)	{
-			if ($article = Class_Notice_SerialArticles::findFirstBy(['clef_unimarc' => $clef_unimarc])) {
-				$enreg["clef_chapeau"] = $article->getClefChapeau();
-				$enreg["clef_numero"] = $article->getClefNumero();
-			}
-		}	else {
-			if(!$article = Class_Notice_SerialArticles::findFirstBy(['clef_chapeau' => $clef_chapeau,
-                                                               'clef_numero' => $clef_numero,
-                                                               'clef_unimarc' => $clef_titre]))
-        $article = Class_Notice_SerialArticles::findFirstBy(['clef_chapeau' => $clef_chapeau,
-                                                             'clef_numero' => $clef_numero,
-                                                             'clef_article' => $clef_article,
-                                                             'clef_unimarc' => '']);
+    $title_key = hash('crc32b', $this->notice['titre_princ']);
+
+    $unimarc_key = $this->notice["clef_unimarc"]
+      ? $this->notice["clef_unimarc"]
+      : $title_key;
+
+    $article_from_record = Class_Notice_SerialArticles::newInstance(['clef_chapeau' => $this->notice["clef_chapeau"],
+                                                                     'clef_numero' => $this->notice["clef_numero"],
+                                                                     'clef_unimarc' => $unimarc_key,
+                                                                     'unimarc' => $this->notice['unimarc'],
+                                                                     'date_maj' => dateDuJour(0),
+                                                                     'qualite' => $this->qualite_bib,
+                                                                     'clef_article' => $this->notice['clef_article']]);
+
+    if(!$this->notice['clef_unimarc']) {
+      $common_attribs = ['clef_chapeau' => $this->notice['clef_chapeau'],
+                         'clef_numero' => $this->notice['clef_numero']];
+
+			if(!$article = Class_Notice_SerialArticles::findFirstBy(array_merge($common_attribs, ['clef_unimarc' => $title_key])))
+        $article = Class_Notice_SerialArticles::findFirstBy(array_merge($common_attribs, ['clef_article' => $this->notice['clef_article'],
+                                                                                          'clef_unimarc' => '']));
+      return $this->updateOrInsertArticle($article, $article_from_record);
     }
 
-		if($article) {
-			// remplacer si qualite superieure ou egale
-			if($enreg["qualite"] >= $article->getQualite())	{
-				$article->updateAttributes($enreg)->save();
-				$this->statut=5;
-			}
-		}	else {
-			Class_Notice_SerialArticles::newInstance($enreg)->save();
-			Class_Notice_SerialArticles::clearCache();
-			$this->statut=1;
-		}
+    if ($article = Class_Notice_SerialArticles::findFirstBy(['clef_unimarc' => $this->notice['clef_unimarc']])) {
+				$article_from_record->setClefChapeau($article->getClefChapeau());
+				$article_from_record->setClefNumero($article->getClefNumero());
+        return $this->updateOrInsertArticle($article, $article_from_record);
+    }
 	}
 
 
+  protected function updateOrInsertArticle($article, $article_from_record) {
+    return $article
+      ? $this->updateArticleWith($article, $article_from_record)
+      : $this->insertArticleWith($article_from_record);
+  }
+
+
+  protected function insertArticleWith($article_from_record) {
+		Class_Notice_SerialArticles::newInstance($article_from_record->toArray())->save();
+    Class_Notice_SerialArticles::clearCache();
+    $this->statut = RECORD_INSERT;
+    return;
+  }
+
+
+  protected function updateArticleWith($article, $article_from_record) {
+    if($enreg["qualite"] >= $article->getQualite())	{
+      $article->updateAttributes($article_from_record->toArray())->save();
+      $this->statut = RECORD_RENEW;
+    }
+  }
+
+
   protected function deleteRecordArticles() {
     $this->statut = self::RECORD_DELETE;
 
-- 
GitLab