Skip to content

Commit

Permalink
Merge pull request #34 from jr-cologne/diff-envs
Browse files Browse the repository at this point in the history
Add functionality to differentiate between production and testing environment
  • Loading branch information
jr-cologne authored Jun 20, 2019
2 parents cdc0214 + 3497747 commit d5b61bc
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ vendor

# Exclude PHPStorm stuff
.idea

.env
16 changes: 11 additions & 5 deletions app/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand All @@ -29,12 +29,18 @@
use Google\Cloud\Datastore\DatastoreClient as GoogleCloudDatastore;

$env = new Env;
$env->loadEnvVarsFromDatastore(new GoogleCloudDatastore([
'projectId' => getenv('GOOGLE_CLOUD_PROJECT'),
]));

if ($env->isProduction()) {
$env->loadEnvVarsFromDatastore(new GoogleCloudDatastore([
'projectId' => getenv('GOOGLE_CLOUD_PROJECT'),
]));
} else {
$env->loadEnvVarsFromDotenvFile(__DIR__ . '/../.env');
}

$app = new CryptoStatus(
(new Config())->load(__DIR__ . '/config/config.php')
(new Config())->load(__DIR__ . '/config/config.php'),
!$env->isProduction()
);

$app->run();
2 changes: 1 addition & 1 deletion app/classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion app/classes/CryptoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
17 changes: 15 additions & 2 deletions app/classes/CryptoStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand All @@ -37,6 +37,13 @@ class CryptoStatus
*/
protected $config;

/**
* Defines whether testing mode is enabled
*
* @var bool $testing
*/
protected $testing;

/**
* The Twitter client instance
*
Expand Down Expand Up @@ -73,9 +80,10 @@ class CryptoStatus
* @throws Exceptions\TwitterClientException
* @throws Exceptions\ConfigException
*/
public function __construct(Config $config)
public function __construct(Config $config, bool $testing = false)
{
$this->config = $config;
$this->testing = $testing;

$this->init();
}
Expand Down Expand Up @@ -110,6 +118,11 @@ public function run()

$tweets = $this->createTweets();

if ($this->testing) {
echo implode(PHP_EOL . PHP_EOL, $tweets);
die();
}

if (!$this->postTweets($tweets)) {
$this->deleteTweets($this->failed_tweets);

Expand Down
2 changes: 1 addition & 1 deletion app/classes/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
58 changes: 52 additions & 6 deletions app/classes/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand All @@ -29,6 +29,16 @@
class Env
{

/**
* Check if the current environment is the production environment
*
* @return bool
*/
public function isProduction() : bool
{
return !empty(getenv('GOOGLE_CLOUD_PROJECT'));
}

/**
* Load environment variables from gcloud datastore
*
Expand All @@ -55,13 +65,28 @@ public function loadEnvVarsFromDatastore(DatastoreClient $datastore)
throw new EnvException("Failed to read environment variables from datastore");
}

foreach ($env_vars as $env_var) {
$setting = $env_var['name'] . '=' . $env_var['value'];
$this->putEnvVars($env_vars);
}

if (!self::put($setting)) {
throw new EnvException('Failed to load environment variables');
}
/**
* Load environment variables from .env file
*
* @param string $dotenv_file
* @throws EnvException
*/
public function loadEnvVarsFromDotenvFile(string $dotenv_file)
{
if (!file_exists($dotenv_file)) {
throw new EnvException("The file ({$dotenv_file}) to load the environment variables from does not exist");
}

$env_vars = file($dotenv_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

if (!$env_vars) {
throw new EnvException("Failed to read environment variables file ({$dotenv_file})");
}

$this->putEnvVars($env_vars);
}

/**
Expand Down Expand Up @@ -92,4 +117,25 @@ public function get(string $varname)

return $env;
}

/**
* Set an array of environment variables
*
* @param array $env_vars
* @throws EnvException
*/
protected function putEnvVars(array $env_vars) : void
{
foreach ($env_vars as $env_var) {
$setting = $env_var;

if (is_array($env_var)) {
$setting = $env_var['name'] . '=' . $env_var['value'];
}

if (!$this->put($setting)) {
throw new EnvException('Failed to load environment variables');
}
}
}
}
2 changes: 1 addition & 1 deletion app/classes/Exceptions/ConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion app/classes/Exceptions/CryptoClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion app/classes/Exceptions/CryptoStatusException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion app/classes/Exceptions/CurlClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion app/classes/Exceptions/EnvException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion app/classes/Exceptions/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion app/classes/Exceptions/TwitterClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion app/classes/TwitterClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down
2 changes: 1 addition & 1 deletion app/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author JR Cologne <[email protected]>
* @copyright 2019 JR Cologne
* @license https://github.com/jr-cologne/CryptoStatus/blob/master/LICENSE MIT
* @version v0.6.8-beta
* @version v0.7.0-beta
* @link https://github.com/jr-cologne/CryptoStatus GitHub Repository
*
* ________________________________________________________________________________
Expand Down

0 comments on commit d5b61bc

Please sign in to comment.