Commit 66b70ac0 by Frédéric

Des trucs

parent efb24d48
...@@ -14,14 +14,20 @@ class FluxController extends BackendBase ...@@ -14,14 +14,20 @@ class FluxController extends BackendBase
* @var Application * @var Application
*/ */
private $application; private $application;
/**
* @var array
*/
private $config;
/** /**
* FluxController constructor. * FluxController constructor.
* @param array $config
* @param Application $application * @param Application $application
*/ */
public function __construct(Application $application) public function __construct(array $config = [], Application $application)
{ {
$this->application = $application; $this->application = $application;
$this->config = $config;
} }
/** /**
...@@ -62,13 +68,14 @@ dump($id); ...@@ -62,13 +68,14 @@ dump($id);
* Synchornisation ('/sync/:id') * Synchornisation ('/sync/:id')
*/ */
public function sync($id) { public function sync($id) {
$flux = new Flux(); $flux = new Flux();
$flux->setTitle('Mon Titre'); $flux->setTitle('Mon Titre');
$flux->setContentType('test'); $flux->setContentType('test2');
$flux->setEnabled(true); $flux->setEnabled(true);
$flux->setKey('2184659e-c7b0-4e8c-8f08-0813bbda30a6'); $flux->setKey('2184659e-c7b0-4e8c-8f08-0813bbda30a6');
$sync = new Synchronisation($this->application, $flux); $sync = new Synchronisation($this->config, $this->application, $flux);
$sync->sync(); $sync->sync();
} }
......
...@@ -4,31 +4,50 @@ namespace Bolt\Extension\Appolo\Tourinsoft\Synchronisation; ...@@ -4,31 +4,50 @@ namespace Bolt\Extension\Appolo\Tourinsoft\Synchronisation;
use Bolt\Application; use Bolt\Application;
use Bolt\Extension\Appolo\Tourinsoft\Storage\Entity\Flux; use Bolt\Extension\Appolo\Tourinsoft\Storage\Entity\Flux;
use Bolt\Extension\Appolo\Tourinsoft\Utils\ContentTypeFile;
use Bolt\Extension\Appolo\Tourinsoft\Utils\Parser;
class Synchronisation class Synchronisation
{ {
/** /**
* @var Application
*/
private $application;
/**
* @var Flux * @var Flux
*/ */
private $flux; private $flux;
/**
* @var array
*/
private $config;
/**
* @var Application
*/
private $application;
/** /**
* TourinsoftParser constructor. * TourinsoftParser constructor.
* @param Application $application * @param array $config
* @param Flux $flux * @param Flux $flux
* @throws \Exception
*/ */
public function __construct(Application $application, Flux $flux) public function __construct(array $config = [], Application $application, Flux $flux)
{ {
$this->application = $application; if(empty($config['tourinsoft'])) {
throw new \Exception('La configuration n\'est pas valide.');
}
$this->flux = $flux; $this->flux = $flux;
$this->config = $config;
$this->application = $application;
} }
/**
* @throws \Exception
*/
public function sync() { public function sync() {
dump('fuck'); $options = $this->config['tourinsoft'];
$options['key'] = $this->flux->getKey();
$parser = new Parser($options);
$keys = $parser->getKeys();
$file = new ContentTypeFile($this->application, $this->flux->getContentType(), $keys);
$file->createOrUpdateFile();
} }
} }
\ No newline at end of file
...@@ -41,7 +41,7 @@ class TourinsoftExtension extends SimpleExtension ...@@ -41,7 +41,7 @@ class TourinsoftExtension extends SimpleExtension
protected function registerBackendControllers() protected function registerBackendControllers()
{ {
return [ return [
'/tourinsoft' => new FluxController($this->getContainer()), '/tourinsoft' => new FluxController($this->getConfig(), $this->getContainer()),
]; ];
} }
......
<?php
namespace Bolt\Extension\Appolo\Tourinsoft\Utils;
use Bolt\Application;
use Bolt\Filesystem\Handler\YamlFile;
use Symfony\Component\Yaml\Yaml;
class ContentTypeFile
{
/**
* @var string
*/
private $contentType;
/**
* @var array
*/
private $keys;
/**
* @var Application
*/
private $application;
/**
* ContentTypeFile constructor.
* @param Application $application
* @param string $contentType
* @param array $keys
* @throws \Exception
*/
public function __construct(Application $application, $contentType = '', array $keys = [])
{
if(empty($contentType)) {
throw new \Exception('Le content type est requis.');
}
$this->contentType = $contentType;
$this->keys = $keys;
$this->application = $application;
}
/**
* Create or update file
*/
public function createOrUpdateFile() {
$yamlData = [
$this->contentType => [
'name' => ucfirst($this->contentType),
'singular_name' => ucfirst($this->contentType),
'fields' => $this->getFields()
]
];
/** @var YamlFile $file */
$file = $this->application['filesystem']->getFile(strtolower("extensions_config://contenttypes/$this->contentType.yml"));
$file->dump($yamlData);
}
/**
* @return array
*/
private function getFields()
{
$fields = [];
foreach ($this->keys as $key) {
$fields[$key] = ['type' => 'text'];
}
$fields['slug'] = [
'type' => 'slug',
'uses' => 'SyndicObjectID',
'group' => 'meta'
];
return $fields;
}
}
\ No newline at end of file
<?php
namespace Bolt\Extension\Appolo\Tourinsoft\Utils;
class File
{
}
\ No newline at end of file
...@@ -2,7 +2,70 @@ ...@@ -2,7 +2,70 @@
namespace Bolt\Extension\Appolo\Tourinsoft\Utils; namespace Bolt\Extension\Appolo\Tourinsoft\Utils;
use GuzzleHttp\Client;
class Parser class Parser
{ {
/**
* @var array
*/
private $options;
/**
* @var array
*/
private $arrayStream = [];
/**
* @var string
*/
private $url;
/**
* Parser constructor.
* @param array $options
* @throws \Exception
*/
public function __construct(array $options = [])
{
if(empty($options['url']) || empty($options['ot_key']) || empty($options['key'])) {
throw new \Exception('La configuration n\'est pas valide.');
}
$this->options = $options;
$this->url = $options['url'].$options['ot_key'].'/'.$options['key'].'/Objects?$format=JSON';
$this->getStreamAsArray();
}
/**
* @return array
*/
public function getKeys() {
$keys = [];
foreach ($this->arrayStream['value'] as $item) {
foreach (array_keys($item) as $key) {
if(!in_array($key, $keys)) {
array_push($keys, $key);
}
}
}
return $keys;
}
/**
* @throws \Exception
*/
protected function getStreamAsArray() {
$client = new Client();
$res = $client->request('GET', $this->url);
if($res->getStatusCode() !== 200) {
throw new \Exception('Le flux n\'a pu être récupéré.');
}
try {
$this->arrayStream = json_decode($res->getBody(), true);
} catch (\Exception $e) {
throw new \Exception('Le flux n\'a pu être décodé.');
}
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment