Skip to content

Commit

Permalink
LoadConfig task
Browse files Browse the repository at this point in the history
  • Loading branch information
jguittard committed Mar 26, 2016
1 parent 4123e21 commit 1fbb9a1
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 19 deletions.
99 changes: 99 additions & 0 deletions src/Task/LoadConfigTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace JG\Task;

use \Task;

/**
* Class LoadConfigTask
*
* @package JG\Task
* @version 1.0
* @author Julien Guittard <julien.guittard@me.com>
*/
class LoadConfigTask extends Task
{
/**
* @var string
*/
protected $fromFile;

/**
* @var string
*/
protected $toProperty;

/**
* @var int
*/
protected $wait;

/**
* Set the fromFile
*
* @param string $fromFile
* @return LoadConfigTask
*/
public function setFromFile($fromFile)
{
$this->fromFile = $fromFile;
return $this;
}

/**
* Set the toProperty
*
* @param string $toProperty
* @return LoadConfigTask
*/
public function setToProperty($toProperty)
{
$this->toProperty = $toProperty;
return $this;
}

/**
* Set the wait
*
* @param int $wait
* @return LoadConfigTask
*/
public function setWait($wait)
{
$this->wait = (int)$wait;
return $this;
}

/**
* Task call
*
* @throws \Exception
* @return void
*/
public function main()
{
if (empty($this->fromFile)) {
throw new \Exception('File path must be set to load config');
}
$wait = $this->wait ?: 5;
for ($i=0; $i<10; $i++) {
if (!file_exists($this->fromFile)) {
sleep($wait);
continue;
}
break;
}
if (!file_exists($this->fromFile)) {
throw new \Exception('File path must exist to load config');
}
$content = file_get_contents($this->fromFile);
$config = json_decode($content, true);
if (!$config) {
throw new \Exception('File content must be valid JSON');
}
foreach ($config as $key => $value) {
$this->project->setNewProperty($this->toProperty .$key, $value);
}
}

}
26 changes: 9 additions & 17 deletions src/Task/PoeditorTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace JG\Task;

use Guzzle\Http\Client;
use GuzzleHttp\Client;
use \Task;

/**
* Class PoeditorTask
*
* @package JG\Task
* @version 1.0
* @author Julien Guittard <julien.guittard@mme.com>
* @author Julien Guittard <julien.guittard@me.com>
*/
class PoeditorTask extends Task
{
Expand Down Expand Up @@ -97,11 +97,6 @@ class PoeditorTask extends Task
'es' => 'es_ES',
];

/**
* @var Client
*/
protected $apiClient;

/**
* Get the type
*
Expand Down Expand Up @@ -274,18 +269,15 @@ public function setExportPath($exportPath)
public function main()
{
$client = new Client();
$request = $client->post(self::API_ENDPOINT, null, $this->getPostParams());
$response = $request->send();
if ($response->getStatusCode() != 200) {
throw new \BuildException('Unable to retrieve translation file');
}
$json = $response->json();
$response = $client->request('POST', self::API_ENDPOINT, [
'form_params' => $this->getPostParams()
]);
$json = json_decode($response->getBody(), true);
$file = $json['item'];

$item = $json['item'];
echo '[Module ' . ucfirst($this->moduleName) . ']' . 'Downloading ' . strtoupper($this->getLanguage()) . ' file from ' . $file;

$handle = fopen($this->retrieveFileName(), 'w');
$client->get($item, null, [Client::CURL_OPTIONS => ['CURLOPT_FILE' => $handle]])->send();
fclose($handle);
$client->request('GET', $file, ['sink' => $this->retrieveFileName()]);
}

protected function retrieveFileName()
Expand Down
2 changes: 1 addition & 1 deletion src/Task/SassTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @package JG\Task
* @version 1.0
* @author Julien Guittard <julien.guittard@mme.com>
* @author Julien Guittard <julien.guittard@me.com>
*/
class SassTask extends Task
{
Expand Down
62 changes: 62 additions & 0 deletions tests/LoadConfigTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace JGTests\Task;

use JG\Task\LoadConfigTask;

/**
* Class LoadConfigTaskTest
*
* @package JGTest\Task
* @version 1.0
* @author Julien Guittard <julien.guittard@me.com>
*/
class LoadConfigTaskTest extends \PHPUnit_Framework_TestCase
{
/**
* @var LoadConfigTask
*/
protected $task;
/**
* Setup
*
* @return void
*/
protected function setUp()
{
$this->task = new LoadConfigTask();
}
/**
* Test the fromFile Setter
*
* @return void
*/
public function testFromFileSetter()
{
$fromFile = '/usr/null';
$this->assertSame($this->task, $this->task->setFromFile($fromFile));
$this->assertAttributeSame($fromFile, 'fromFile', $this->task);
}
/**
* Test the toProperty Setter
*
* @return void
*/
public function testToPropertySetter()
{
$toProperty = 'myConfig';
$this->assertSame($this->task, $this->task->setToProperty($toProperty));
$this->assertAttributeSame($toProperty, 'toProperty', $this->task);
}
/**
* Test the wait Setter
*
* @return void
*/
public function waitPropertySetter()
{
$wait = 5;
$this->assertSame($this->task, $this->task->setWait($wait));
$this->assertAttributeSame($wait, 'wait', $this->task);
}
}
2 changes: 1 addition & 1 deletion tests/SaasTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @package JGTest\Task
* @version 1.0
* @author Julien Guittard <julien.guittard@mme.com>
* @author Julien Guittard <julien.guittard@me.com>
*/
class SaasTaskTest extends \PHPUnit_Framework_TestCase
{
Expand Down

0 comments on commit 1fbb9a1

Please sign in to comment.