Skip to content

Commit

Permalink
Merge pull request #7 from ipublikuj/feature/exceptions
Browse files Browse the repository at this point in the history
Override exceptions
  • Loading branch information
akadlec authored Aug 4, 2024
2 parents 5d52d4b + 2596174 commit 9c64c1e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,20 @@ jobs:
run: "${{ env.composer-install }}"

- name: "Tests"
run: "make coverage"
run: "make coverage-clover"

- name: "Upload logs"
uses: "actions/upload-artifact@v3"
with:
name: "Logs - Tests (${{ runner.os }}, ${{ matrix.php-version }})"
path: "var/log"
if-no-files-found: "ignore"

- name: "Coveralls.io"
env:
CI_NAME: github
CI: true
COVERALLS_REPO_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
run: |
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.1.0/php-coveralls.phar
php php-coveralls.phar --verbose --config tests/.coveralls.yml
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.5.3/php-coveralls.phar
php php-coveralls.phar --verbose --config tools/.coveralls.yml
8 changes: 5 additions & 3 deletions src/Crud/Create/EntityCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ public function __construct(
}

/**
* @throws DBAL\Exception\UniqueConstraintViolationException
* @throws Exceptions\EntityCreation
* @throws Exceptions\InvalidArgument
* @throws Exceptions\InvalidState
* @throws ORM\Exception\ORMException
*/
public function create(Utils\ArrayHash $values, Entities\IEntity|null $entity = null): Entities\IEntity
{
Expand Down Expand Up @@ -121,7 +119,11 @@ public function create(Utils\ArrayHash $values, Entities\IEntity|null $entity =
Utils\Arrays::invoke($this->afterAction, $entity, $values);

if ($this->getFlush()) {
$this->entityManager->flush();
try {
$this->entityManager->flush();
} catch (DBAL\Exception\UniqueConstraintViolationException | ORM\Exception\ORMException $ex) {
throw new Exceptions\InvalidState('Entity could not be created', $ex->getCode(), $ex);
}
}

return $entity;
Expand Down
19 changes: 11 additions & 8 deletions src/Crud/Delete/EntityDeleter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ class EntityDeleter extends Crud\CrudManager
public array $afterAction = [];

/**
* @throws DBAL\Exception
* @throws Exceptions\InvalidArgument
* @throws ORM\Exception\ORMException
* @throws Exceptions\InvalidState
*/
public function delete(Entities\IEntity|int|string $entity): bool
{
Expand All @@ -59,15 +58,19 @@ public function delete(Entities\IEntity|int|string $entity): bool

Utils\Arrays::invoke($this->beforeAction, $entity);

$this->entityManager->getConnection()->beginTransaction();
try {
$this->entityManager->getConnection()->beginTransaction();

$this->entityManager->remove($entity);
$this->entityManager->remove($entity);

if ($this->getFlush() === true) {
$this->entityManager->flush();
}
if ($this->getFlush() === true) {
$this->entityManager->flush();
}

$this->entityManager->getConnection()->commit();
$this->entityManager->getConnection()->commit();
} catch (ORM\Exception\ORMException | DBAL\Exception $ex) {
throw new Exceptions\InvalidState('Entity could not be deleted', $ex->getCode(), $ex);
}

Utils\Arrays::invoke($this->afterAction);

Expand Down
9 changes: 6 additions & 3 deletions src/Crud/Update/EntityUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ public function __construct(
}

/**
* @throws DBAL\Exception\UniqueConstraintViolationException
* @throws Exceptions\InvalidArgument
* @throws ORM\Exception\ORMException
* @throws Exceptions\InvalidState
*/
public function update(Utils\ArrayHash $values, Entities\IEntity|int|string $entity): Entities\IEntity
{
Expand All @@ -80,7 +79,11 @@ public function update(Utils\ArrayHash $values, Entities\IEntity|int|string $ent
Utils\Arrays::invoke($this->afterAction, $entity, $values);

if ($this->getFlush() === true) {
$this->entityManager->flush();
try {
$this->entityManager->flush();
} catch (DBAL\Exception\UniqueConstraintViolationException | ORM\Exception\ORMException $ex) {
throw new Exceptions\InvalidState('Entity could not be updated', $ex->getCode(), $ex);
}
}

return $entity;
Expand Down

0 comments on commit 9c64c1e

Please sign in to comment.