Completed
Branch develop (e3b860)
by Luke
13:48
created

PackageCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 10
nc 1
nop 6
1
<?php
2
3
/**
4
 * Moodle component manager.
5
 *
6
 * @author Luke Carrier <[email protected]>
7
 * @copyright 2016 Luke Carrier
8
 * @license GPL-3.0+
9
 */
10
11
namespace ComponentManager\Command;
12
13
use ComponentManager\Console\Argument;
14
use ComponentManager\Moodle;
15
use ComponentManager\MoodleApi;
16
use ComponentManager\PackageFormat\PackageFormatFactory;
17
use ComponentManager\PackageRepository\PackageRepositoryFactory;
18
use ComponentManager\PackageSource\PackageSourceFactory;
19
use ComponentManager\PlatformUtil;
20
use ComponentManager\Task\PackageTask;
21
use Psr\Log\LoggerInterface;
22
use Symfony\Component\Console\Input\InputDefinition;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Filesystem\Filesystem;
27
28
/**
29
 * Package command.
30
 *
31
 * Assembles an entire Moodle instance from the specified project file, then
32
 * packages it in the specified format.
33
 */
34
class PackageCommand extends ProjectAwareCommand {
35
    /**
36
     * Help.
37
     *
38
     * @var string
39
     */
40
    const HELP = <<<HELP
41
Packages a Moodle site from a project file.
42
HELP;
43
44
    /**
45
     * Filesystem.
46
     *
47
     * @var \Symfony\Component\Filesystem\Filesystem
48
     */
49
    protected $filesystem;
50
51
    /**
52
     * Moodle.org plugin and update API.
53
     *
54
     * @var MoodleApi
55
     */
56
    protected $moodleApi;
57
58
    /**
59
     * Initialiser.
60
     *
61
     * @param \ComponentManager\MoodleApi              $moodleApi
62
     * @param \Symfony\Component\Filesystem\Filesystem $filesystem
63
     */
64
    public function __construct(PackageRepositoryFactory $packageRepositoryFactory,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 83 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
65
                                PackageSourceFactory $packageSourceFactory,
66
                                PackageFormatFactory $packageFormatFactory,
67
                                MoodleApi $moodleApi, Filesystem $filesystem,
68
                                LoggerInterface $logger) {
69
        $this->moodleApi  = $moodleApi;
70
        $this->filesystem = $filesystem;
71
72
        parent::__construct(
73
                $packageRepositoryFactory, $packageSourceFactory,
74
                $packageFormatFactory, $logger);
75
    }
76
77
    /**
78
     * @override \ComponentManager\Command\AbstractCommand
79
     */
80
    protected function configure() {
81
        $this
82
            ->setName('package')
83
            ->setDescription('Packages a Moodle site from a project file')
84
            ->setHelp(static::HELP)
85
            ->setDefinition(new InputDefinition([
86
                new InputOption(Argument::OPTION_PACKAGE_FORMAT, null,
87
                                InputOption::VALUE_REQUIRED,
88
                                Argument::OPTION_PACKAGE_FORMAT_HELP),
89
                new InputOption(Argument::OPTION_PROJECT_FILE, null,
90
                                InputOption::VALUE_REQUIRED,
91
                                Argument::OPTION_PROJECT_FILE_HELP),
92
                new InputOption(Argument::OPTION_PACKAGE_DESTINATION, null,
93
                                InputOption::VALUE_REQUIRED,
94
                                Argument::OPTION_PACKAGE_DESTINATION_HELP),
95
            ]));
96
    }
97
98
    /**
99
     * @override \ComponentManager\Command\AbstractCommand
100
     */
101
    protected function execute(InputInterface $input, OutputInterface $output) {
102
        $projectFilename = $input->getOption(Argument::OPTION_PROJECT_FILE);
103
        $packageDestination = PlatformUtil::expandPath(
104
                $input->getOption(Argument::OPTION_PACKAGE_DESTINATION));
105
        $packageFormat   = $input->getOption(Argument::OPTION_PACKAGE_FORMAT);
106
107
        $tempDirectory       = PlatformUtil::createTempDirectory();
108
        $archive             = $tempDirectory
109
                             . PlatformUtil::directorySeparator()
110
                             . 'moodle.zip';
111
        $destination         = $tempDirectory
112
                             . PlatformUtil::directorySeparator() . 'moodle';
113
        $projectLockFilename = $destination . PlatformUtil::directorySeparator()
114
                             . 'componentmgr.lock.json';
115
116
        $moodle  = new Moodle($destination);
117
        $project = $this->getProject($projectFilename, $projectLockFilename);
118
119
        $task = new PackageTask(
120
                $this->moodleApi, $project, $archive, $destination,
121
                $this->filesystem, $moodle, $packageFormat,
122
                $packageDestination);
123
        $task->execute($this->logger);
124
    }
125
}
126