Skip to content

Commit

Permalink
Merge pull request #145 from ajardin/feature/refactoring
Browse files Browse the repository at this point in the history
Minor refactoring
  • Loading branch information
ajardin committed May 19, 2020
2 parents 3ee7be6 + f9bd569 commit c20f1e1
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 30 deletions.
9 changes: 6 additions & 3 deletions src/Environment/EnvironmentEntity.php
Expand Up @@ -75,10 +75,13 @@ public function isActive(): bool
return $this->active;
}

public function setActive(bool $active): self
public function activate(): void
{
$this->active = $active;
$this->active = true;
}

return $this;
public function deactivate(): void
{
$this->active = false;
}
}
6 changes: 3 additions & 3 deletions src/EventSubscriber/CommandSubscriber.php
Expand Up @@ -4,7 +4,7 @@

namespace App\EventSubscriber;

use App\Exception\InvalidConfigurationException;
use App\Exception\MissingRequirementException;
use App\Helper\BinaryChecker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
Expand Down Expand Up @@ -42,7 +42,7 @@ public static function getSubscribedEvents(): array
/**
* Checks whether all required environment variables are set before executing any commands.
*
* @throws InvalidConfigurationException
* @throws MissingRequirementException
*/
public function onConsoleCommand(ConsoleCommandEvent $event): void
{
Expand Down Expand Up @@ -73,7 +73,7 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
}, $result)
);

throw new InvalidConfigurationException('At least one binary is missing from your system.');
throw new MissingRequirementException('At least one binary is missing from your system.');
}
}
}
4 changes: 2 additions & 2 deletions src/EventSubscriber/EnvironmentSubscriber.php
Expand Up @@ -93,7 +93,7 @@ public function onEnvironmentStart(EnvironmentStartedEvent $event): void
}
}

$environment->setActive(true);
$environment->activate();
$this->database->save();
}

Expand All @@ -118,7 +118,7 @@ public function onEnvironmentStop(EnvironmentStoppedEvent $event): void
}
}

$environment->setActive(false);
$environment->deactivate();
$this->database->save();
}

Expand Down
11 changes: 11 additions & 0 deletions src/Exception/MissingRequirementException.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace App\Exception;

use Exception;

class MissingRequirementException extends Exception implements OrigamiExceptionInterface
{
}
2 changes: 2 additions & 0 deletions src/Helper/CurrentContext.php
Expand Up @@ -5,6 +5,7 @@
namespace App\Helper;

use App\Environment\EnvironmentEntity;
use App\Exception\FilesystemException;
use App\Exception\InvalidEnvironmentException;
use App\Middleware\Binary\DockerCompose;
use App\Middleware\Database;
Expand All @@ -31,6 +32,7 @@ public function __construct(Database $database, ProcessProxy $processProxy, Dock
/**
* Attempts to load the environment to manage in different ways and throws an exception if this is not possible.
*
* @throws FilesystemException
* @throws InvalidEnvironmentException
*/
public function getEnvironment(InputInterface $input): EnvironmentEntity
Expand Down
6 changes: 3 additions & 3 deletions src/Helper/ProcessProxy.php
Expand Up @@ -4,7 +4,7 @@

namespace App\Helper;

use App\Exception\InvalidEnvironmentException;
use App\Exception\FilesystemException;
use Symfony\Component\Process\Process;

/**
Expand All @@ -15,12 +15,12 @@ class ProcessProxy
/**
* Retrieves the current working directory, or throws \App\Exception\InvalidEnvironmentException on failure.
*
* @throws InvalidEnvironmentException
* @throws FilesystemException
*/
public function getWorkingDirectory(): string
{
if (!$cwd = getcwd()) {
throw new InvalidEnvironmentException('Unable to determine the current working directory.');
throw new FilesystemException('Unable to determine the current working directory.');
}

return $cwd;
Expand Down
7 changes: 4 additions & 3 deletions src/Middleware/Binary/DockerCompose.php
Expand Up @@ -5,6 +5,7 @@
namespace App\Middleware\Binary;

use App\Environment\EnvironmentEntity;
use App\Exception\InvalidConfigurationException;
use App\Exception\InvalidEnvironmentException;
use App\Helper\ProcessFactory;
use App\Validator\Constraints\ConfigurationFiles;
Expand Down Expand Up @@ -193,7 +194,7 @@ public function removeServices(): bool
/**
* Checks whether the environment has been installed and correctly configured.
*
* @throws InvalidEnvironmentException
* @throws InvalidConfigurationException
*/
private function checkEnvironmentConfiguration(EnvironmentEntity $environment): void
{
Expand All @@ -203,13 +204,13 @@ private function checkEnvironmentConfiguration(EnvironmentEntity $environment):
$dotenv = new Dotenv(true);
$dotenv->overload(sprintf('%s/var/docker/.env', $environment->getLocation()));
} else {
throw new InvalidEnvironmentException($errors[0]->getMessage());
throw new InvalidConfigurationException($errors[0]->getMessage());
}

$filesConstraint = new ConfigurationFiles();
$errors = $this->validator->validate($environment, $filesConstraint);
if ($errors->has(0)) {
throw new InvalidEnvironmentException($errors[0]->getMessage());
throw new InvalidConfigurationException($errors[0]->getMessage());
}
}
}
4 changes: 3 additions & 1 deletion src/Middleware/Database.php
Expand Up @@ -6,6 +6,7 @@

use App\Environment\EnvironmentCollection;
use App\Environment\EnvironmentEntity;
use App\Exception\FilesystemException;
use App\Exception\InvalidEnvironmentException;
use Ergebnis\Environment\Variables;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
Expand All @@ -27,6 +28,7 @@ class Database
private $environments;

/**
* @throws FilesystemException
* @throws InvalidEnvironmentException
*/
public function __construct(Variables $systemVariables)
Expand All @@ -37,7 +39,7 @@ public function __construct(Variables $systemVariables)

$this->path = $home.\DIRECTORY_SEPARATOR.self::DATABASE_FILENAME;
if (!@is_file($this->path) && !@touch($this->path)) {
throw new InvalidEnvironmentException('Unable to create the database file.'); // @codeCoverageIgnore
throw new FilesystemException('Unable to create the database file.'); // @codeCoverageIgnore
}

$this->serializer = new Serializer([new ObjectNormalizer(), new ArrayDenormalizer()], [new JsonEncoder()]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/Main/StartCommandTest.php
Expand Up @@ -84,7 +84,7 @@ public function testItStartsTheEnvironment(): void
public function testItDoesNotStartMultipleEnvironments(): void
{
$environment = $this->getFakeEnvironment();
$environment->setActive(true);
$environment->activate();

(new MethodProphecy($this->currentContext, 'getEnvironment', [Argument::type(InputInterface::class)]))
->shouldBeCalledOnce()
Expand Down
4 changes: 2 additions & 2 deletions tests/Command/Main/UninstallCommandTest.php
Expand Up @@ -58,7 +58,7 @@ protected function setUp(): void
public function testItUninstallsTheCurrentEnvironment(): void
{
$environment = $this->getFakeEnvironment();
$environment->setActive(false);
$environment->deactivate();

(new MethodProphecy($this->currentContext, 'getEnvironment', [Argument::type(InputInterface::class)]))
->shouldBeCalledOnce()
Expand Down Expand Up @@ -90,7 +90,7 @@ public function testItUninstallsTheCurrentEnvironment(): void
public function testItDoesNotUninstallARunningEnvironment(): void
{
$environment = $this->getFakeEnvironment();
$environment->setActive(true);
$environment->activate();

(new MethodProphecy($this->currentContext, 'getEnvironment', [Argument::type(InputInterface::class)]))
->shouldBeCalledOnce()
Expand Down
10 changes: 5 additions & 5 deletions tests/EventSubscriber/CommandSubscriberTest.php
Expand Up @@ -5,7 +5,7 @@
namespace App\Tests\EventSubscriber;

use App\EventSubscriber\CommandSubscriber;
use App\Exception\InvalidConfigurationException;
use App\Exception\MissingRequirementException;
use App\Helper\BinaryChecker;
use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy;
Expand Down Expand Up @@ -49,7 +49,7 @@ protected function tearDown(): void
}

/**
* @throws InvalidConfigurationException
* @throws MissingRequirementException
*/
public function testItDoesNotCheckRequirementsWithSymfonyCommands(): void
{
Expand All @@ -76,7 +76,7 @@ public function testItDoesNotCheckRequirementsWithSymfonyCommands(): void
}

/**
* @throws InvalidConfigurationException
* @throws MissingRequirementException
*/
public function testItDetectsExistingBinaryWithOrigamiCommands(): void
{
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testItDetectsExistingBinaryWithOrigamiCommands(): void
}

/**
* @throws InvalidConfigurationException
* @throws MissingRequirementException
*/
public function testItDetectsMissingBinaryWithOrigamiCommands(): void
{
Expand All @@ -123,7 +123,7 @@ public function testItDetectsMissingBinaryWithOrigamiCommands(): void
;

$this->expectExceptionObject(
new InvalidConfigurationException('At least one binary is missing from your system.')
new MissingRequirementException('At least one binary is missing from your system.')
);

$subscriber = new CommandSubscriber($requirements, $binaryChecker->reveal());
Expand Down
8 changes: 4 additions & 4 deletions tests/EventSubscriber/EnvironmentSubscriberTest.php
Expand Up @@ -100,7 +100,7 @@ public function testItStartsTheDockerSynchronizationWithSuccess(): void
$environment = $this->prophet->prophesize(EnvironmentEntity::class);
$this->prophesizeCommonMethods($environment);

(new MethodProphecy($environment, 'setActive', [true]))
(new MethodProphecy($environment, 'activate', []))
->shouldBeCalledOnce()
;

Expand Down Expand Up @@ -149,7 +149,7 @@ public function testItStartsTheDockerSynchronizationWithoutSuccess(): void
$environment = $this->prophet->prophesize(EnvironmentEntity::class);
$this->prophesizeCommonMethods($environment);

(new MethodProphecy($environment, 'setActive', [true]))
(new MethodProphecy($environment, 'activate', []))
->shouldBeCalledOnce()
;

Expand Down Expand Up @@ -193,7 +193,7 @@ public function testItStopsTheDockerSynchronizationWithSuccess(): void
$environment = $this->prophet->prophesize(EnvironmentEntity::class);
$this->prophesizeCommonMethods($environment);

(new MethodProphecy($environment, 'setActive', [false]))
(new MethodProphecy($environment, 'deactivate', []))
->shouldBeCalledOnce()
;

Expand Down Expand Up @@ -232,7 +232,7 @@ public function testItStopsTheDockerSynchronizationWithoutSuccess(): void
$environment = $this->prophet->prophesize(EnvironmentEntity::class);
$this->prophesizeCommonMethods($environment);

(new MethodProphecy($environment, 'setActive', [false]))
(new MethodProphecy($environment, 'deactivate', []))
->shouldBeCalledOnce()
;

Expand Down
5 changes: 3 additions & 2 deletions tests/Middleware/Binary/DockerComposeDefaultTest.php
Expand Up @@ -5,6 +5,7 @@
namespace App\Tests\Middleware\Binary;

use App\Environment\EnvironmentEntity;
use App\Exception\InvalidConfigurationException;
use App\Exception\InvalidEnvironmentException;
use App\Middleware\Binary\DockerCompose;
use App\Validator\Constraints\ConfigurationFiles;
Expand Down Expand Up @@ -94,7 +95,7 @@ public function testItThrowsAnExceptionWithMissingDotEnvFile(): void
->shouldNotBeCalled()
;

$this->expectException(InvalidEnvironmentException::class);
$this->expectException(InvalidConfigurationException::class);

$dockerCompose = new DockerCompose($this->validator->reveal(), $this->processFactory->reveal());
$dockerCompose->setActiveEnvironment($this->environment);
Expand Down Expand Up @@ -124,7 +125,7 @@ public function testItThrowsAnExceptionWithMissingConfigurationFiles(): void
->willReturn($errors)
;

$this->expectException(InvalidEnvironmentException::class);
$this->expectException(InvalidConfigurationException::class);

$dockerCompose = new DockerCompose($this->validator->reveal(), $this->processFactory->reveal());
$dockerCompose->setActiveEnvironment($this->environment);
Expand Down
3 changes: 2 additions & 1 deletion tests/Middleware/DatabaseTest.php
Expand Up @@ -6,6 +6,7 @@

use App\Environment\EnvironmentCollection;
use App\Environment\EnvironmentEntity;
use App\Exception\FilesystemException;
use App\Exception\InvalidEnvironmentException;
use App\Middleware\Database;
use App\Tests\TestLocationTrait;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function testItCreatesTheDatabaseFile(): void
*/
public function testItThrowsAnExceptionIfTheDatabaseIsNotCreated(): void
{
$this->expectException(InvalidEnvironmentException::class);
$this->expectException(FilesystemException::class);

$fakeVariables = FakeVariables::fromArray(['HOME' => '/fake/location']);
new Database($fakeVariables);
Expand Down

0 comments on commit c20f1e1

Please sign in to comment.