Skip to content
Snippets Groups Projects
Commit 6aabe969 authored by Sebastien ANDRE's avatar Sebastien ANDRE
Browse files

hotline : #143141 : tools to clear unexist codif on album

parent 2e277242
Branches
Tags
No related merge requests found
- ticket #143141 : Administration : Album nettoyer les documents PNB pour supprimer les facettes associées à des codifications supprimées
\ No newline at end of file
......@@ -24,7 +24,13 @@ class Class_Migration_AlbumClearCodif {
protected
$_cat_id,
$_list_codif_status;
$_list_codif_status,
$_map_attrib_class = [
'genre' => Class_CodifGenre::class,
'sections' => Class_CodifSection::class,
'annexes' => Class_Bib::class,
'matiere' => Class_CodifMatiere::class
];
public function __construct($cat_id) {
$this->_cat_id = $cat_id;
......@@ -32,146 +38,108 @@ class Class_Migration_AlbumClearCodif {
}
public function run() {
public function run() : string {
try {
if (!$this->_cat_id
|| !$category = Class_AlbumCategorie::find($this->_cat_id))
return;
return "Category can't be found with id: " . $this->_cat_id;
$albums = Class_Album::findAllBy(['cat_id' => $category->getSousCategoriesIds()]);
array_map(function ($album) {
$this->_withAlbumDo($album);
}, $albums);
$albums = Class_Album::findAllBy(['cat_id' => $category
->getSousCategoriesIds()]);
(new Storm_Cache())->clean();
$count = 0;
foreach ($albums as $album)
$count += $this->_withAlbumDo($album);
return sprintf('Number of album updated: %s / %s, for category id: %s',
$count, count($albums), $this->_cat_id);
} catch (Exception $e) {
return;
return 'An exception has occured with stack trace: ' . $e;
}
}
protected function _withAlbumDo($album) {
$must_save = $this->_checkGenre($album);
$must_save |= $this->_checkSection($album);
$must_save |= $this->_checkAnnexe($album);
$must_save |= $this->_checkMatiere($album);
protected function _withAlbumDo(Class_Album $album) : int {
$has_changed = false;
foreach ($this->_map_attrib_class as $attrib => $class_name) {
$this->_checkCodif($album, $attrib, $class_name);
$has_changed |= $album->hasChangedAttribute($attrib);
}
if ($must_save) {
if ($has_changed) {
$album->save();
$album->index();
}
}
protected function _checkGenre($album) {
return $this->_checkCodif($album, 'genre', Class_CodifGenre::class);
}
protected function _checkSection($album) {
return $this->_checkCodif($album, 'sections', Class_CodifSection::class);
}
protected function _checkAnnexe($album) {
return $this->_checkCodif($album, 'annexes', Class_CodifAnnexe::class);
}
protected function _checkMatiere($album) {
return $this->_checkCodif($album, 'matiere', Class_CodifMatiere::class);
return $has_changed ? 1 : 0;
}
protected function _checkCodif($album, $attrib, $class_name) {
$is_updated = false;
$list_codif = [];
foreach (explode(';', $album->$attrib) as $codif_id)
$is_updated |= $this->_isUpdatedListCodif($class_name, $codif_id, $list_codif);
protected function _checkCodif(Class_Album $album,
string $attrib,
string $class_name) {
$list_codif = array_filter(explode(';', $album->$attrib),
function ($codif_id) use ($class_name) {
return $this->_existForClassName($class_name,
$codif_id);
});
if ($is_updated)
$album->$attrib = implode(';', $list_codif);
$album->$attrib = implode(';', $list_codif);
return $is_updated;
return $this;
}
protected function _isUpdatedListCodif($class_name, $codif_id, &$list_codif) {
$valid = $this->_existForClassName($class_name, $codif_id);
if ($valid)
$list_codif [] = $codif_id;
return !$valid;
}
protected function _existForClassName(string $class_name,
string $codif_id) : bool {
if (!$codif_id)
return false;
protected function _existForClassName($class_name, $codif_id) {
$codif_for_type = $this->_list_codif_status
->detect(function ($codif_status) use ($class_name, $codif_id) {
return $class_name == $codif_status->getClassName()
&& $codif_id == $codif_status->getCodifId();
});
if (!$codif_for_type)
$codif_for_type = $this->_addNewCodif($class_name, $codif_id);
if (!$codif_for_type) {
$codif_for_type = (new AlbumClearCodif_CodifStatus($class_name, $codif_id));
$this->_list_codif_status->add($codif_for_type);
}
return $codif_for_type->isExist();
}
protected function _addNewCodif($type, $codif_id) {
$new_codif = (new CodifStatus($type, $codif_id))->init();
if ($new_codif->isValid())
$this->_list_codif_status->add($new_codif);
return $new_codif;
}
}
class CodifStatus {
class AlbumClearCodif_CodifStatus {
protected
$_exist = false,
$_class_name,
$_codif_id;
public function __construct($class_name, $codif_id) {
public function __construct(string $class_name, string $codif_id) {
$this->_class_name = $class_name;
$this->_codif_id = $codif_id;
}
public function isValid() {
return $this->_class_name && $this->_codif_id;
}
public function init() {
if (!$this->isValid())
return $this;
$codif = $this->_class_name::find($this->_codif_id);
if ($codif)
$this->_exist = true;
return $this;
}
public function getClassName() {
public function getClassName() : string {
return $this->_class_name;
}
public function getCodifId() {
public function getCodifId() : string {
return $this->_codif_id;
}
public function isExist() {
public function isExist() : bool {
return $this->_exist;
}
}
<?php
require __DIR__ . '/../console.php';
(new Class_Migration_AlbumClearCodif($argv[1]))->run();
if (2 !== $argc) {
echo 'This script should have one argument, the category id' . "\n\n";
return;
}
echo (new Class_Migration_AlbumClearCodif($argv[1]))->run();
echo "\n\n";
......@@ -23,7 +23,9 @@
/* hotline: #143141 */
class AlbumClearCodifTest extends ModelTestCase {
protected $_storm_default_to_volatile = true;
protected
$_storm_default_to_volatile = true,
$_message;
public function setUp() {
parent::setUp();
......@@ -32,11 +34,11 @@ class AlbumClearCodifTest extends ModelTestCase {
['id' => 1,
'libelle' => 'Adulte'
]);
$this->fixture(Class_CodifAnnexe::class,
$this->fixture(Class_Bib::class,
['id' => 4,
'libelle' => 'Biviers'
]);
$this->fixture(Class_CodifAnnexe::class,
$this->fixture(Class_Bib::class,
['id' => 7,
'libelle' => 'Froges'
]);
......@@ -68,7 +70,14 @@ class AlbumClearCodifTest extends ModelTestCase {
'genre' => '1;5',
'sections' => '2',
'annexes' => '4;6;7',
'matiere' => '8;9;10;11;12',
'matiere' => '8;9;;10;11;12',
'titre' => 'Amuse-bouche'
]);
$this->fixture(Class_Album::class,
['id' => 202,
'cat_id' => 100,
'annexes' => '4;;7',
'titre' => 'Amuse-bouche'
]);
$this->fixture(Class_Album::class,
......@@ -77,11 +86,18 @@ class AlbumClearCodifTest extends ModelTestCase {
'genre' => '1;5',
'sections' => '2',
'annexes' => '4;6;7',
'matiere' => '8;9;10;11;12',
'matiere' => '8;9;;10;11;12',
'titre' => 'Animal'
]);
(new Class_Migration_AlbumClearCodif(100))->run();
$this->_message = (new Class_Migration_AlbumClearCodif(100))->run();
}
/** @test */
public function withCatId100ShouldResponseMessageNumberOfAlbumUpdated2() {
$this->assertEquals('Number of album updated: 2 / 2, for category id: 100',
$this->_message);
}
......@@ -121,6 +137,12 @@ class AlbumClearCodifTest extends ModelTestCase {
}
/** @test */
public function album202ShouldHaveLostAnnexeEmptyId() {
$this->assertEquals('4;7', Class_Album::find(202)->getAnnexes());
}
/** @test */
public function album200ShouldHaveLostMatiereId10And11() {
$this->assertEquals('8;9;12', Class_Album::find(200)->getMatiere());
......@@ -129,7 +151,28 @@ class AlbumClearCodifTest extends ModelTestCase {
/** @test */
public function album201ShouldNotHaveLostMatiereIds() {
$this->assertEquals('8;9;10;11;12', Class_Album::find(201)->getMatiere());
$this->assertEquals('8;9;;10;11;12', Class_Album::find(201)->getMatiere());
}
}
class AlbumClearCodifWithErrorsTest extends ModelTestCase {
protected
$_storm_default_to_volatile = true,
$_message;
public function setUp() {
parent::setUp();
$this->_message = (new Class_Migration_AlbumClearCodif(100))->run();
}
/** @test */
public function withCatId100ShouldResponseMessageCategoryCantBeFound() {
$this->assertEquals("Category can't be found with id: 100", $this->_message);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment