Skip to content

Commit eaa9854

Browse files
committed
Extracts .gitattributes writing
1 parent 175aff9 commit eaa9854

File tree

3 files changed

+78
-57
lines changed

3 files changed

+78
-57
lines changed

src/Commands/ValidateCommand.php

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Stolt\LeanPackage\Exceptions\NoLicenseFilePresent;
1919
use Stolt\LeanPackage\Exceptions\NonExistentGlobPatternFile;
2020
use Stolt\LeanPackage\Exceptions\PresetNotAvailable;
21+
use Stolt\LeanPackage\GitattributesFileRepository;
2122
use Stolt\LeanPackage\Helpers\InputReader;
2223
use Symfony\Component\Console\Command\Command;
2324
use Symfony\Component\Console\Input\InputArgument;
@@ -54,6 +55,8 @@ final class ValidateCommand extends Command
5455
*/
5556
protected Validator $archiveValidator;
5657

58+
protected GitattributesFileRepository $gitattributesFileRepository;
59+
5760
/**
5861
* Input reader.
5962
*
@@ -70,6 +73,7 @@ public function __construct(Analyser $analyser, Validator $archiveValidator, Inp
7073
{
7174
$this->analyser = $analyser;
7275
$this->archiveValidator = $archiveValidator;
76+
$this->gitattributesFileRepository = new GitattributesFileRepository($this->analyser);
7377
$this->inputReader = $inputReader;
7478

7579
parent::__construct();
@@ -453,7 +457,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
453457
$expectedGitattributesFileContent = $this->generatedHeader . PHP_EOL . PHP_EOL . $expectedGitattributesFileContent;
454458
}
455459

456-
$outputContent .= $this->createGitattributesFile(
460+
$outputContent .= $this->gitattributesFileRepository->createGitattributesFile(
457461
$expectedGitattributesFileContent
458462
);
459463

@@ -555,7 +559,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
555559
$expectedGitattributesFileContent = $this->modifiedHeader . PHP_EOL . PHP_EOL . $expectedGitattributesFileContent;
556560
}
557561

558-
$outputContent .= $this->overwriteGitattributesFile(
562+
$outputContent .= $this->gitattributesFileRepository->overwriteGitattributesFile(
559563
$expectedGitattributesFileContent
560564
);
561565

@@ -658,8 +662,8 @@ protected function isDefaultGlobPatternFilePresent(): bool
658662
* Validate archive of current Git HEAD.
659663
*
660664
* @param boolean $validateLicenseFilePresence Whether the archive should have a license file or not.
665+
* @throws GitNotAvailable|NoLicenseFilePresent
661666
* @throws GitHeadNotAvailable
662-
* @throws GitNotAvailable
663667
* @return boolean
664668
*/
665669
protected function isValidArchive(bool $validateLicenseFilePresence = false): bool
@@ -708,56 +712,4 @@ protected function getSuggestGitattributesFileCreationOptionOutput(
708712

709713
return PHP_EOL . PHP_EOL . $content;
710714
}
711-
712-
/**
713-
* Create the gitattributes file.
714-
*
715-
* @param string $content The content of the gitattributes file
716-
* @throws GitattributesCreationFailed
717-
*
718-
* @return string
719-
*/
720-
protected function createGitattributesFile(string $content): string
721-
{
722-
$bytesWritten = file_put_contents(
723-
$this->analyser->getGitattributesFilePath(),
724-
$content
725-
);
726-
727-
if ($bytesWritten) {
728-
$content = 'Created a .gitattributes file with the shown content:'
729-
. PHP_EOL . '<info>' . $content . '</info>';
730-
731-
return PHP_EOL . PHP_EOL . $content;
732-
}
733-
734-
$message = 'Creation of .gitattributes file failed.';
735-
throw new GitattributesCreationFailed($message);
736-
}
737-
738-
/**
739-
* Overwrite an existing gitattributes file.
740-
*
741-
* @param string $content The content of the gitattributes file
742-
* @throws GitattributesCreationFailed
743-
*
744-
* @return string
745-
*/
746-
protected function overwriteGitattributesFile(string $content): string
747-
{
748-
$bytesWritten = file_put_contents(
749-
$this->analyser->getGitattributesFilePath(),
750-
$content
751-
);
752-
753-
if ($bytesWritten) {
754-
$content = 'Overwrote it with the shown content:'
755-
. PHP_EOL . '<info>' . $content . '</info>';
756-
757-
return PHP_EOL . PHP_EOL . $content;
758-
}
759-
760-
$message = 'Overwrite of .gitattributes file failed.';
761-
throw new GitattributesCreationFailed($message);
762-
}
763715
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\LeanPackage;
6+
7+
use Stolt\LeanPackage\Exceptions\GitattributesCreationFailed;
8+
9+
final class GitattributesFileRepository
10+
{
11+
protected Analyser $analyser;
12+
13+
public function __construct(Analyser $analyser)
14+
{
15+
$this->analyser = $analyser;
16+
}
17+
18+
/**
19+
* Create the gitattributes file.
20+
*
21+
* @param string $content The content of the gitattributes file
22+
* @throws GitattributesCreationFailed
23+
* @return string
24+
*
25+
*/
26+
public function createGitattributesFile(string $content): string
27+
{
28+
$bytesWritten = file_put_contents(
29+
$this->analyser->getGitattributesFilePath(),
30+
$content
31+
);
32+
33+
if ($bytesWritten) {
34+
$content = 'Created a .gitattributes file with the shown content:'
35+
. PHP_EOL . '<info>' . $content . '</info>';
36+
37+
return PHP_EOL . PHP_EOL . $content;
38+
}
39+
40+
$message = 'Creation of .gitattributes file failed.';
41+
throw new GitattributesCreationFailed($message);
42+
}
43+
44+
/**
45+
* Overwrite an existing gitattributes file.
46+
*
47+
* @param string $content The content of the gitattributes file
48+
* @throws GitattributesCreationFailed
49+
* @return string
50+
*
51+
*/
52+
public function overwriteGitattributesFile(string $content): string
53+
{
54+
$bytesWritten = file_put_contents(
55+
$this->analyser->getGitattributesFilePath(),
56+
$content
57+
);
58+
59+
if ($bytesWritten) {
60+
$content = 'Overwrote it with the shown content:'
61+
. PHP_EOL . '<info>' . $content . '</info>';
62+
63+
return PHP_EOL . PHP_EOL . $content;
64+
}
65+
66+
$message = 'Overwrite of .gitattributes file failed.';
67+
throw new GitattributesCreationFailed($message);
68+
}
69+
}

tests/Commands/ValidateCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ public function failingGitattributesFilesCreationReturnsExpectedStatusCode(): vo
793793
);
794794

795795
$builder = new MockBuilder();
796-
$builder->setNamespace('Stolt\LeanPackage\Commands')
796+
$builder->setNamespace('Stolt\LeanPackage')
797797
->setName('file_put_contents')
798798
->setFunctionProvider(new FixedValueFunction(false));
799799

@@ -1660,7 +1660,7 @@ public function failingGitattributesFilesOverwriteReturnsExpectedStatusCode(): v
16601660
);
16611661

16621662
$builder = new MockBuilder();
1663-
$builder->setNamespace('Stolt\LeanPackage\Commands')
1663+
$builder->setNamespace('Stolt\LeanPackage')
16641664
->setName('file_put_contents')
16651665
->setFunctionProvider(new FixedValueFunction(false));
16661666

0 commit comments

Comments
 (0)