Skip to content

Commit c7aadd3

Browse files
author
Sven Jungnickel
committed
Add possibility to set a fallback url
1 parent efd6809 commit c7aadd3

File tree

7 files changed

+197
-5
lines changed

7 files changed

+197
-5
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://tooly.me-dresden.de/phpmd/2.6.0/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
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Tooly\Script\Decision;
4+
5+
use Tooly\Model\Tool;
6+
7+
/**
8+
* @package Tooly\Script\Decision
9+
*/
10+
class UseFallbackURLDecision extends AbstractDecision
11+
{
12+
/**
13+
* @param Tool $tool
14+
*
15+
* @return bool
16+
*/
17+
public function canProceed(Tool $tool)
18+
{
19+
if (true === $this->helper->getDownloader()->isAccessible($tool->getUrl())) {
20+
return true;
21+
}
22+
23+
if (empty($tool->getFallbackUrl())) {
24+
return true;
25+
}
26+
27+
if (false === $this->helper->getDownloader()->isAccessible($tool->getFallbackUrl())){
28+
return false;
29+
}
30+
31+
return true;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getReason()
38+
{
39+
return '<error>Fallback URL is not accessible!</error>';
40+
}
41+
}

src/Script/Processor.php

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

1515
/**
@@ -80,7 +80,7 @@ public function process(Tool $tool)
8080
return;
8181
}
8282

83-
$data = $this->helper->getDownloader()->download($tool->getUrl());
83+
$data = $this->helper->getDownloader()->download($this->getDownloadUrl($tool));
8484
$filename = $tool->getFilename();
8585

8686
$this->helper->getFilesystem()->createFile($filename, $data);
@@ -124,6 +124,7 @@ private function getDecisions()
124124
return [
125125
new OnlyDevDecision($this->configuration, $this->helper),
126126
new IsAccessibleDecision($this->configuration, $this->helper),
127+
new UseFallbackURLDecision($this->configuration, $this->helper),
127128
new FileAlreadyExistDecision($this->configuration, $this->helper),
128129
new IsVerifiedDecision($this->configuration, $this->helper),
129130
new DoReplaceDecision($this->configuration, $this->helper, $this->io),
@@ -166,4 +167,18 @@ private function removeFromDir($dir, array $excludeToolNames = [])
166167
$this->helper->getFilesystem()->remove($path);
167168
}
168169
}
170+
171+
/**
172+
* @param Tool $tool
173+
*
174+
* @return string
175+
*/
176+
private function getDownloadUrl(Tool $tool)
177+
{
178+
if (false === $this->helper->getDownloader()->isAccessible($tool->getUrl())) {
179+
return $tool->getFallbackUrl();
180+
}
181+
182+
return $tool->getUrl();
183+
}
169184
}

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()
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Tooly\Tests\Script\Decision;
4+
5+
use Tooly\Factory\ToolFactory;
6+
use Tooly\Script\Decision\UseFallbackURLDecision;
7+
use Tooly\Script\Helper\Downloader;
8+
9+
/**
10+
* @package Tooly\Tests\Script\Decision
11+
*/
12+
class UseFallbackUrlDecisionTest extends DecisionTestCase
13+
{
14+
public function testAccessibleUrlsWillReturnTrue()
15+
{
16+
$downloader = $this
17+
->getMockBuilder(Downloader::class)
18+
->getMock();
19+
20+
$downloader
21+
->expects($this->once())
22+
->method('isAccessible')
23+
->willReturn(true);
24+
25+
$this->helper
26+
->expects($this->once())
27+
->method('getDownloader')
28+
->willReturn($downloader);
29+
30+
$decision = new UseFallbackURLDecision($this->configuration, $this->helper);
31+
$this->assertTrue($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [])));
32+
}
33+
34+
public function testEmptySignUrlReturnsTrue()
35+
{
36+
$downloader = $this
37+
->getMockBuilder(Downloader::class)
38+
->getMock();
39+
40+
$downloader
41+
->expects($this->once())
42+
->method('isAccessible')
43+
->willReturn(false);
44+
45+
$this->helper
46+
->expects($this->once())
47+
->method('getDownloader')
48+
->willReturn($downloader);
49+
50+
$decision = new UseFallbackURLDecision($this->configuration, $this->helper);
51+
$this->assertTrue($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [])));
52+
}
53+
54+
public function testNotAccessibleFallbackUrlReturnsFalse()
55+
{
56+
$downloader = $this
57+
->getMockBuilder(Downloader::class)
58+
->getMock();
59+
60+
$downloader
61+
->expects($this->exactly(2))
62+
->method('isAccessible')
63+
->will($this->onConsecutiveCalls(false, false));
64+
65+
$this->helper
66+
->expects($this->exactly(2))
67+
->method('getDownloader')
68+
->willReturn($downloader);
69+
70+
$decision = new UseFallbackURLDecision($this->configuration, $this->helper);
71+
$this->assertFalse($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [
72+
'fallback-url' => 'fallback-url'
73+
])));
74+
}
75+
76+
public function testAccessibleFallbackUrlWillReturnTrue()
77+
{
78+
$downloader = $this
79+
->getMockBuilder(Downloader::class)
80+
->getMock();
81+
82+
$downloader
83+
->expects($this->exactly(2))
84+
->method('isAccessible')
85+
->will($this->onConsecutiveCalls(false, true));
86+
87+
$this->helper
88+
->expects($this->exactly(2))
89+
->method('getDownloader')
90+
->willReturn($downloader);
91+
92+
$decision = new UseFallbackURLDecision($this->configuration, $this->helper);
93+
$this->assertTrue($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [
94+
'fallback-url' => 'fallback-url'
95+
])));
96+
}
97+
98+
public function testCanGetReason()
99+
{
100+
$decision = new UseFallbackURLDecision($this->configuration, $this->helper);
101+
$this->assertRegExp('/error/', $decision->getReason());
102+
}
103+
}

0 commit comments

Comments
 (0)