mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Add services and tests for associated entities management
Implemented services to provide associated persons and third parties for accompanying periods and their works. Included comprehensive tests to ensure proper functionality and associations.
This commit is contained in:
parent
968835a262
commit
4f18b1d2b2
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Service\AccompanyingPeriod;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
|
||||
/**
|
||||
* Provide persons associated with an accompanying period.
|
||||
*/
|
||||
class ProvidePersonsAssociated
|
||||
{
|
||||
/**
|
||||
* @return list<Person>
|
||||
*/
|
||||
public function getPersonsAssociated(AccompanyingPeriod $period): array
|
||||
{
|
||||
return array_values(
|
||||
$period->getCurrentParticipations()
|
||||
->map(fn (AccompanyingPeriodParticipation $participation) => $participation->getPerson())
|
||||
->toArray()
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Service\AccompanyingPeriod;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
|
||||
/**
|
||||
* Provide third parties participating to an AccompanyingPeriod.
|
||||
*/
|
||||
class ProvideThirdPartiesAssociated
|
||||
{
|
||||
/**
|
||||
* @return list<ThirdParty>
|
||||
*/
|
||||
public function getThirdPartiesAssociated(AccompanyingPeriod $period): array
|
||||
{
|
||||
$thirdParties = [];
|
||||
|
||||
foreach (
|
||||
$period
|
||||
->getResources()->filter(fn (Resource $resource) => null !== $resource->getThirdParty())
|
||||
->map(fn (Resource $resource) => $resource->getThirdParty()) as $thirdParty) {
|
||||
$thirdParties[] = $thirdParty;
|
||||
}
|
||||
|
||||
if (null !== $requestor = $period->getRequestorThirdParty()) {
|
||||
$thirdParties[] = $requestor;
|
||||
}
|
||||
|
||||
return array_values(
|
||||
// filter objects to remove duplicates
|
||||
array_filter(
|
||||
$thirdParties,
|
||||
fn ($o, $k) => array_search($o, $thirdParties, true) === $k,
|
||||
ARRAY_FILTER_USE_BOTH
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Service\AccompanyingPeriodWork;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Service\AccompanyingPeriod\ProvidePersonsAssociated as ProvidePersonsAssociatedAccompanyingPeriod;
|
||||
|
||||
/**
|
||||
* Provide persons associated with an accompanying period work.
|
||||
*/
|
||||
class ProvidePersonsAssociated
|
||||
{
|
||||
public function __construct(private readonly ProvidePersonsAssociatedAccompanyingPeriod $providePersonsAssociated) {}
|
||||
|
||||
/**
|
||||
* @return list<Person>
|
||||
*/
|
||||
public function getPersonsAssociated(AccompanyingPeriod\AccompanyingPeriodWork $work): array
|
||||
{
|
||||
return $this->providePersonsAssociated->getPersonsAssociated($work->getAccompanyingPeriod());
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Service\AccompanyingPeriodWork;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
||||
use Chill\PersonBundle\Service\AccompanyingPeriod\ProvideThirdPartiesAssociated as ProvideThirdPartiesAssociatedAccompanyingPeriod;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
|
||||
/**
|
||||
* Provide third parties associated with an accompanying period work.
|
||||
*/
|
||||
class ProvideThirdPartiesAssociated
|
||||
{
|
||||
public function __construct(private readonly ProvideThirdPartiesAssociatedAccompanyingPeriod $thirdPartiesAssociated) {}
|
||||
|
||||
/**
|
||||
* @return list<ThirdParty>
|
||||
*/
|
||||
public function getThirdPartiesAssociated(AccompanyingPeriodWork $accompanyingPeriodWork): array
|
||||
{
|
||||
$thirdParties = $this->thirdPartiesAssociated->getThirdPartiesAssociated($accompanyingPeriodWork->getAccompanyingPeriod());
|
||||
|
||||
if (null !== $tp = $accompanyingPeriodWork->getHandlingThierParty()) {
|
||||
$thirdParties[] = $tp;
|
||||
}
|
||||
|
||||
foreach ($accompanyingPeriodWork->getThirdParties() as $thirdParty) {
|
||||
$thirdParties[] = $thirdParty;
|
||||
}
|
||||
|
||||
return array_values(
|
||||
// filter objects to remove duplicates
|
||||
array_filter(
|
||||
$thirdParties,
|
||||
fn ($o, $k) => array_search($o, $thirdParties, true) === $k,
|
||||
ARRAY_FILTER_USE_BOTH
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Service\AccompanyingPeriod;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Service\AccompanyingPeriod\ProvideThirdPartiesAssociated;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class ProvideThirdPartiesAssociatedTest extends TestCase
|
||||
{
|
||||
public function testGetThirdPartiesAssociated()
|
||||
{
|
||||
$period = new AccompanyingPeriod();
|
||||
$period->setRequestor($tp1 = new ThirdParty());
|
||||
$period->addResource((new AccompanyingPeriod\Resource())->setResource($tp1));
|
||||
$period->addResource((new AccompanyingPeriod\Resource())->setResource($tp2 = new ThirdParty()));
|
||||
$period->addResource((new AccompanyingPeriod\Resource())->setResource($p1 = new Person()));
|
||||
|
||||
$provider = new ProvideThirdPartiesAssociated();
|
||||
|
||||
$thirdParties = $provider->getThirdPartiesAssociated($period);
|
||||
|
||||
self::assertCount(2, $thirdParties);
|
||||
self::assertContains($tp1, $thirdParties);
|
||||
self::assertContains($tp2, $thirdParties);
|
||||
self::assertNotContains($p1, $thirdParties);
|
||||
self::assertNotContains(null, $thirdParties);
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Service\AccompanyingPeriodWork;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Service\AccompanyingPeriodWork\ProvideThirdPartiesAssociated;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class ProvideThirdPartiesAssociatedTest extends TestCase
|
||||
{
|
||||
public function testGetThirdPartiesAssociated()
|
||||
{
|
||||
$period = new AccompanyingPeriod();
|
||||
$period->setRequestor($tp1 = new ThirdParty());
|
||||
$period->addResource((new AccompanyingPeriod\Resource())->setResource($tp1));
|
||||
$period->addResource((new AccompanyingPeriod\Resource())->setResource($tp2 = new ThirdParty()));
|
||||
$period->addResource((new AccompanyingPeriod\Resource())->setResource($p1 = new Person()));
|
||||
$period->addWork($work = new AccompanyingPeriod\AccompanyingPeriodWork());
|
||||
$work->addThirdParty($tp3 = new ThirdParty());
|
||||
$work->addThirdParty($tp1);
|
||||
$work->setHandlingThierParty($tp4 = new ThirdParty());
|
||||
|
||||
$providerAccPeriod = new \Chill\PersonBundle\Service\AccompanyingPeriod\ProvideThirdPartiesAssociated();
|
||||
$provider = new ProvideThirdPartiesAssociated($providerAccPeriod);
|
||||
|
||||
|
||||
$thirdParties = $provider->getThirdPartiesAssociated($work);
|
||||
|
||||
self::assertContains($tp1, $thirdParties);
|
||||
self::assertContains($tp2, $thirdParties);
|
||||
self::assertContains($tp3, $thirdParties);
|
||||
self::assertContains($tp4, $thirdParties);
|
||||
self::assertNotContains($p1, $thirdParties);
|
||||
self::assertCount(4, $thirdParties);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user