knot-lib/datastore-services

View on GitHub
src/util/DataStoreStringTrait.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php
declare(strict_types=1);

namespace knotlib\datastoreservices\util;

use Psr\Container\ContainerInterface as PsrContainerInterface;

use knotlib\datastoreservices\DI;
use knotlib\services\exception\StringTypeException;
use knotlib\services\exception\StringNotFoundException;

trait DataStoreStringTrait
{
    /**
     * Get database driver
     *
     * @param PsrContainerInterface $container
     *
     * @return string
     *
     * @throws StringNotFoundException
     * @throws StringTypeException
     */
    public function getDatabaseDriver(PsrContainerInterface $container) : string
    {
        $uri = DI::URI_STRING_DB_DRIVER;
        $string = $container->get($uri);
        if (!$string){
            throw new StringNotFoundException('DI string not found: ' . $uri);
        }
        if (!is_string($string)){
            throw new StringTypeException($uri, gettype($string));
        }
        return $string;
    }

    /**
     * Get database DSN
     *
     * @param PsrContainerInterface $container
     *
     * @return string
     *
     * @throws StringNotFoundException
     * @throws StringTypeException
     */
    public function getDatabaseDSN(PsrContainerInterface $container) : string
    {
        $uri = DI::URI_STRING_DB_DSN;
        $string = $container->get($uri);
        if (!$string){
            throw new StringNotFoundException('DI string not found: ' . $uri);
        }
        if (!is_string($string)){
            throw new StringTypeException($uri, gettype($string));
        }
        return $string;
    }
}