-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7bc045
commit bd9bad0
Showing
2 changed files
with
107 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of GitHub Profile Views Counter. | ||
* | ||
* (c) Anton Komarev <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Komarev\GitHubProfileViewsCounter; | ||
|
||
use Contracts\Komarev\GitHubProfileViewsCounter\CounterRepositoryInterface; | ||
use Dotenv\Dotenv; | ||
use PDO; | ||
|
||
final class CounterRepositoryFactory | ||
{ | ||
public function create( | ||
string $appBasePath | ||
): CounterRepositoryInterface { | ||
$dotEnv = Dotenv::createImmutable($appBasePath); | ||
$env = $dotEnv->load(); | ||
|
||
$dotEnv->required([ | ||
'REPOSITORY', | ||
]); | ||
|
||
$repositoryType = $env['REPOSITORY']; | ||
|
||
switch ($repositoryType) { | ||
case 'pdo': | ||
$dotEnv->required([ | ||
'DB_DRIVER', | ||
'DB_HOST', | ||
'DB_PORT', | ||
'DB_USER', | ||
'DB_PASSWORD', | ||
'DB_NAME', | ||
]); | ||
|
||
$dsn = sprintf( | ||
'%s:host=%s;port=%d;dbname=%s', | ||
$env['DB_DRIVER'], | ||
$env['DB_HOST'], | ||
$env['DB_PORT'], | ||
$env['DB_NAME'], | ||
); | ||
$dbConnection = new PDO( | ||
$dsn, | ||
$env['DB_USER'], | ||
$env['DB_PASSWORD'], | ||
[ | ||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | ||
], | ||
); | ||
|
||
return new CounterPdoRepository($dbConnection); | ||
case 'file': | ||
$dotEnv->required([ | ||
'FILE_STORAGE_PATH', | ||
]); | ||
|
||
$storagePath = $env['FILE_STORAGE_PATH'] !== '' | ||
? $env['FILE_STORAGE_PATH'] | ||
: $appBasePath . '/storage'; | ||
|
||
return new CounterFileRepository($storagePath); | ||
default: | ||
throw new \Exception( | ||
"Unsupported repository `$repositoryType`", | ||
); | ||
} | ||
} | ||
} |