MigrateFromSaasCommand::execute()   B
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 30
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 25
nc 2
nop 2
crap 12
1
<?php
2
3
namespace ParseServerMigration\Console\Command;
4
5
use Monolog\Logger;
6
use Parse\ParseObject;
7
use ParseServerMigration\Config;
8
use ParseServerMigration\Console\PictureApplicationService;
9
use ParseServerMigration\Console\PictureRepository;
10
use Parse\ParseQuery;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
16
/**
17
 * @author Maxence Dupressoir <[email protected]>
18
 * @copyright 2016 Meetic
19
 */
20
class MigrateFromSaasCommand extends Command
21
{
22
    /**
23
     * @var PictureApplicationService
24
     */
25
    private $pictureApplicationService;
26
27
    /**
28
     * @var Logger
29
     */
30
    private $logger;
31
32
    /**
33
     * @param PictureApplicationService $pictureApplicationService
34
     * @param Logger                    $logger
35
     */
36
    public function __construct(PictureApplicationService $pictureApplicationService, Logger $logger)
37
    {
38
        $this->pictureApplicationService = $pictureApplicationService;
39
        $this->logger = $logger;
40
41
        parent::__construct();
42
    }
43
44
    protected function configure()
45
    {
46
        $this
47
            ->setName('parse:migration:migrate')
48
            ->setDescription('Fetch existing SAAS Parse server and insert all data into MongoDB')
49
        ;
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $io = new SymfonyStyle($input, $output);
55
56
        $query = new ParseQuery(Config::PARSE_FILES_CLASS_NAME);
57
58
        $io->progressStart($query->count() * 2);
59
60
        //This is crap but we can't count other wise
61
        $query = new ParseQuery(Config::PARSE_FILES_CLASS_NAME);
62
63
        try {
64
            $this->pictureApplicationService->migrateAllPictures();
65
        } catch (\ErrorException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
66
        }
67
68
        //Todo we need to compare perf between this way of dumping all images vs PictureRepository::migrateAllPictures()
69
        $query->each(function (ParseObject $picture) use ($io) {
70
            try {
71
                $io->text('Migrating picture\'s image');
72
                $message = $this->pictureApplicationService->migrateImage($picture);
73
                $io->progressAdvance(1);
74
                $io->newLine();
75
                $this->logger->info($message);
76
                $io->success($message);
77
78
                $io->text('Migrate picture\'s thumbnail');
79
                $message = $this->pictureApplicationService->migrateThumbnail($picture);
80
                $io->progressAdvance(1);
81
                $this->logger->info($message);
82
                $io->success($message);
83
            } catch (\ErrorException $exception) {
84
                $message = 'Upload failed for: ['.$picture->get(Config::PARSE_FILES_FIELD_NAME)->getName().'] \nDetail error : ['.$exception->getMessage().']';
85
86
                $this->logger->error($message);
87
                $io->warning($message);
88
            }
89
        });
90
    }
91
}
92