Skip to content

Commit

Permalink
Merge branch 'feature/problems-problem' into laminas
Browse files Browse the repository at this point in the history
  • Loading branch information
mikola-rollun committed May 3, 2023
2 parents 0f0916a + 1a3aead commit 8ed3a81
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/Domain/Traits/ProblemTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait ProblemTrait
* @var ArrayObject|null
*/
protected $problems;
protected $defaultMaxProblemsPerObject = 10;
protected $defaultMaxProblemsPerObject = 3;

/**
* @param ArrayObjectItemInterface $problem
Expand All @@ -28,29 +28,34 @@ public function addProblemItem(mixed $problem)
$problem = new ArrayObjectItem($problem);
}

$this->clampProblems(getenv('MAX_PROBLEMS_PER_OBJECT') ?: $this->defaultMaxProblemsPerObject);

$problems = $this->getProblems();
$problemsCount = $problems->count();
$maxProblemsPerObject = getenv('MAX_PROBLEMS_PER_OBJECT') ?? $this->defaultMaxProblemsPerObject;
$problems->addItem($problem);
$this->setProblems($problems);
}

if ($problemsCount < $maxProblemsPerObject) {
$this->problems->addItem($problem);
return;
public function clampProblems(int $maxProblemsSize) {
if ($this->problems === null) {
$this->problems = new ArrayObject(true);
}

$problems = $this->getProblems();
$problemsCount = $problems->count();

$problemsArray = $problems->toArray();
$newProblems = new ArrayObject(true);

$deleteRowsCount = $problemsCount - $maxProblemsPerObject;
$deleteRowsCount = $problemsCount - $maxProblemsSize;
for ($i = 0 ; $i < $deleteRowsCount ; $i++) {
array_shift($problemsArray);
}
foreach($problemsArray as $problemItem) {
$arrayObjectProblemItem = new ArrayObjectItem($problemItem);
$newProblems->addItem($arrayObjectProblemItem);
}

$newProblems->addItem($problem);
$this->setProblems($problems);

$this->setProblems($newProblems);
}

/**
Expand Down

0 comments on commit 8ed3a81

Please sign in to comment.