Passed
Pull Request — main (#12)
by Dante
01:09
created

ImportProjectCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
eloc 14
c 2
b 1
f 0
dl 0
loc 36
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 27 4
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2024 ChannelWeb Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace BEdita\ImportTools\Command;
16
17
use BEdita\ImportTools\Utility\Project;
18
use Cake\Command\Command;
19
use Cake\Console\Arguments;
20
use Cake\Console\ConsoleIo;
21
use Cake\Datasource\ConnectionManager;
22
23
/**
24
 * Command to prepare `applications` and `users` to import a project in a new environment.
25
 * Since applications api keys and users passwords will generally differ this command will try to
26
 * sync those values, when possible, in order to enable migration.
27
 *
28
 * The new project database you want to import must be reachable via an `import` key Datasource configuration.
29
 * This new project will be modified this way:
30
 *  - new project `applications` must be present as name on the current project DB => current api keys are saved in the new imported project
31
 *  - users password hashes are changed (if set) in the new imported project using current users password hashes on records with the same `username`
32
 *
33
 * After this command is finished the new imported project can be used in the current environment and replace current project/database.
34
 */
35
class ImportProjectCommand extends Command
36
{
37
    /**
38
     * {@inheritDoc}
39
     *
40
     * Main command execution:
41
     * - applications and users are loaded from current and imported project
42
     * - applications api keys and users passwords are updated to be used in current environment
43
     */
44
    public function execute(Arguments $args, ConsoleIo $io)
45
    {
46
        $io->out('Start');
47
        $project = new Project();
48
49
        // check connection
50
        if (!$project->checkDatasourceConfig($io)) {
51
            $this->abort();
52
        }
53
54
        $importConnection = ConnectionManager::get('import');
55
        $defaultConnection = ConnectionManager::get('default');
56
57
        // review `applications`
58
        if ($project->reviewApplications($importConnection, $defaultConnection, $io) === false) {
59
            $this->abort();
60
        }
61
62
        // review `users`
63
        if ($project->reviewUsers($importConnection, $defaultConnection, $io) === false) {
64
            $this->abort();
65
        }
66
67
        $io->success('Import project done.');
68
        $io->out('End');
69
70
        return static::CODE_SUCCESS;
71
    }
72
}
73