Skip to content

Commit

Permalink
Fixed psalm errors
Browse files Browse the repository at this point in the history
Signed-off-by: Bastian Schwarz <[email protected]>
  • Loading branch information
Bastian Schwarz committed May 13, 2023
1 parent debd85e commit c40c375
Show file tree
Hide file tree
Showing 17 changed files with 428 additions and 3 deletions.
Empty file removed .cache/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
/docker-compose.yml export-ignore
/infection.json.dist export-ignore
/psalm.xml export-ignore

*.php text=auto eol=lf
*.sh text=auto eol=lf
/run text=auto eol=lf
4 changes: 4 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/phpunit.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codenamephp/deploymentchecks.async",
"description": "",
"description": "Package that let's you run the deployment checks in parallel.",
"type": "library",
"license": "Apache-2.0",
"authors": [
Expand All @@ -10,7 +10,9 @@
}
],
"require": {
"php": "^8.2"
"php": "^8.2",
"codenamephp/deploymentchecks.base": "*",
"spatie/async": "^1.5"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion docker/application/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM webdevops/php-dev:8.1
FROM webdevops/php-dev:8.2

COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer
COPY --from=phario/phive:0.15.2 /usr/local/bin/phive /usr/local/bin/phive
Expand Down
19 changes: 19 additions & 0 deletions run
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

#
# Copyright 2023 Bastian Schwarz <[email protected]>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

docker compose exec -it -u application application bash -c "$*"
30 changes: 30 additions & 0 deletions src/Check/Factory/FromCheck/FromCheckFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace de\codenamephp\deploymentchecks\async\Check\Factory\FromCheck;

use de\codenamephp\deploymentchecks\async\Check\ParallelCheckInterface;
use de\codenamephp\deploymentchecks\base\Check\CheckInterface;

/**
* Interface to create parallel checks from a regular check on the fly
*/
interface FromCheckFactoryInterface
{

public function build(CheckInterface $check): ParallelCheckInterface;
}
46 changes: 46 additions & 0 deletions src/Check/Factory/FromCheck/WithErrorHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace de\codenamephp\deploymentchecks\async\Check\Factory\FromCheck;

use de\codenamephp\deploymentchecks\async\Check\ParallelCheckInterface;
use de\codenamephp\deploymentchecks\async\Check\ParallelCheckWithErrorHandler;
use de\codenamephp\deploymentchecks\async\ErrorHandler\ErrorHandlerInterface;
use de\codenamephp\deploymentchecks\async\ErrorHandler\RethrowException;
use de\codenamephp\deploymentchecks\async\SuccessHandler\AddToResultCollection;
use de\codenamephp\deploymentchecks\async\SuccessHandler\SuccessHandlerInterface;
use de\codenamephp\deploymentchecks\base\Check\CheckInterface;

/**
* Factory to create ParallelCheckWithErrorHandler instances
*
* @psalm-api
*/
final class WithErrorHandlerFactory implements FromCheckFactoryInterface
{
public function __construct(
public SuccessHandlerInterface $successHandler = new AddToResultCollection(),
public ErrorHandlerInterface $errorHandler = new RethrowException(),
) {}

public function build(CheckInterface $check): ParallelCheckInterface
{
return new ParallelCheckWithErrorHandler($check, $this->successHandler, $this->errorHandler);
}


}
42 changes: 42 additions & 0 deletions src/Check/ParallelCheckInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace de\codenamephp\deploymentchecks\async\Check;

use de\codenamephp\deploymentchecks\async\SuccessHandler\SuccessHandlerInterface;
use de\codenamephp\deploymentchecks\base\Check\Result\ResultInterface;

/**
* Interface for checks that can be used in parallel using the async pool
*/
interface ParallelCheckInterface
{

/**
* Since the async pool only accepts callables, this method is used to invoke the check and return the result
*
* @return ResultInterface
*/
public function __invoke(): ResultInterface;

/**
* Gets the success handler that is used to handle the result if the check was successful
*
* @return SuccessHandlerInterface
*/
public function successHandler(): SuccessHandlerInterface;
}
59 changes: 59 additions & 0 deletions src/Check/ParallelCheckWithErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace de\codenamephp\deploymentchecks\async\Check;

use de\codenamephp\deploymentchecks\async\ErrorHandler\ErrorHandlerInterface;
use de\codenamephp\deploymentchecks\async\ErrorHandler\RethrowException;
use de\codenamephp\deploymentchecks\async\SuccessHandler\AddToResultCollection;
use de\codenamephp\deploymentchecks\async\SuccessHandler\SuccessHandlerInterface;
use de\codenamephp\deploymentchecks\base\Check\CheckInterface;
use de\codenamephp\deploymentchecks\base\Check\Result\ResultInterface;

/**
* Basically a wrapper around a check that implements the ParallelCheckInterface and WithErrorHandlerInterface. The spatie async pool expects a callable so this
* class implements that interface and just calls the check. The check itself is passed in the constructor and the success and error handler are optional.
*
* The default AsyncCheckCollection will use this check to run the checks in parallel. It will use the success and error handler as quasi callbacks to handle the result
* of the check.
*
* The default success handler will add the result to the result collection and the default error handler will rethrow the exception.
*/
final readonly class ParallelCheckWithErrorHandler implements ParallelCheckInterface, WithErrorHandlerInterface
{

public function __construct(
public CheckInterface $check,
public SuccessHandlerInterface $successHandler = new AddToResultCollection(),
public ErrorHandlerInterface $errorHandler = new RethrowException(),
) {}

public function __invoke(): ResultInterface
{
return $this->check->run();
}

public function successHandler(): SuccessHandlerInterface
{
return $this->successHandler;
}

public function errorHandler(): ErrorHandlerInterface
{
return $this->errorHandler;
}
}
29 changes: 29 additions & 0 deletions src/Check/WithErrorHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace de\codenamephp\deploymentchecks\async\Check;

use de\codenamephp\deploymentchecks\async\ErrorHandler\ErrorHandlerInterface;

/**
* Interface for parallel checks that want to define a custom error handler
*/
interface WithErrorHandlerInterface
{

public function errorHandler(): ErrorHandlerInterface;
}
61 changes: 61 additions & 0 deletions src/Collection/AsyncCheckCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace de\codenamephp\deploymentchecks\async\Collection;

use de\codenamephp\deploymentchecks\async\Check\Factory\FromCheck\FromCheckFactoryInterface;
use de\codenamephp\deploymentchecks\async\Check\WithErrorHandlerInterface;
use de\codenamephp\deploymentchecks\base\Check\CheckInterface;
use de\codenamephp\deploymentchecks\base\Check\Result\Collection\ResultCollectionInterface;
use de\codenamephp\deploymentchecks\base\Check\Result\ResultInterface;
use Spatie\Async\Pool;
use Throwable;

/**
* @psalm-api
*/
final readonly class AsyncCheckCollection implements CheckInterface
{

/**
* @var array<CheckInterface>
*/
public array $checks;

public function __construct(
public Pool $pool,
public ResultCollectionInterface $resultCollection,
public FromCheckFactoryInterface $fromCheckFactory,
CheckInterface ...$checks)
{
$this->checks = $checks;
}

public function run(): ResultInterface
{
foreach ($this->checks as $check) {
$parallelCheck = $this->fromCheckFactory->build($check);
$runnable = $this->pool
->add($parallelCheck)
->then(fn(ResultInterface $output) => $parallelCheck->successHandler()->handle($this->resultCollection, $output));
if ($parallelCheck instanceof WithErrorHandlerInterface) $runnable->catch(fn(Throwable $exception): mixed => $parallelCheck->errorHandler()->handle($this->resultCollection, $exception));
}
$this->pool->wait();
return $this->resultCollection;
}
}
30 changes: 30 additions & 0 deletions src/ErrorHandler/ErrorHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace de\codenamephp\deploymentchecks\async\ErrorHandler;

use de\codenamephp\deploymentchecks\base\Check\Result\Collection\ResultCollectionInterface;
use Throwable;

/**
* Interface for a simple error handler. It expects the result collection build in the async collection and the exception that was thrown.
*/
interface ErrorHandlerInterface
{

public function handle(ResultCollectionInterface $resultCollection, Throwable $exception): mixed;
}
Loading

0 comments on commit c40c375

Please sign in to comment.