ProjectAwareCommand::getMoodle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 12
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\Moodle;
14
use ComponentManager\PackageFormat\PackageFormatFactory;
15
use ComponentManager\PackageRepository\PackageRepositoryFactory;
16
use ComponentManager\PackageSource\PackageSourceFactory;
17
use ComponentManager\Platform\Platform;
18
use ComponentManager\Project\Project;
19
use ComponentManager\Project\ProjectFile;
20
use ComponentManager\Project\ProjectLockFile;
21
use Psr\Log\LoggerInterface;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Filesystem\Filesystem;
24
25
/**
26
 * Project-aware command.
27
 *
28
 * Provides helpful utility methods for accessing the project in the current
29
 * working directory. Commands requiring interaction with the Moodle instance
30
 * being processed should extend this class.
31
 */
32
abstract class ProjectAwareCommand extends Command {
33
    /**
34
     * Project filename.
35
     *
36
     * @var string
37
     */
38
    const PROJECT_FILENAME = 'componentmgr.json';
39
40
    /**
41
     * Project lock filename.
42
     *
43
     * @var string
44
     */
45
    const PROJECT_LOCK_FILENAME = 'componentmgr.lock.json';
46
47
    /**
48
     * Filesystem.
49
     *
50
     * @var Filesystem
51
     */
52
    protected $filesystem;
53
54
    /**
55
     * Logger.
56
     *
57
     * @var LoggerInterface
58
     */
59
    protected $logger;
60
61
    /**
62
     * Moodle bridge.
63
     *
64
     * @var Moodle
65
     */
66
    protected $moodle;
67
68
    /**
69
     * Package format factory.
70
     *
71
     * @var PackageFormatFactory
72
     */
73
    protected $packageFormatFactory;
74
75
    /**
76
     * Package repository factory.
77
     *
78
     * @var PackageRepositoryFactory
79
     */
80
    protected $packageRepositoryFactory;
81
82
    /**
83
     * Package source factory.
84
     *
85
     * @var PackageSourceFactory
86
     */
87
    protected $packageSourceFactory;
88
89
    /**
90
     * Project.
91
     *
92
     * Lazily loaded -- be sure to call getProject() in order to ensure the
93
     * value is defined.
94
     *
95
     * @var Project
96
     */
97
    protected $project;
98
99
    /**
100
     * Platform support library.
101
     *
102
     * @var Platform
103
     */
104
    protected $platform;
105
106
    /**
107
     * Initialiser.
108
     *
109
     * @param PackageRepositoryFactory $packageRepositoryFactory
110
     * @param PackageSourceFactory     $packageSourceFactory
111
     * @param PackageFormatFactory     $packageFormatFactory
112
     * @param Platform                 $platform
113
     * @param Filesystem               $filesystem
114
     * @param LoggerInterface          $logger
115
     */
116
    public function __construct(PackageRepositoryFactory $packageRepositoryFactory,
117
                                PackageSourceFactory $packageSourceFactory,
118
                                PackageFormatFactory $packageFormatFactory,
119
                                Platform $platform, Filesystem $filesystem,
120
                                LoggerInterface $logger) {
121
        $this->packageRepositoryFactory = $packageRepositoryFactory;
122
        $this->packageSourceFactory     = $packageSourceFactory;
123
        $this->packageFormatFactory     = $packageFormatFactory;
124
125
        $this->filesystem = $filesystem;
126
        $this->platform   = $platform;
127
128
        $this->logger = $logger;
129
130
        parent::__construct();
131
    }
132
133
    /**
134
     * Get the Moodle bridge.
135
     *
136
     * @param string|null $moodleDirectory
137
     *
138
     * @return Moodle
139
     */
140
    protected function getMoodle($moodleDirectory=null) {
141
        if ($this->moodle === null) {
142
            $moodleDirectory = ($moodleDirectory === null)
143
                    ? $this->platform->getWorkingDirectory() : $moodleDirectory;
144
145
            $this->moodle = new Moodle($moodleDirectory, $this->platform);
146
        }
147
148
        return $this->moodle;
149
    }
150
151
    /**
152
     * Get project.
153
     *
154
     * @param string|null $projectFilename
155
     * @param string|null $projectLockFilename
156
     *
157
     * @return Project
158
     */
159
    protected function getProject($projectFilename=null, $projectLockFilename=null) {
160
        $workingDirectory = $this->platform->getWorkingDirectory();
161
162
        if ($this->project === null) {
163 View Code Duplication
            if ($projectFilename === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
                $projectFilename = $this->platform->joinPaths([
165
                    $workingDirectory,
166
                    static::PROJECT_FILENAME,
167
                ]);
168
            } else {
169
                $projectFilename = $this->platform->expandPath(
170
                        $projectFilename);
171
            }
172
173 View Code Duplication
            if ($projectLockFilename === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
                $projectLockFilename = $this->platform->joinPaths([
175
                    $workingDirectory,
176
                    static::PROJECT_LOCK_FILENAME,
177
                ]);
178
            } else {
179
                $projectLockFilename = $this->platform->expandPath(
180
                        $projectLockFilename);
181
            }
182
183
            $this->logger->info('Parsing project file', [
184
                'filename' => $projectFilename,
185
            ]);
186
            $this->project = new Project(
187
                    new ProjectFile($projectFilename),
188
                    new ProjectLockFile($projectLockFilename),
189
                    $this->packageRepositoryFactory,
190
                    $this->packageSourceFactory,
191
                    $this->packageFormatFactory);
192
        }
193
194
        return $this->project;
195
    }
196
}
197