Skip to content

Commit dbc5778

Browse files
authored
Merge pull request #17 from svenjungnickel/fallback-url
Add possibility to set a fallback url
2 parents efd6809 + 6e258bd commit dbc5778

File tree

8 files changed

+138
-6
lines changed

8 files changed

+138
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
},
6666
"phpmd": {
6767
"url": "http://static.phpmd.org/php/latest/phpmd.phar",
68+
"fallback-url": "https://github.com/jakzal/phpmd/releases/download/2.6.0-jakzal-2/phpmd.phar",
6869
"force-replace": true
6970
},
7071
"security-checker": {

src/Factory/ToolFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static function createTool($name, $directory, array $parameters)
2424
'only-dev' => true,
2525
'force-replace' => false,
2626
'rename' => false,
27+
'fallback-url' => null,
2728
];
2829

2930
$parameters = array_merge($defaults, $parameters);
@@ -47,6 +48,10 @@ public static function createTool($name, $directory, array $parameters)
4748
$tool->setNameToToolKey();
4849
}
4950

51+
if (null !== $parameters['fallback-url']) {
52+
$tool->setFallbackUrl($parameters['fallback-url']);
53+
}
54+
5055
return $tool;
5156
}
5257

src/Model/Tool.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ class Tool
4242
*/
4343
private $rename = false;
4444

45+
/**
46+
* @var string
47+
*/
48+
private $fallbackUrl;
49+
4550
/**
4651
* @param string $name
4752
* @param string $filename
4853
* @param string $url
4954
* @param string $signUrl
50-
*
5155
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5256
*/
5357
public function __construct($name, $filename, $url, $signUrl = null)
@@ -137,4 +141,22 @@ public function renameToConfigKey()
137141
{
138142
return $this->rename;
139143
}
144+
145+
/**
146+
* @param string $url
147+
*
148+
* @return void
149+
*/
150+
public function setFallbackUrl($url)
151+
{
152+
$this->fallbackUrl = $url;
153+
}
154+
155+
/**
156+
* @return string
157+
*/
158+
public function getFallbackUrl()
159+
{
160+
return $this->fallbackUrl;
161+
}
140162
}

src/Script/Decision/IsAccessibleDecision.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class IsAccessibleDecision extends AbstractDecision
1717
public function canProceed(Tool $tool)
1818
{
1919
if (false === $this->helper->getDownloader()->isAccessible($tool->getUrl())) {
20-
return false;
20+
return $this->fallbackUrlIsAccessible($tool);
2121
}
2222

2323
if (empty($tool->getSignUrl())) {
@@ -38,4 +38,16 @@ public function getReason()
3838
{
3939
return '<error>At least one given URL are not accessible!</error>';
4040
}
41+
42+
/**
43+
* @param Tool $tool
44+
*
45+
* @return bool
46+
*/
47+
private function fallbackUrlIsAccessible(Tool $tool)
48+
{
49+
$fallbackUrl = $tool->getFallbackUrl();
50+
51+
return false === empty($fallbackUrl) && true === $this->helper->getDownloader()->isAccessible($fallbackUrl);
52+
}
4153
}

src/Script/Processor.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Tooly\Script\Decision\IsAccessibleDecision;
1010
use Tooly\Script\Decision\IsVerifiedDecision;
1111
use Tooly\Script\Decision\OnlyDevDecision;
12-
use Tooly\Script\Helper;
1312
use Tooly\Model\Tool;
1413

1514
/**
@@ -80,7 +79,7 @@ public function process(Tool $tool)
8079
return;
8180
}
8281

83-
$data = $this->helper->getDownloader()->download($tool->getUrl());
82+
$data = $this->helper->getDownloader()->download($this->getDownloadUrl($tool));
8483
$filename = $tool->getFilename();
8584

8685
$this->helper->getFilesystem()->createFile($filename, $data);
@@ -166,4 +165,18 @@ private function removeFromDir($dir, array $excludeToolNames = [])
166165
$this->helper->getFilesystem()->remove($path);
167166
}
168167
}
168+
169+
/**
170+
* @param Tool $tool
171+
*
172+
* @return string
173+
*/
174+
private function getDownloadUrl(Tool $tool)
175+
{
176+
if (false === $this->helper->getDownloader()->isAccessible($tool->getUrl())) {
177+
return $tool->getFallbackUrl();
178+
}
179+
180+
return $tool->getUrl();
181+
}
169182
}

tests/Factory/ToolFactoryTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ public function testCanCreateATool()
1818
$tool = ToolFactory::createTool('test', 'vfs://root', [
1919
'url' => 'my-url',
2020
'sign-url' => 'my-sign-url',
21-
'force-replace' => true
21+
'force-replace' => true,
22+
'only-dev' => false,
23+
'rename' => true,
24+
'fallback-url' => 'fallback-url'
2225
]);
2326

2427
$this->assertInstanceOf(Tool::class, $tool);
2528
$this->assertEquals('my-url', $tool->getUrl());
2629
$this->assertEquals('my-sign-url', $tool->getSignUrl());
2730
$this->assertTrue($tool->forceReplace());
28-
$this->assertTrue($tool->isOnlyDev());
31+
$this->assertFalse($tool->isOnlyDev());
32+
$this->assertTrue($tool->renameToConfigKey());
33+
$this->assertEquals('fallback-url', $tool->getFallbackUrl());
2934
}
3035

3136
public function testCanCreateMultipleTools()

tests/Script/Decision/IsAccessibleDecisionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ public function testNotAccessibleToolSignUrlReturnsFalse()
7474
])));
7575
}
7676

77+
public function testNotAccessibleToolUrlButAccessibleFallbackUrlReturnsTrue()
78+
{
79+
$downloader = $this
80+
->getMockBuilder(Downloader::class)
81+
->getMock();
82+
83+
$downloader
84+
->expects($this->exactly(2))
85+
->method('isAccessible')
86+
->will($this->onConsecutiveCalls(false, true));
87+
88+
$this->helper
89+
->expects($this->exactly(2))
90+
->method('getDownloader')
91+
->willReturn($downloader);
92+
93+
$decision = new IsAccessibleDecision($this->configuration, $this->helper);
94+
$this->assertTrue($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [
95+
'fallback-url' => 'fallback-url'
96+
])));
97+
}
98+
7799
public function testAccessibleUrlsWillReturnTrue()
78100
{
79101
$downloader = $this

tests/Script/Processor/ProcessTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,56 @@ public function testCanSuccessfullyDownloadATool()
121121
$processor = new Processor($this->io, $this->helper, $this->configuration);
122122
$processor->process($tool);
123123
}
124+
125+
public function testCanSuccessfullyDownloadAToolViaFallbackUrl()
126+
{
127+
vfsStream::setup('bin');
128+
129+
$downloader = $this
130+
->getMockBuilder(Downloader::class)
131+
->getMock();
132+
133+
$downloader
134+
->expects($this->exactly(3))
135+
->method('isAccessible')
136+
->will($this->onConsecutiveCalls(false, true, false));
137+
138+
$filesystem = $this
139+
->getMockBuilder(Filesystem::class)
140+
->getMock();
141+
142+
$filesystem
143+
->method('isFileAlreadyExist')
144+
->willReturn(false);
145+
146+
$this->helper
147+
->method('getFilesystem')
148+
->willReturn($filesystem);
149+
150+
$this->helper
151+
->expects($this->exactly(4))
152+
->method('getDownloader')
153+
->willReturn($downloader);
154+
155+
$this->helper
156+
->method('isFileAlreadyExist')
157+
->willReturn(false);
158+
159+
$this->io
160+
->expects($this->exactly(2))
161+
->method('write');
162+
163+
$tool = $this
164+
->getMockBuilder(Tool::class)
165+
->disableOriginalConstructor()
166+
->getMock();
167+
168+
$tool
169+
->expects($this->exactly(2))
170+
->method('getFallbackUrl')
171+
->willReturn('//test.html');
172+
173+
$processor = new Processor($this->io, $this->helper, $this->configuration);
174+
$processor->process($tool);
175+
}
124176
}

0 commit comments

Comments
 (0)