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\Step; |
12
|
|
|
|
13
|
|
|
use ComponentManager\ComponentSpecification; |
14
|
|
|
use ComponentManager\Exception\InvalidProjectException; |
15
|
|
|
use ComponentManager\PackageRepository\PackageRepository; |
16
|
|
|
use ComponentManager\Project\Project; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Validate the project file. |
21
|
|
|
*/ |
22
|
|
|
class ValidateProjectStep implements Step { |
23
|
|
|
/** |
24
|
|
|
* Project to validate. |
25
|
|
|
* |
26
|
|
|
* @var Project |
27
|
|
|
*/ |
28
|
|
|
protected $project; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Initialiser. |
32
|
|
|
* |
33
|
|
|
* @param Project $project |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Project $project) { |
36
|
|
|
$this->project = $project; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc Step |
41
|
|
|
*/ |
42
|
|
|
public function execute($task, LoggerInterface $logger) { |
43
|
|
|
$componentSpecifications = $this->project->getProjectFile()->getComponentSpecifications(); |
44
|
|
|
$packageRepositories = $this->project->getPackageRepositories(); |
45
|
|
|
|
46
|
|
|
$result = true; |
47
|
|
|
|
48
|
|
|
if (!$this->project->getProjectFile()->getMoodleVersion()) { |
49
|
|
|
$result = false; |
50
|
|
|
$logger->warn('No moodle.version key; package operations will fail'); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($componentSpecifications as $componentSpecification) { |
54
|
|
|
if (!$this->isValidComponentName($componentSpecification)) { |
55
|
|
|
$result = false; |
56
|
|
|
$logger->error('An invalid component name was specified', [ |
57
|
|
|
'componentName' => $componentSpecification->getName(), |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!$this->isComponentSpecificationComplete($componentSpecification)) { |
62
|
|
|
$result = false; |
63
|
|
|
$logger->error('An incomplete component specification was specified', [ |
64
|
|
|
'componentSpecification' => $componentSpecification, |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!$this->isValidPackageRepository($componentSpecification, $packageRepositories)) { |
69
|
|
|
$result = false; |
70
|
|
|
$logger->error('Component uses undeclared package repository', [ |
71
|
|
|
'componentName' => $componentSpecification->getName(), |
72
|
|
|
'packageRepositoryName' => $componentSpecification->getPackageRepository(), |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!$result) { |
77
|
|
|
throw new InvalidProjectException( |
78
|
|
|
'The supplied project file is invalid', |
79
|
|
|
InvalidProjectException::CODE_VALIDATION_FAILED); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Does the supplied component have a valid name? |
86
|
|
|
* |
87
|
|
|
* @param ComponentSpecification $componentSpecification |
88
|
|
|
* |
89
|
|
|
* @return boolean |
90
|
|
|
*/ |
91
|
|
|
protected function isValidComponentName(ComponentSpecification $componentSpecification) { |
92
|
|
|
$parts = explode('_', $componentSpecification->getName(), 2); |
93
|
|
|
return count($parts) === 2; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Is the supplied component's package repository known to us? |
98
|
|
|
* |
99
|
|
|
* @param ComponentSpecification $componentSpecification |
100
|
|
|
* @param PackageRepository[] $packageRepositories |
101
|
|
|
* |
102
|
|
|
* @return boolean |
103
|
|
|
*/ |
104
|
|
|
protected function isValidPackageRepository(ComponentSpecification $componentSpecification, $packageRepositories) { |
105
|
|
|
return array_key_exists( |
106
|
|
|
$componentSpecification->getPackageRepository(), $packageRepositories); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Is the supplied component's specification complete? |
111
|
|
|
* |
112
|
|
|
* @param ComponentSpecification $componentSpecification |
113
|
|
|
* |
114
|
|
|
* @return boolean |
115
|
|
|
*/ |
116
|
|
|
protected function isComponentSpecificationComplete(ComponentSpecification $componentSpecification) { |
117
|
|
|
return !!$componentSpecification->getPackageRepository() |
118
|
|
|
&& !!$componentSpecification->getPackageSource(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: