Completed
Push — master ( ed845b...38c1c8 )
by Luke
04:37
created

ProjectAwareCommand::getProject()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 37
Code Lines 26

Duplication

Lines 18
Ratio 48.65 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 18
loc 37
ccs 0
cts 22
cp 0
rs 8.5806
cc 4
eloc 26
nc 5
nop 2
crap 20
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 \Symfony\Component\Filesystem\Filesystem
51
     */
52
    protected $filesystem;
53
54
    /**
55
     * Logger.
56
     *
57
     * @var \Psr\Log\LoggerInterface
58
     */
59
    protected $logger;
60
61
    /**
62
     * Moodle bridge.
63
     *
64
     * @var \ComponentManager\Moodle
65
     */
66
    protected $moodle;
67
68
    /**
69
     * Package format factory.
70
     *
71
     * @var \ComponentManager\PackageFormat\PackageFormatFactory
72
     */
73
    protected $packageFormatFactory;
74
75
    /**
76
     * Package repository factory.
77
     *
78
     * @var \ComponentManager\PackageRepository\PackageRepositoryFactory
79
     */
80
    protected $packageRepositoryFactory;
81
82
    /**
83
     * Package source factory.
84
     *
85
     * @var \ComponentManager\PackageSource\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 \ComponentManager\Project\Project
96
     */
97
    protected $project;
98
    /**
99
     * Platform support library.
100
     *
101
     * @var \ComponentManager\Platform\Platform
102
     */
103
    protected $platform;
104
105
    /**
106
     * Initialiser.
107
     *
108
     * @param \ComponentManager\PackageRepository\PackageRepositoryFactory $packageRepositoryFactory
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 100 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...
109
     * @param \ComponentManager\PackageSource\PackageSourceFactory         $packageSourceFactory
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 96 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...
110
     * @param \ComponentManager\PackageFormat\PackageFormatFactory         $packageFormatFactory
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 96 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...
111
     * @param \ComponentManager\Platform\Platform                          $platform
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 84 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...
112
     * @param \Symfony\Component\Filesystem\Filesystem                     $filesystem
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 86 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...
113
     * @param \Psr\Log\LoggerInterface                                     $logger
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 82 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...
114
     */
115
    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...
116
                                PackageSourceFactory $packageSourceFactory,
117
                                PackageFormatFactory $packageFormatFactory,
118
                                Platform $platform, Filesystem $filesystem,
119
                                LoggerInterface $logger) {
120
        $this->packageRepositoryFactory = $packageRepositoryFactory;
121
        $this->packageSourceFactory     = $packageSourceFactory;
122
        $this->packageFormatFactory     = $packageFormatFactory;
123
124
        $this->filesystem = $filesystem;
125
        $this->platform   = $platform;
126
127
        $this->logger = $logger;
128
129
        parent::__construct();
130
    }
131
132
    /**
133
     * Get the Moodle bridge.
134
     *
135
     * @param string|null $moodleDirectory
136
     *
137
     * @return \ComponentManager\Moodle
138
     */
139
    protected function getMoodle($moodleDirectory=null) {
140
        if ($this->moodle === null) {
141
            $moodleDirectory = ($moodleDirectory === null)
142
                    ? $this->platform->getWorkingDirectory() : $moodleDirectory;
143
144
            $this->moodle = new Moodle($moodleDirectory, $this->platform);
145
        }
146
147
        return $this->moodle;
148
    }
149
150
    /**
151
     * Get project.
152
     *
153
     * @param string|null $projectFilename
154
     * @param string|null $projectLockFilename
155
     *
156
     * @return \ComponentManager\Project\Project
157
     */
158
    protected function getProject($projectFilename=null, $projectLockFilename=null) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 85 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...
159
        $workingDirectory = $this->platform->getWorkingDirectory();
160
161
        if ($this->project === null) {
162 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...
163
                $projectFilename = $this->platform->joinPaths([
164
                    $workingDirectory,
165
                    static::PROJECT_FILENAME,
166
                ]);
167
            } else {
168
                $projectFilename = $this->platform->expandPath(
169
                        $projectFilename);
170
            }
171
172 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...
173
                $projectLockFilename = $this->platform->joinPaths([
174
                    $workingDirectory,
175
                    static::PROJECT_LOCK_FILENAME,
176
                ]);
177
            } else {
178
                $projectLockFilename = $this->platform->expandPath(
179
                        $projectLockFilename);
180
            }
181
182
            $this->logger->info('Parsing project file', [
183
                'filename' => $projectFilename,
184
            ]);
185
            $this->project = new Project(
186
                    new ProjectFile($projectFilename),
187
                    new ProjectLockFile($projectLockFilename),
188
                    $this->packageRepositoryFactory,
189
                    $this->packageSourceFactory,
190
                    $this->packageFormatFactory);
191
        }
192
193
        return $this->project;
194
    }
195
}
196