2021-12-06 10:06:55 +01:00

64 lines
2.0 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Chill\ActivityBundle\Repository\ActivityACLAwareRepository;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use function in_array;
class AccompanyingPeriodValidityValidator extends ConstraintValidator
{
private ActivityACLAwareRepository $activityRepository;
public function __construct(ActivityACLAwareRepository $activityRepository)
{
$this->activityRepository = $activityRepository;
}
public function validate($period, Constraint $constraint)
{
if (!$constraint instanceof AccompanyingPeriodValidity) {
throw new UnexpectedTypeException($constraint, AccompanyingPeriodValidity::class);
}
if (!$period instanceof AccompanyingPeriod) {
throw new UnexpectedValueException($period, AccompanyingPeriod::class);
}
$socialIssues = [];
$activities = $this->activityRepository->findByAccompanyingPeriod($period, 'SEE');
foreach ($activities as $activity) {
$socialIssues = $activity->getSocialIssues()->getValues();
}
foreach ($period->getWorks() as $work) {
$socialIssues[] = $work->getSocialAction()->getIssue();
}
foreach ($socialIssues as $si) {
if (!in_array($si, $period->getSocialIssues()->getValues(), true)) {
$this->context
->buildViolation(
$constraint->messageSocialIssueCannotBeDeleted
)
->addViolation();
}
}
}
}