/** * Establish a database connection and create the connection object * * @param Setting $settings * @return PDO * @throws Exception */ private function doConnect(Setting $settings) : PDO { try { # Create a new PDO connection $connection = new PDO($settings->getSetting('dsn'), $settings->getSetting('user'), $settings->getSetting('password')); } catch (PDOException $exception) { # Stop the execution throw new Exception($exception->getMessage()); } # Set attributes $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); return $connection; }
/** * Connects to the Redis server * * @param Setting $settings Setting instance with the required attributes * @return Redis Current object to daisy chain method calls * * @throws Exception When the Redis server was not reached */ private function doConnect(Setting $settings) : self { # Connection $connection = new \Redis(); $connection->connect($settings->getSetting('address'), $settings->getSetting('port'), $settings->getSetting('timeout')); # When error occurred if ($connection->getLastError() !== null) { throw new Exception('Failed to connect to the Redis server: ' . $connection->getLastError()); } # Set options $connection->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); # Store the connection $this->connection = $connection; return $this; }