Skip to content

Commit 29325b1

Browse files
committed
Remove duplicate code in helper class and make needed changes
1 parent 0cf1bbe commit 29325b1

File tree

6 files changed

+64
-43
lines changed

6 files changed

+64
-43
lines changed

src/Script/Decision/DoReplaceDecision.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(Configuration $configuration, Helper $helper, IOInte
3636
*/
3737
public function canProceed(Tool $tool)
3838
{
39-
if (false === $this->helper->isFileAlreadyExist($tool->getFilename(), null)) {
39+
if (false === $this->helper->getFilesystem()->isFileAlreadyExist($tool->getFilename())) {
4040
return true;
4141
}
4242

src/Script/Decision/IsAccessibleDecision.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class IsAccessibleDecision extends AbstractDecision
1616
*/
1717
public function canProceed(Tool $tool)
1818
{
19-
if (false === $this->helper->isAccessible($tool->getUrl())) {
19+
if (false === $this->helper->getDownloader()->isAccessible($tool->getUrl())) {
2020
return false;
2121
}
2222

2323
if (empty($tool->getSignUrl())) {
2424
return true;
2525
}
2626

27-
if (false === $this->helper->isAccessible($tool->getSignUrl())) {
27+
if (false === $this->helper->getDownloader()->isAccessible($tool->getSignUrl())) {
2828
return false;
2929
}
3030

src/Script/Helper.php

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ public function __construct(Filesystem $filesystem, Downloader $downloader, Veri
3838
$this->verifier = $verifier;
3939
}
4040

41-
/**
42-
* @param string $url
43-
*
44-
* @return bool
45-
*/
46-
public function isAccessible($url)
47-
{
48-
return $this->downloader->isAccessible($url);
49-
}
50-
5141
/**
5242
* @param string $filename
5343
* @param string $targetFile
@@ -74,14 +64,14 @@ public function isFileAlreadyExist($filename, $targetFile)
7464
*/
7565
public function isVerified($signatureUrl, $fileUrl)
7666
{
77-
$data = $this->download($fileUrl);
78-
$signatureData = $this->download($signatureUrl);
67+
$data = $this->downloader->download($fileUrl);
68+
$signatureData = $this->downloader->download($signatureUrl);
7969

8070
$tmpFile = rtrim(sys_get_temp_dir(), '/') . '/_tool';
81-
$this->createFile($tmpFile, $data);
71+
$this->filesystem->createFile($tmpFile, $data);
8272

8373
$tmpSignFile = rtrim(sys_get_temp_dir(), '/') . '/_tool.sign';
84-
$this->createFile($tmpSignFile, $signatureData);
74+
$this->filesystem->createFile($tmpSignFile, $signatureData);
8575

8676
$result = $this->verifier->checkGPGSignature($tmpSignFile, $tmpFile);
8777

@@ -92,34 +82,26 @@ public function isVerified($signatureUrl, $fileUrl)
9282
}
9383

9484
/**
95-
* @param string $url
96-
*
97-
* @return string
85+
* @return Filesystem
9886
*/
99-
public function download($url)
87+
public function getFilesystem()
10088
{
101-
return $this->downloader->download($url);
89+
return $this->filesystem;
10290
}
10391

10492
/**
105-
* @param string $filename
106-
* @param string $content
107-
*
108-
* @return bool
93+
* @return Downloader
10994
*/
110-
public function createFile($filename, $content)
95+
public function getDownloader()
11196
{
112-
return $this->filesystem->createFile($filename, $content);
97+
return $this->downloader;
11398
}
11499

115100
/**
116-
* @param string $sourceFile
117-
* @param string $file
118-
*
119-
* @return bool
101+
* @return Verifier
120102
*/
121-
public function symlinkFile($sourceFile, $file)
103+
public function getVerifier()
122104
{
123-
return $this->filesystem->symlinkFile($sourceFile, $file);
105+
return $this->verifier;
124106
}
125107
}

src/Script/Processor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ public function process(Tool $tool)
7878
return;
7979
}
8080

81-
$data = $this->helper->download($tool->getUrl());
81+
$data = $this->helper->getDownloader()->download($tool->getUrl());
8282
$filename = $tool->getFilename();
8383

84-
$this->helper->createFile($filename, $data);
84+
$this->helper->getFilesystem()->createFile($filename, $data);
8585

8686
$this->io->write(sprintf(
8787
'<info>File "%s" successfully downloaded!</info>',
@@ -98,7 +98,7 @@ public function symlink(Tool $tool)
9898
$composerDir = $this->configuration->getComposerBinDirectory();
9999
$composerPath = $composerDir . DIRECTORY_SEPARATOR . basename($filename);
100100

101-
$this->helper->symlinkFile($filename, $composerPath);
101+
$this->helper->getFilesystem()->symlinkFile($filename, $composerPath);
102102
}
103103

104104
/**

tests/Script/Decision/IsAccessibleDecisionTest.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Tooly\Factory\ToolFactory;
66
use Tooly\Model\Tool;
77
use Tooly\Script\Decision\IsAccessibleDecision;
8+
use Tooly\Script\Helper\Downloader;
89

910
/**
1011
* @package Tooly\Tests\Script\Decision
@@ -13,32 +14,60 @@ class IsAccessibleDecisionTest extends DecisionTestCase
1314
{
1415
public function testNotAccessibleToolUrlReturnsFalse()
1516
{
16-
$this->helper
17+
$downloader = $this
18+
->getMockBuilder(Downloader::class)
19+
->getMock();
20+
21+
$downloader
1722
->expects($this->once())
1823
->method('isAccessible')
1924
->willReturn(false);
2025

26+
$this->helper
27+
->expects($this->once())
28+
->method('getDownloader')
29+
->willReturn($downloader);
30+
2131
$decision = new IsAccessibleDecision($this->configuration, $this->helper);
2232
$this->assertFalse($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [])));
2333
}
2434

2535
public function testEmptySignUrlReturnsTrue()
2636
{
27-
$this->helper
37+
$downloader = $this
38+
->getMockBuilder(Downloader::class)
39+
->getMock();
40+
41+
$downloader
2842
->expects($this->once())
2943
->method('isAccessible')
3044
->willReturn(true);
3145

46+
$this->helper
47+
->expects($this->once())
48+
->method('getDownloader')
49+
->willReturn($downloader);
50+
3251
$decision = new IsAccessibleDecision($this->configuration, $this->helper);
3352
$this->assertTrue($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [])));
3453
}
3554

3655
public function testNotAccessibleToolSignUrlReturnsFalse()
3756
{
38-
$this->helper
57+
$downloader = $this
58+
->getMockBuilder(Downloader::class)
59+
->getMock();
60+
61+
$downloader
62+
->expects($this->exactly(2))
3963
->method('isAccessible')
4064
->will($this->onConsecutiveCalls(true, false));
4165

66+
$this->helper
67+
->expects($this->exactly(2))
68+
->method('getDownloader')
69+
->willReturn($downloader);
70+
4271
$decision = new IsAccessibleDecision($this->configuration, $this->helper);
4372
$this->assertFalse($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [
4473
'sign-url' => 'sign-url'
@@ -47,10 +76,20 @@ public function testNotAccessibleToolSignUrlReturnsFalse()
4776

4877
public function testAccessibleUrlsWillReturnTrue()
4978
{
50-
$this->helper
79+
$downloader = $this
80+
->getMockBuilder(Downloader::class)
81+
->getMock();
82+
83+
$downloader
84+
->expects($this->exactly(2))
5185
->method('isAccessible')
5286
->will($this->onConsecutiveCalls(true, true));
5387

88+
$this->helper
89+
->expects($this->exactly(2))
90+
->method('getDownloader')
91+
->willReturn($downloader);
92+
5493
$decision = new IsAccessibleDecision($this->configuration, $this->helper);
5594
$this->assertTrue($decision->canProceed(ToolFactory::createTool('tool', __DIR__, [
5695
'sign-url' => 'sign-url'

tests/Script/HelperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testCanCheckIfFileAlreadyExist()
8484
$this->assertFalse($helper->isFileAlreadyExist('foo', 'bar'));
8585
}
8686

87-
public function testCanCopyAFile()
87+
public function testCanSymlinkAFile()
8888
{
8989
$filesystem = $this
9090
->getMockBuilder(Filesystem::class)
@@ -96,6 +96,6 @@ public function testCanCopyAFile()
9696
->willReturn(true);
9797

9898
$helper = new Helper($filesystem, new Downloader, new Verifier);
99-
$this->assertTrue($helper->symlinkFile('foo', 'bar'));
99+
$this->assertTrue($helper->getFilesystem()->symlinkFile('foo', 'bar'));
100100
}
101101
}

0 commit comments

Comments
 (0)