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

ProjectAwareCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 7
dl 27
loc 126
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
A getMoodle() 0 10 3
B getProject() 15 32 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\PackageRepository;
16
use ComponentManager\PackageRepository\PackageRepositoryFactory;
17
use ComponentManager\PackageSource\PackageSourceFactory;
18
use ComponentManager\PlatformUtil;
19
use ComponentManager\Project\Project;
20
use ComponentManager\Project\ProjectFile;
21
use ComponentManager\Project\ProjectLockFile;
22
use Psr\Log\LoggerInterface;
23
use Symfony\Component\Console\Command\Command;
24
25
/**
26
 * Project-aware command trait.
27
 *
28
 * Provides helpful utility methods for accessing the project in the currrent
29
 * working directory. Import this into command implementations to reduce
30
 * duplication.
31
 */
32
abstract class ProjectAwareCommand extends Command {
33
    /**
34
     * Logger.
35
     *
36
     * @var \Psr\Log\LoggerInterface
37
     */
38
    protected $logger;
39
40
    /**
41
     * Moodle bridge.
42
     *
43
     * @var \ComponentManager\Moodle
44
     */
45
    protected $moodle;
46
47
    /**
48
     * Package format factory.
49
     *
50
     * @var \ComponentManager\PackageFormat\PackageFormatFactory
51
     */
52
    protected $packageFormatFactory;
53
54
    /**
55
     * Package repository factory.
56
     *
57
     * @var \ComponentManager\PackageRepository\PackageRepositoryFactory
58
     */
59
    protected $packageRepositoryFactory;
60
61
    /**
62
     * Package source factory.
63
     *
64
     * @var \ComponentManager\PackageSource\PackageSourceFactory
65
     */
66
    protected $packageSourceFactory;
67
68
    /**
69
     * Project.
70
     *
71
     * Lazily loaded -- be sure to call getProject() in order to ensure the
72
     * value is defined.
73
     *
74
     * @var \ComponentManager\Project\Project
75
     */
76
    protected $project;
77
78
    /**
79
     * Initialiser.
80
     *
81
     * @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...
82
     * @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...
83
     * @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...
84
     * @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...
85
     */
86 View Code Duplication
    public function __construct(PackageRepositoryFactory $packageRepositoryFactory,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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...
87
                                PackageSourceFactory $packageSourceFactory,
88
                                PackageFormatFactory $packageFormatFactory,
89
                                LoggerInterface $logger) {
90
        $this->packageRepositoryFactory = $packageRepositoryFactory;
91
        $this->packageSourceFactory     = $packageSourceFactory;
92
        $this->packageFormatFactory     = $packageFormatFactory;
93
94
        $this->logger = $logger;
95
96
        parent::__construct();
97
    }
98
99
    /**
100
     * Get the Moodle bridge.
101
     *
102
     * @param string|null $moodleDirectory
103
     *
104
     * @return \ComponentManager\Moodle
105
     */
106
    protected function getMoodle($moodleDirectory=null) {
107
        if ($this->moodle === null) {
108
            $moodleDirectory = ($moodleDirectory === null)
109
                    ? PlatformUtil::workingDirectory() : $moodleDirectory;
110
111
            $this->moodle = new Moodle($moodleDirectory);
112
        }
113
114
        return $this->moodle;
115
    }
116
117
    /**
118
     * Get project.
119
     *
120
     * @param string|null $projectFilename
121
     * @param string|null $projectLockFilename
122
     *
123
     * @return \ComponentManager\Project\Project
124
     */
125
    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...
126
        if ($this->project === null) {
127 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...
128
                $projectFilename = PlatformUtil::workingDirectory()
129
                                 . PlatformUtil::directorySeparator()
130
                                 . 'componentmgr.json';
131
            } else {
132
                $projectFilename = PlatformUtil::expandPath($projectFilename);
133
            }
134
135 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...
136
                $projectLockFilename = PlatformUtil::workingDirectory()
137
                                     . PlatformUtil::directorySeparator()
138
                                     . 'componentmgr.lock.json';
139
            } else {
140
                $projectLockFilename = PlatformUtil::expandPath(
141
                        $projectLockFilename);
142
            }
143
144
            $this->logger->info('Parsing project file', [
145
                'filename' => $projectFilename,
146
            ]);
147
            $this->project = new Project(
148
                    new ProjectFile($projectFilename),
149
                    new ProjectLockFile($projectLockFilename),
150
                    $this->packageRepositoryFactory,
151
                    $this->packageSourceFactory,
152
                    $this->packageFormatFactory);
153
        }
154
155
        return $this->project;
156
    }
157
}
158