. */ namespace SP\Infrastructure\Config\Repositories; use SP\Domain\Config\Models\Config as ConfigModel; use SP\Domain\Config\Ports\ConfigRepository; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Infrastructure\Common\Repositories\BaseRepository; use SP\Infrastructure\Database\QueryData; use SP\Infrastructure\Database\QueryResult; /** * Class Config * * @template T of ConfigModel */ final class Config extends BaseRepository implements ConfigRepository { public const TABLE = 'Config'; /** * @param ConfigModel $config * * @return QueryResult * @throws ConstraintException * @throws QueryException */ public function update(ConfigModel $config): QueryResult { $query = $this->queryFactory ->newUpdate() ->table(self::TABLE) ->cols($config->toArray(['value'])) ->where('parameter = :parameter') ->limit(1) ->bindValues( [ 'value' => $config->getValue(), 'parameter' => $config->getParameter() ] ); $queryData = QueryData::build($query); return $this->db->runQuery($queryData); } /** * @param ConfigModel $config * * @return QueryResult * @throws ConstraintException * @throws QueryException */ public function create(ConfigModel $config): QueryResult { $query = $this->queryFactory ->newInsert() ->into(self::TABLE) ->cols($config->toArray()); $queryData = QueryData::build($query); return $this->db->runQuery($queryData); } /** * @param string $param * * @return QueryResult * @throws ConstraintException * @throws QueryException */ public function getByParam(string $param): QueryResult { $query = $this->queryFactory ->newSelect() ->from(self::TABLE) ->cols(ConfigModel::getCols()) ->where('parameter = :parameter') ->bindValues(['parameter' => $param]) ->limit(1); $queryData = QueryData::buildWithMapper($query, ConfigModel::class); return $this->db->runQuery($queryData); } /** * @param string $param * * @return bool * @throws ConstraintException * @throws QueryException */ public function has(string $param): bool { $query = $this->queryFactory ->newSelect() ->from(self::TABLE) ->cols(ConfigModel::getCols(['value'])) ->where('parameter = :parameter') ->bindValues(['parameter' => $param]) ->limit(1); $queryData = QueryData::build($query); return $this->db->runQuery($queryData)->getNumRows() === 1; } }