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