Passed
Push — master ( 240c83...03f39b )
by Mathias
06:41
created

Version36::addProcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yawik\Migration\Migrator;
6
7
8
use Auth\Entity\User;
9
use Auth\Service\UploadHandler;
10
use Doctrine\ODM\MongoDB\DocumentManager;
11
use MongoDB\Database;
12
use Psr\Container\ContainerInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Yawik\Migration\Contracts\ProcessorInterface;
15
use Yawik\Migration\Contracts\MigratorInterface;
16
use Yawik\Migration\Migrator\Version36\OrganizationProcessor;
17
use Yawik\Migration\Migrator\Version36\FileProcessor;
18
19
/**
20
 * Migrate old yawik database to 0.36
21
 *
22
 * @package Yawik\Migration
23
 */
24
class Version36 implements MigratorInterface
25
{
26
    /**
27
     * @var DocumentManager
28
     */
29
    private DocumentManager $dm;
30
31
    private Database $db;
32
33
    /**
34
     * @var OutputInterface
35
     */
36
    private OutputInterface $output;
37
38
    /**
39
     * @var iterable|ProcessorInterface[]
40
     */
41
    private iterable $processors;
42
43
    public function __construct(
44
        DocumentManager $dm,
45
        OutputInterface $output
46
    )
47
    {
48
        $this->dm = $dm;
49
        $this->db = $dm->getDocumentDatabase(User::class);
50
        $this->output = $output;
51
    }
52
53
    public static function factory(ContainerInterface $container)
54
    {
55
        $dm = $container->get(DocumentManager::class);
56
        $output = $container->get(OutputInterface::class);
57
58
        $migrator = new self($dm, $output);
59
        $migrator
60
            ->createFileProcessor(
61
            'applications'
62
            )
63
            ->createFileProcessor(
64
            'cvs.attachments',
65
            )
66
            ->createFileProcessor(
67
                'cvs.contact.images',
68
            )
69
            ->createFileProcessor(
70
                'organizations.images'
71
            )
72
            ->createFileProcessor(
73
                'users.images',
74
            )
75
        ;
76
        $migrator->addProcessor(new OrganizationProcessor($dm, $output));
77
        return $migrator;
78
    }
79
80
    public function createFileProcessor(
81
        string $bucketName
82
    )
83
    {
84
        $this->addProcessor(new FileProcessor(
85
            $this->dm,
86
            $this->output,
87
            $bucketName,
88
        ));
89
90
        return $this;
91
    }
92
93
    public function addProcessor(ProcessorInterface $processor)
94
    {
95
        $this->processors[] = $processor;
96
    }
97
98
    public function getDescription(): string
99
    {
100
        return "Migrate Older Yawik (version<=0.36)";
101
    }
102
103
    public function version(): string
104
    {
105
        return "0.36.0";
106
    }
107
108
    /**
109
     * @return bool
110
     * @throws \Exception
111
     */
112
    public function migrate(): bool
113
    {
114
        $status = true;
115
        foreach($this->processors as $processor){
116
            try{
117
                $cStat = $processor->process();
118
            }catch (\Exception $exception){
119
                $cStat = false;
120
            }
121
            if(false === $cStat){
122
                $status = false;
123
            }
124
        }
125
        return $status;
126
    }
127
}