File size: 4,784 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php

namespace Mautic\CoreBundle\Update\Step;

use Mautic\CoreBundle\Exception\UpdateFailedException;
use Mautic\CoreBundle\Helper\PathsHelper;
use Mautic\CoreBundle\Helper\UpdateHelper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class InstallNewFilesStep implements StepInterface
{
    private ?ProgressBar $progressBar = null;

    private ?InputInterface $input = null;

    public function __construct(
        private TranslatorInterface $translator,
        private UpdateHelper $updateHelper,
        private PathsHelper $pathsHelper
    ) {
    }

    public function getOrder(): int
    {
        return 10;
    }

    public function shouldExecuteInFinalStage(): bool
    {
        return false;
    }

    /**
     * @throws UpdateFailedException
     */
    public function execute(ProgressBar $progressBar, InputInterface $input, OutputInterface $output): void
    {
        $this->progressBar = $progressBar;
        $this->input       = $input;

        $zipFile = $this->getZipPackage();

        $progressBar->setMessage($this->translator->trans('mautic.core.command.update.step.validate_update_package'));
        $progressBar->advance();

        $zipper = new \ZipArchive();
        $opened = $zipper->open($zipFile);

        $this->validateArchive($opened);

        // Extract the archive file now in place
        $progressBar->setMessage($this->translator->trans('mautic.core.update.step.extracting.package'));
        $progressBar->advance();

        if (!$zipper->extractTo($this->pathsHelper->getRootPath())) {
            throw new UpdateFailedException($this->translator->trans('mautic.core.update.error', ['%error%' => $this->translator->trans('mautic.core.update.error_extracting_package')]));
        }

        $zipper->close();
        @unlink($zipFile);
    }

    /**
     * @throws UpdateFailedException
     */
    private function getZipPackage(): string
    {
        if ($package = $this->input->getOption('update-package')) {
            if (!file_exists($package)) {
                throw new UpdateFailedException($this->translator->trans('mautic.core.update.archive_no_such_file'));
            }

            $this->progressBar->setMessage($this->translator->trans('mautic.core.command.update.step.loading_package').'                  ');
            $this->progressBar->advance();

            return $package;
        }

        $this->progressBar->setMessage($this->translator->trans('mautic.core.command.update.step.loading_update_information').'                  ');
        $this->progressBar->advance();

        $update = $this->updateHelper->fetchData();

        if (!isset($update['package'])) {
            throw new UpdateFailedException($this->translator->trans('mautic.core.update.no_cache_data'));
        }

        $this->progressBar->setMessage($this->translator->trans('mautic.core.command.update.step.download_update_package').'                  ');
        $this->progressBar->advance();

        // Fetch the update package
        $package = $this->updateHelper->fetchPackage($update['package']);

        if (isset($package['error']) && true === $package['error']) {
            throw new UpdateFailedException($this->translator->trans($package['message']));
        }

        return $this->pathsHelper->getCachePath().'/'.basename($update['package']);
    }

    /**
     * @param bool|string $opened
     *
     * @throws UpdateFailedException
     */
    private function validateArchive($opened): void
    {
        if (true === $opened) {
            return;
        }

        // Get the exact error
        switch ($opened) {
            case \ZipArchive::ER_EXISTS:
                $error = 'mautic.core.update.archive_file_exists';
                break;
            case \ZipArchive::ER_INCONS:
            case \ZipArchive::ER_INVAL:
            case \ZipArchive::ER_MEMORY:
                $error = 'mautic.core.update.archive_zip_corrupt';
                break;
            case \ZipArchive::ER_NOENT:
                $error = 'mautic.core.update.archive_no_such_file';
                break;
            case \ZipArchive::ER_NOZIP:
                $error = 'mautic.core.update.archive_not_valid_zip';
                break;
            case \ZipArchive::ER_READ:
            case \ZipArchive::ER_SEEK:
            case \ZipArchive::ER_OPEN:
            default:
                $error = 'mautic.core.update.archive_could_not_open';
                break;
        }

        throw new UpdateFailedException($this->translator->trans('mautic.core.update.error', ['%error%' => $this->translator->trans($error)]));
    }
}