Brak opisu

FeatureFlagsRepository.php 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace MailPoet\Features;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Doctrine\Repository;
  5. use MailPoet\Entities\FeatureFlagEntity;
  6. /**
  7. * @extends Repository<FeatureFlagEntity>
  8. */
  9. class FeatureFlagsRepository extends Repository {
  10. protected function getEntityClassName() {
  11. return FeatureFlagEntity::class;
  12. }
  13. /**
  14. * @param array $data
  15. * @throws \RuntimeException
  16. * @throws \InvalidArgumentException
  17. * @return FeatureFlagEntity
  18. */
  19. public function createOrUpdate(array $data = []) {
  20. if (!$data['name']) {
  21. throw new \InvalidArgumentException('Missing name');
  22. }
  23. $featureFlag = $this->findOneBy([
  24. 'name' => $data['name'],
  25. ]);
  26. if (!$featureFlag) {
  27. $featureFlag = new FeatureFlagEntity($data['name']);
  28. $this->persist($featureFlag);
  29. }
  30. if (array_key_exists('value', $data)) {
  31. $featureFlag->setValue($data['value']);
  32. }
  33. try {
  34. $this->flush();
  35. } catch (\Exception $e) {
  36. throw new \RuntimeException("Error when saving feature " . $data['name']);
  37. }
  38. return $featureFlag;
  39. }
  40. }