2022-03-13 19:32:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Core\Setting;
|
|
|
|
|
|
|
|
use App\Core\Entity\Setting;
|
|
|
|
use App\Core\Factory\SettingFactory;
|
|
|
|
use App\Core\Manager\EntityManager;
|
|
|
|
use App\Core\Repository\SettingRepositoryQuery;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class SettingManager.
|
|
|
|
*
|
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class SettingManager
|
|
|
|
{
|
2023-10-20 09:44:18 +02:00
|
|
|
public function __construct(
|
|
|
|
protected EntityManager $entityManager,
|
|
|
|
protected SettingRepositoryQuery $query,
|
|
|
|
protected SettingFactory $factory
|
|
|
|
) {
|
2022-03-13 19:32:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function init(string $code, string $section, string $label, $value = null)
|
|
|
|
{
|
|
|
|
$entity = $this->get($code);
|
|
|
|
$isNew = null === $entity;
|
|
|
|
|
|
|
|
if ($isNew) {
|
|
|
|
$entity = $this->factory->create($code);
|
|
|
|
$entity->setValue($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$entity
|
|
|
|
->setSection($section)
|
|
|
|
->setLabel($label)
|
|
|
|
;
|
|
|
|
|
|
|
|
if ($isNew) {
|
|
|
|
$this->entityManager->create($entity);
|
|
|
|
} else {
|
|
|
|
$this->entityManager->update($entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get(string $code): ?Setting
|
|
|
|
{
|
|
|
|
return $this->query->create()
|
|
|
|
->where('.code = :code')
|
|
|
|
->setParameter(':code', $code)
|
|
|
|
->findOne()
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set(string $code, $value): bool
|
|
|
|
{
|
|
|
|
$entity = $this->get($code);
|
|
|
|
|
|
|
|
if (!$entity) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entity->setValue($value);
|
|
|
|
$this->entityManager->update($entity);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|