Skip to content

Commit

Permalink
Adds minor code tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Jul 4, 2024
1 parent 58e59cf commit a42b906
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
70 changes: 33 additions & 37 deletions src/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getFoundUnexpectedArtifacts(): array
/**
* Has repository a HEAD.
*
* @throws \Stolt\LeanPackage\Exceptions\GitNotAvailable
* @throws GitNotAvailable
*
* @return boolean
*/
Expand All @@ -115,46 +115,45 @@ public function hasHead(): bool
}

/**
* Is the Git command available?
*
* @param string $command The command to check availabilty of. Defaults to git.
* Create a Git archive from the current HEAD.
*
* @throws GitHeadNotAvailable|GitNotAvailable
* @return boolean
*/
public function isGitCommandAvailable($command = 'git'): bool
public function createArchive(): bool
{
\exec('where ' . $command . ' 2>&1', $output, $returnValue);
if ((new OsHelper())->isWindows() === false) {
\exec('which ' . $command . ' 2>&1', $output, $returnValue);
if ($this->hasHead()) {
$command = 'git archive -o ' . $this->getFilename() . ' HEAD 2>&1';
\exec($command, $output, $returnValue);

return $returnValue === 0;
}

return $returnValue === 0;
throw new GitHeadNotAvailable('No Git HEAD present to create an archive from.');
}

/**
* Create a Git archive from the current HEAD.
* Is the Git command available?
*
* @throws \Stolt\LeanPackage\Exceptions\GitHeadNotAvailable
* @param string $command The command to check availabilty of. Defaults to git.
*
* @return boolean
*/
public function createArchive(): bool
public function isGitCommandAvailable($command = 'git'): bool
{
if ($this->hasHead()) {
$command = 'git archive -o ' . $this->getFilename() . ' HEAD 2>&1';
\exec($command, $output, $returnValue);

return $returnValue === 0;
\exec('where ' . $command . ' 2>&1', $output, $returnValue);
if ((new OsHelper())->isWindows() === false) {
\exec('which ' . $command . ' 2>&1', $output, $returnValue);
}

throw new GitHeadNotAvailable('No Git HEAD present to create an archive from.');
return $returnValue === 0;
}

/**
* Compare archive against unexpected artifacts.
*
* @param array $unexpectedArtifacts The unexpected artifacts.
* @throws \Stolt\LeanPackage\Exceptions\NoLicenseFilePresent
* @throws NoLicenseFilePresent
* @return array
*/
public function compareArchive(array $unexpectedArtifacts): array
Expand Down Expand Up @@ -195,29 +194,12 @@ public function compareArchive(array $unexpectedArtifacts): array
return $foundUnexpectedArtifacts;
}

/**
* Remove temporary Git archive.
*
* @return boolean
*/
public function removeArchive(): bool
{
if (\file_exists($this->getFilename())) {
return \unlink($this->getFilename());
}

return false;
}

/**
* Delegator for temporary archive creation and comparison against
* a set of unexpected artifacts.
*
* @param array $unexpectedArtifacts The unexpected artifacts of the archive.
*
* @throws \Stolt\LeanPackage\Exceptions\GitNotAvailable
* @throws \Stolt\LeanPackage\Exceptions\GitHeadNotAvailable
*
* @throws GitHeadNotAvailable|NoLicenseFilePresent|GitNotAvailable
* @return array
*/
public function getUnexpectedArchiveArtifacts(array $unexpectedArtifacts): array
Expand All @@ -228,4 +210,18 @@ public function getUnexpectedArchiveArtifacts(array $unexpectedArtifacts): array

return $this->foundUnexpectedArtifacts;
}

/**
* Remove temporary Git archive.
*
* @return boolean
*/
public function removeArchive(): bool
{
if (\file_exists($this->getFilename())) {
return \unlink($this->getFilename());
}

return false;
}
}
21 changes: 12 additions & 9 deletions src/Archive/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use Stolt\LeanPackage\Archive;
use Stolt\LeanPackage\Exceptions\GitArchiveNotValidatedYet;
use Stolt\LeanPackage\Exceptions\GitHeadNotAvailable;
use Stolt\LeanPackage\Exceptions\GitNotAvailable;
use Stolt\LeanPackage\Exceptions\NoLicenseFilePresent;

class Validator
{
/**
* @var \Stolt\LeanPackage\Archive
* @var Archive
*/
private Archive $archive;

Expand All @@ -20,7 +23,7 @@ class Validator
/**
* Initialise.
*
* @param \Stolt\LeanPackage\Archive $archive The archive to validate.
* @param Archive $archive The archive to validate.
*/
public function __construct(Archive $archive)
{
Expand All @@ -30,7 +33,7 @@ public function __construct(Archive $archive)
/**
* Set if license file presence should be validated.
*
* @return \Stolt\LeanPackage\Archive\Validator
* @return Validator
*/
public function shouldHaveLicenseFile(): Validator
{
Expand All @@ -42,7 +45,7 @@ public function shouldHaveLicenseFile(): Validator
/**
* Accessor for injected archive instance.
*
* @return \Stolt\LeanPackage\Archive
* @return Archive
*/
public function getArchive(): Archive
{
Expand All @@ -54,10 +57,10 @@ public function getArchive(): Archive
*
* @param array $unexpectedArtifacts Artifacts not expected in archive.
*
* @throws \Stolt\LeanPackage\Exceptions\GitHeadNotAvailable
* @throws \Stolt\LeanPackage\Exceptions\GitNotAvailable
* @throws \Stolt\LeanPackage\Exceptions\GitHeadNotAvailable
* @throws \Stolt\LeanPackage\Exceptions\GitNotAvailable
* @throws GitNotAvailable
* @throws GitHeadNotAvailable
* @throws GitNotAvailable|NoLicenseFilePresent
* @throws GitHeadNotAvailable
* @return boolean
*/
public function validate(array $unexpectedArtifacts): bool
Expand All @@ -77,7 +80,7 @@ public function validate(array $unexpectedArtifacts): bool
/**
* Accessor for found unexpected archive artifacts.
*
* @throws \Stolt\LeanPackage\Exceptions\GitArchiveNotValidatedYet
* @throws GitArchiveNotValidatedYet
*
* @return array
*/
Expand Down

0 comments on commit a42b906

Please sign in to comment.