1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yawik\Migration\Migrator\Version36; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
9
|
|
|
use MongoDB\BSON\ObjectId; |
10
|
|
|
use MongoDB\Collection; |
11
|
|
|
use Organizations\Entity\Organization; |
12
|
|
|
use Organizations\Entity\OrganizationImage; |
13
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Yawik\Migration\Contracts\ProcessorInterface; |
16
|
|
|
use Yawik\Migration\Util\MongoUtilTrait; |
17
|
|
|
|
18
|
|
|
class OrganizationProcessor implements ProcessorInterface |
19
|
|
|
{ |
20
|
|
|
use MongoUtilTrait; |
21
|
|
|
|
22
|
|
|
private OutputInterface $output; |
23
|
|
|
private Collection $collection; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
DocumentManager $dm, |
27
|
|
|
OutputInterface $output |
28
|
|
|
) |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
$database = $dm->getDocumentDatabase(Organization::class); |
32
|
|
|
$this->collection = $database->selectCollection('organizations'); |
33
|
|
|
$this->output = $output; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function process(): bool |
37
|
|
|
{ |
38
|
|
|
$col = $this->collection; |
39
|
|
|
|
40
|
|
|
$count = $col->countDocuments(); |
41
|
|
|
$progressBar = new ProgressBar($this->output, $count); |
42
|
|
|
$progressBar->setFormat('<info>processing document </info><comment>organizations</comment> [%current%/%max%]'); |
43
|
|
|
$progressBar->start(); |
44
|
|
|
|
45
|
|
|
$status = true; |
46
|
|
|
foreach($col->find() as $current){ |
47
|
|
|
$progressBar->advance(); |
48
|
|
|
$val = $this->getNamespacedValue('images.images', $current); |
49
|
|
|
if(!is_null($val)){ |
50
|
|
|
$col->updateOne( |
51
|
|
|
['_id' => $current['_id']], |
52
|
|
|
[ |
53
|
|
|
'$set' => [ |
54
|
|
|
'images.images' => $this->processImages($val) |
55
|
|
|
] |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$progressBar->finish(); |
62
|
|
|
return $status; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function processImages(array $images) |
66
|
|
|
{ |
67
|
|
|
$newImages = []; |
68
|
|
|
foreach($images as $image){ |
69
|
|
|
if(!isset($image['$ref'])){ |
70
|
|
|
$oid = null; |
71
|
|
|
if(is_string($image)){ |
72
|
|
|
$oid = new ObjectId($image); |
73
|
|
|
} |
74
|
|
|
if(!is_null($oid)){ |
75
|
|
|
$newImages[] = [ |
76
|
|
|
'$ref' => 'organizations.images.files', |
77
|
|
|
'$id' => $oid, |
78
|
|
|
'_entity' => OrganizationImage::class |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
}else{ |
82
|
|
|
$image['$ref'] = 'organizations.images.files'; |
83
|
|
|
$image['_entity'] = OrganizationImage::class; |
84
|
|
|
$newImages[] = $image; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return $newImages; |
88
|
|
|
} |
89
|
|
|
} |