Skip to content
This repository has been archived by the owner on Mar 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from ph-ash/fix-stop-push-into-leafs
Browse files Browse the repository at this point in the history
Decline pushes into leafs
  • Loading branch information
cawolf committed Apr 3, 2019
2 parents 51043f3 + 5609eed commit b94afd6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 16 deletions.
1 change: 1 addition & 0 deletions config/validator/validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ App\Dto\MonitoringData:
properties:
path:
- \App\Validator\Constraints\IsBranch: ~
- \App\Validator\Constraints\IsLeaf: ~
tileExpansionGrowthExpression:
- Regex:
pattern: '/[\+\*]\s*\d+/'
Expand Down
8 changes: 6 additions & 2 deletions src/Repository/MonitoringData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use App\Document\MonitoringData as MonitoringDataDocument;
use App\Exception\PersistenceLayerException;
use Doctrine\ODM\MongoDB\Cursor;

interface MonitoringData
{
Expand All @@ -20,7 +19,12 @@ public function save(MonitoringDataDocument $monitoringData): void;
/**
* @throws PersistenceLayerException
*/
public function findLeafs(string $path): Cursor;
public function isPathIncludedInBranch(string $path): bool;

/**
* @throws PersistenceLayerException
*/
public function isLeafIncludedInPath(string $path): bool;

/**
* @throws PersistenceLayerException
Expand Down
25 changes: 21 additions & 4 deletions src/Repository/MonitoringDataRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use App\Repository\MonitoringData as MonitoringDataInterface;
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository;
use Doctrine\ODM\MongoDB\Cursor;
use Doctrine\ODM\MongoDB\LockException;
use Doctrine\ODM\MongoDB\LockMode;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
Expand Down Expand Up @@ -68,17 +67,35 @@ public function save(MonitoringData $monitoringData): void
}
}

public function findLeafs(string $path): Cursor
public function isPathIncludedInBranch(string $path): bool
{
$qb = $this->getDocumentManager()->createQueryBuilder(MonitoringData::class);
try {
$qb->field('path')->equals(new MongoRegex('/' . $path . '..*/'))->limit(1);
return $qb->getQuery()->execute();
return $qb->getQuery()->execute()->count(true) > 0;
} catch (Exception $exception) {
throw new PersistenceLayerException('Failed to find leafs.', 0, $exception);
throw new PersistenceLayerException('Failed to find branches including path.', 0, $exception);
}
}

public function isLeafIncludedInPath(string $path): bool
{
$fragments = explode('.', $path);
while (count($fragments) >= 1) {
$potentialId = array_pop($fragments);
$potentialPath = implode('.', $fragments) ?: null;
try {
$matchedById = $this->find($potentialId);
} catch (Exception $exception) {
throw new PersistenceLayerException('Failed to find potential leafs included in path.', 0, $exception);
}
if ($matchedById && $matchedById->getPath() === $potentialPath) {
return true;
}
}
return false;
}

public function delete(string $id): void
{
try {
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Constraints/IsBranch.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

class IsBranch extends Constraint
{
public $message = 'Path \'{{path}}\' is a Branch and can not be deleted or contain MonitoringData';
public $message = 'Path \'{{path}}\' is a Branch and can not contain MonitoringData, please change your path';
}
14 changes: 5 additions & 9 deletions src/Validator/Constraints/IsBranchValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace App\Validator\Constraints;

use App\Exception\PersistenceLayerException;
use App\Repository\MonitoringDataRepository;
use App\Repository\MonitoringData as MonitoringDataRepository;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
Expand All @@ -30,14 +30,10 @@ public function validate($value, Constraint $constraint): void
throw new UnexpectedTypeException($constraint, IsBranch::class);
}

if ($value !== null) {
$result = $this->monitoringDataRepository->findLeafs($value);

if ($result->count(true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{path}}', $value)
->addViolation();
}
if (($value !== null) && $this->monitoringDataRepository->isPathIncludedInBranch($value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{path}}', $value)
->addViolation();
}
}
}
12 changes: 12 additions & 0 deletions src/Validator/Constraints/IsLeaf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

class IsLeaf extends Constraint
{
public $message = 'Path \'{{path}}\' includes a MonitoringData leaf, please change your path';
}
39 changes: 39 additions & 0 deletions src/Validator/Constraints/IsLeafValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Validator\Constraints;

use App\Exception\PersistenceLayerException;
use App\Repository\MonitoringData as MonitoringDataRepository;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class IsLeafValidator extends ConstraintValidator
{
private $monitoringDataRepository;

public function __construct(MonitoringDataRepository $monitoringDataRepository)
{
$this->monitoringDataRepository = $monitoringDataRepository;
}

/**
* @param mixed $value
* @throws PersistenceLayerException
* @throws UnexpectedTypeException
*/
public function validate($value, Constraint $constraint): void
{
if (!$constraint instanceof IsLeaf) {
throw new UnexpectedTypeException($constraint, IsLeaf::class);
}

if (($value !== null) && $this->monitoringDataRepository->isLeafIncludedInPath($value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{path}}', $value)
->addViolation();
}
}
}

0 comments on commit b94afd6

Please sign in to comment.