PictureApplicationService::migrateAllPictures()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
namespace ParseServerMigration\Console;
4
5
use Parse\ParseObject;
6
use ParseServerMigration\Config;
7
8
class PictureApplicationService
9
{
10
    /**
11
     * @var PictureRepository
12
     */
13
    private $pictureRepository;
14
15
    /**
16
     * @param PictureRepository $pictureRepository
17
     */
18 2
    public function __construct(PictureRepository $pictureRepository)
19
    {
20 2
        $this->pictureRepository = $pictureRepository;
21 2
    }
22
23
    /**
24
     * @param ParseObject $picture
25
     *
26
     * @return string
27
     */
28 2
    public function migrateImage(ParseObject $picture) : string
29
    {
30 2
        $originalFileName = $picture->get(Config::PARSE_FILES_FIELD_NAME)->getName();
31 2
        $updateResult = $this->pictureRepository->renameImage($picture);
32
33 2
        if ($updateResult->getMatchedCount() != 1) {
34 1
            return 'Image : ['.$originalFileName.'] already migrated continuing';
35
        }
36
37 1
        $uploadResult = $this->pictureRepository->uploadImage($picture);
38
39 1
        return 'Migration success for: ['.$originalFileName.'] Uploaded to : ['.$uploadResult['ObjectURL'].']';
40
    }
41
42
    /**
43
     * @param ParseObject $picture
44
     *
45
     * @return string
46
     */
47
    public function migrateThumbnail(ParseObject $picture)
48
    {
49
        if ($picture->get(Config::PARSE_FILES_THUMBNAIL_FIELD_NAME) === null) {
50
            return 'No thumbnail for photo';
51
        }
52
53
        $originalFileName = $picture->get(Config::PARSE_FILES_THUMBNAIL_FIELD_NAME)->getName();
54
        $updateResult = $this->pictureRepository->renameThumbnail($picture);
55
56
        if ($updateResult->getMatchedCount() != 1) {
57
            return 'Thumbnail : ['.$originalFileName.'] already migrated continuing';
58
        }
59
60
        $uploadResult = $this->pictureRepository->uploadThumbnail($picture);
61
62
        return 'Migration success for: ['.$originalFileName.'] Uploaded to : ['.$uploadResult['ObjectURL'].']';
63
    }
64
65
    /**
66
     * @param int $limit
67
     * @param bool $orderDesc
68
     *
69
     * @return mixed
70
     */
71
    public function retrievePictures(int $limit, bool $orderDesc)
72
    {
73
        $images = [];
74
        foreach ($this->pictureRepository->findAllImages($limit, $orderDesc) as $batch) {
75
            $images = array_merge($images, $batch);
76
        }
77
78
        return $images;
79
    }
80
    /**
81
     * This will actually read from Parse server and insert data into a given MongoDB database.
82
     *
83
     * @return \MongoDB\InsertManyResult
84
     *
85
     * @throws \Exception
86
     */
87
    public function migrateAllPictures()
88
    {
89
        $insertResult = $this->pictureRepository->migrateAllPictures();
90
91
        if ($insertResult->getInsertedCount()) {
92
            throw new \Exception('An error occurred when inserting document into mongoDB');
93
        }
94
95
        return $insertResult;
96
    }
97
}
98