Categories
Postgres

How to PHP Singleton with different schema Postgres Database connection PDO ?

/**
 * Class StorageService
 *
 * @package App\Core\Services
 */
class StorageService extends \App\Core\Classes\Service
{

    private static $instance = null;
    private $storageConfigs = false;
    private $dbConnections = false;

    public $schema ;
    public $database_host ;
    public $database_port ;
    public $database_name ;
    public $database_user ;
    public $newConnection ;


    public function __construct($schema = 'api')
    {

        $dbConfigs = $this->storageConfigs()['databases'];
        
        $dbConfig = $dbConfigs[$schema];

        $this->schema = $schema;
        $this->database_host = $dbConfig['database_host'];
        $this->database_port = $dbConfig['database_port'];
        $this->database_name = $dbConfig['database_name'];
        $this->database_user = $dbConfig['database_user'];
        $database_password  = $dbConfig['database_password'];
        

        $dsn = "pgsql:host={$this->database_host};port={$this->database_port};dbname={$this->database_name};";
        $pdoOpts =
            [
                \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
                \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
            ];

        try {
            $this->newConnection = new \PDO($dsn, $this->database_user, $database_password, $pdoOpts);

        } catch (\Throwable $e) {

            /**
             * Add Exception error
             *
             * @date July 15th 2021
             * @author David Raleche
             */
            $error = 'MAPI CANT CONNECT DATABASE schema'.$schema
                .' DSN: '.$dsn
                .' USER: '.$this->database_user
                .' PASSWORD'.$database_password
                .' '.(string) $pdoOpts
                . '  ' .$e->getMessage();
            HelperService::logger('critical', $error);
            throw new \Exception('Database connection issue '.$e->getMessage(), 500);
        }



    }


    /**
     * Storage Configs
     *
     * @return bool|mixed
     */
    private function storageConfigs()
    {
        if ($this->storageConfigs === false) {
            $this->storageConfigs = yaml_parse_file(__DIR__ . '/../../config/storage.yaml');
        }
        return $this->storageConfigs;
    }

    /**
     * Get Db connection
     *
     * @param string $schema
     * @param bool $allowShared
     *
     * @return bool|\PDO
     */
    
    public function getConnection() : \PDO
    {
        return $this->newConnection;
    }


    public static function getInstance($schema)
    {
        if(!self::$instance )
        {
            self::$instance = new StorageService($schema);
        }

        if ( self::$instance->schema !== $schema)
        {
            self::$instance = new StorageService($schema);
        }

        return self::$instance;
    }
Categories
Technical

Load Testing / Performance Testing

Tools required in a LAMP stack environment for load testing are Jmeter, Blazemeter

Categories
Technical

How to find a string in the php.ini or phpinfo() ?

SSH into the machine and simply type the command below :

php -i | grep “your_string”

Categories
PHPUnit

Phpunit stops unexpectedly without any message or error

My solution for that issue is to first identify which phpUnit test is failing this way

Then set a try-catch methodology in your code to analyze the error

From experience it is likely due to php or phpUnit running out of memory probably cause by a recursive pattern in the code

Encapsulate the code with a try catch see below

try {

      [Code to encapsulate]

} catch (\Throwable $e) {

var_dump($e->getCode());
var_dump($e->getMessage());

}

An \Exception is not likely to be caught in this type of error

Categories
Laravel Valet

How to fix valet ? Laravel Valet – This Site Can’t Be Reached

How to fix ?

NOTICE: [pool valet] ‘user’ directive is ignored when FPM is not running as root

or

ERROR: FPM initialization failed

or

Laravel Valet – This Site Can’t Be Reached

or

valet nginx 137 upstream timed out (60: Operation timed out) while reading response header from upstream, client: 127.0.0.1,

Solution

valet uninstall
rm -rf ~/.valet
rm -rf ~/.config/valet
valet install
valet link
valet open
Categories
Technical

MACOS – How to reset PATH ?

Default PATH David Raleche

 PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/:~/.composer/vendor/bin

Basic and Essential PATH

PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

Categories
Technical

Google-Fi ! $35/month for 2 people !

Click here to have more info about the google-Fi https://g.co/fi/r/UE5V06

Categories
Technical

HTOP – new Linux Command

HTOP

This a new Linux command that allow to monitor different processes

htop is an interactive system-monitor process-viewer and process-manager. It is designed as an alternative to the Unix program top. It shows a frequently updated list of the processes running on a computer, normally ordered by the amount of CPU usage.

Categories
Apache

How to import Jmeter test to Blazemeter ?

To import Jmeter collection test to Blazemeter

  1. Go to Jmeter
  2. Go to File => Save test Plan As
  1. Import the file to Blazemeter (Upload script)
Categories
Apache

How to see logstash status ?

Find below how to check the status of the logstash service

service logstash status

What is logstash ?

Logstash is a free and open server-side data processing pipeline that ingests data from a multitude of sources, transforms it, and then sends it to your favorite “stash.”