Skip to content

Commit

Permalink
Merge pull request #88 from Pirate-Parties-International/dev
Browse files Browse the repository at this point in the history
[] minor cleanup
  • Loading branch information
j-h-s authored Feb 20, 2018
2 parents ebcb7de + dea6c29 commit eba8759
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions src/AppBundle/Services/PopulationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,42 @@ class PopulationService
protected $em;
protected $log;
protected $connect;
protected $filePath;

public function __construct(Container $container) {
$this->container = $container;
$this->em = $this->container->get('doctrine')->getManager();
$this->log = $this->container->get('logger');
$this->connect = $this->container->get('ConnectionService');
$this->filePath = $this->getFilePath();
@set_exception_handler([$this->connect, 'exception_handler']);
}


/**
* Returns path to local json file where population data is stored
* @return string
*/
public function getFilePath() {
$appRoot = $this->container->get('kernel')->getRootDir() . '/..';
$filePath = $appRoot . '/etc/population.json';

return $filePath;
}


/**
* Returns population data for a party, if available
* @param string $partyCode
* @return int
*/
public function getPopulation($partyCode) {
$filePath = $this->getFilePath();

if (!file_exists($filePath)) {
if (!file_exists($this->filePath)) {
$this->log->warning("Population data missing");
return null;
}

$json = file_get_contents($filePath);
$json = file_get_contents($this->filePath);
$array = json_decode($json, true);

if (!isset($array[$partyCode])) {
Expand Down Expand Up @@ -71,15 +83,14 @@ public function getPopulationData() {
}

$data = $arr['total_population']['population'];
$this->log->debug(" + Population for " . $party->getCode() . " = " . $data);
$this->log->debug(" + " . $party->getCode() . ": " . $data);

$temp[$party->getCode()] = $data;
}

$this->log->info(" + Saving new data to file");
$out = json_encode($temp);
$filePath = $this->getFilePath();
file_put_contents($filePath, $out);
$out = json_encode($temp, JSON_PRETTY_PRINT);
file_put_contents($this->filePath, $out);
$this->log->notice("# Done");
}

Expand Down Expand Up @@ -126,14 +137,12 @@ public function getCountryName($name) {
* @return bool
*/
public function checkLocalData() {
$filePath = $this->getFilePath();

if (!file_exists($filePath)) {
if (!file_exists($this->filePath)) {
$this->log->notice("- Population data does not exist");
return false;
}

$json = file_get_contents($filePath);
$json = file_get_contents($this->filePath);
$array = json_decode($json, true);

$timeLimit = strtotime('-1 month');
Expand All @@ -152,19 +161,7 @@ public function checkLocalData() {
}

$this->log->notice("+ Population data is up-to-date");
return $filePath;
}


/**
* Returns path to local json file where population data is stored
* @return string
*/
public function getFilePath() {
$appRoot = $this->container->get('kernel')->getRootDir() . '/..';
$filePath = $appRoot . '/etc/population.json';

return $filePath;
return true;
}

}

0 comments on commit eba8759

Please sign in to comment.