Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Nov 21, 2024
1 parent 73b63da commit dbeef36
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 60 deletions.
79 changes: 79 additions & 0 deletions app/Domain/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\Domain;

use App\Exceptions\InvalidConfiguration;
use Symfony\Component\Yaml\Yaml;

class Configuration
{
const CONFIGURATION_FILE = '.pa.yml';

private string $config;

private mixed $yamlConfig = null;

public function __construct(readonly PackageAnalyser $analyser, $directoryWithConfigFile = null)
{
$this->config = realpath($directoryWithConfigFile.DIRECTORY_SEPARATOR.self::CONFIGURATION_FILE);

if ($directoryWithConfigFile === null) {
$this->config = realpath(getcwd().DIRECTORY_SEPARATOR.self::CONFIGURATION_FILE);
}
}

/**
* @throws InvalidConfiguration
*/
public function hasConfiguration(): bool
{
if (file_exists($this->config)) {
if ($this->yamlConfig === null) {
$this->yamlConfig = Yaml::parseFile($this->config);
}
if (is_array($this->yamlConfig)) {
if (array_key_exists('violationThreshold', $this->yamlConfig) && is_int($this->yamlConfig['violationThreshold']) === false) {
throw new InvalidConfiguration('No numeric violation threshold provided.');
}
if (array_key_exists('stepsToOmit', $this->yamlConfig)) {
if (is_array($this->yamlConfig['stepsToOmit']) === false) {
throw new InvalidConfiguration('No array of steps to omit provided.');
}
$availableStepsToOmit = array_map(function ($step) {
return $step['id'];
}, $this->analyser->getSteps());

foreach ($this->yamlConfig['stepsToOmit'] as $stepToOmit) {
if (! in_array($stepToOmit, $availableStepsToOmit)) {
throw new InvalidConfiguration(sprintf("Unknown step '%s' provided.", $stepToOmit));
}
}
}

return true;
}

return false;
}

return false;
}

public function getStepsToOmit(): array
{
if ($this->yamlConfig === null) {
$this->yamlConfig = Yaml::parseFile($this->config);
}

return $this->yamlConfig['stepsToOmit'];
}

public function getViolationThreshold(): int
{
if ($this->yamlConfig === null) {
$this->yamlConfig = Yaml::parseFile($this->config);
}

return $this->yamlConfig['violationThreshold'];
}
}
5 changes: 5 additions & 0 deletions app/Exceptions/InvalidConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace App\Exceptions;

class InvalidConfiguration extends \Exception {}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
"require": {
"php": "^8.2",
"laravel-zero/framework": "^11.0",
"nunomaduro/termwind": "^2.0"
"nunomaduro/termwind": "^2.0",
"symfony/yaml": "^7.1"
},
"require-dev": {
"laravel/pint": "^1.17",
"mockery/mockery": "^1.6",
"pestphp/pest": "^2.22",
"pestphp/pest": "^2.35",
"stolt/lean-package-validator": "^4.0.0"
},
"autoload": {
Expand Down
Loading

0 comments on commit dbeef36

Please sign in to comment.