diff --git a/app/config.yml b/app/config.yml new file mode 100644 index 0000000..21229e7 --- /dev/null +++ b/app/config.yml @@ -0,0 +1,3 @@ +auth: + uid: + token: diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..98b17a5 --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "autoload": { + "psr-0": { + "": "src/" + } + }, + "require": { + "symfony/console": "2.*", + "psr/log": "1.0.0", + "symfony/finder": "~2.6", + "symfony/yaml": "~2.6", + "symfony/filesystem": "3.0.x-dev", + "guzzlehttp/guzzle": "~5.2" + } +} diff --git a/console b/console new file mode 100755 index 0000000..aab334c --- /dev/null +++ b/console @@ -0,0 +1,9 @@ +#!/usr/bin/env php +run(); diff --git a/src/Api/.ConfigLoader.php.swp b/src/Api/.ConfigLoader.php.swp new file mode 100644 index 0000000..ff79c38 Binary files /dev/null and b/src/Api/.ConfigLoader.php.swp differ diff --git a/src/Api/Client.php b/src/Api/Client.php new file mode 100644 index 0000000..ce29504 --- /dev/null +++ b/src/Api/Client.php @@ -0,0 +1,51 @@ +client = new GuzzleClient(); + } + + public function getAuthorization($username, $password) + { + return new ClientResponse($this->get( + 'auth/', + array( + 'username' => $username, + 'password' => $password, + ) + ); + } + + public function get($uri, array $options = array()) + { + try { + return new ClientResponse($this->client->get(API_HOST.$uri, $options)); + } catch (RequestException $e) { + throw new ApiClientException(sprintf('Request exception (GET): %s', $e->getMessage())); + } catch (HttpConnectException $e) { + throw new ApiClientException(sprintf('HTTP Connection exception: %s', $e->getMessage())); + } + } + + public function post($uri, array $options = array()) + { + try { + return new ClientResponse($this->client->post(API_HOST.$uri, $options)); + } catch (RequestException $e) { + throw new ApiClientException(sprintf('Request exception (POST): %s', $e->getMessage())); + } catch (HttpConnectException $e) { + throw new ApiClientException(sprintf('HTTP Connection exception: %s', $e->getMessage())); + } + } +} diff --git a/src/Api/ClientException.php b/src/Api/ClientException.php new file mode 100644 index 0000000..c9c5637 --- /dev/null +++ b/src/Api/ClientException.php @@ -0,0 +1,7 @@ +response = $response; + } + + public function hasError() + { + return isset($this->response->json()['error']); + } + + public function getErrorCode() + { + if ($this->hasError()) { + return null; + } + + return $this->response->json()['code']; + } + + public function getErrorMessage() + { + if ($this->hasError()) { + return null; + } + + return $this->response->json()['error']; + } + + public function getData() + { + return $this->response->json(); + } +} diff --git a/src/Api/ConfigLoader.php b/src/Api/ConfigLoader.php new file mode 100644 index 0000000..2dbca74 --- /dev/null +++ b/src/Api/ConfigLoader.php @@ -0,0 +1,22 @@ +filesystem = new Filesystem(); + } + + public function configExists() + { + return $this->filesystem->exists($this->configFile); + } +} diff --git a/src/Console/Application.php b/src/Console/Application.php new file mode 100644 index 0000000..a14f715 --- /dev/null +++ b/src/Console/Application.php @@ -0,0 +1,44 @@ + 'Console\\Command', + ); + + public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') + { + parent::__construct($name, $version); + + $this->loadCommands(); + } + + public function addCommandsPath($path, $namespace) + { + $this->commandsPaths[$path] = trim($namespace, '/'); + + return $this; + } + + protected function loadCommands() + { + $finder = new Finder(); + + foreach ($this->commandsPaths as $path => $namespace) { + $finder->files('*Command.php')->in($path); + + foreach ($finder as $file) { + $className = $namespace.'\\'.str_replace('.php', '', $file->getFilename()); + $this->addCommands(array( + new $className(), + )); + } + } + } + +} diff --git a/src/Console/Command/.AuthCommand.php.swp b/src/Console/Command/.AuthCommand.php.swp new file mode 100644 index 0000000..7fd81e8 Binary files /dev/null and b/src/Console/Command/.AuthCommand.php.swp differ diff --git a/src/Console/Command/AuthCommand.php b/src/Console/Command/AuthCommand.php new file mode 100644 index 0000000..b8d8990 --- /dev/null +++ b/src/Console/Command/AuthCommand.php @@ -0,0 +1,41 @@ +setName('auth:login') + ->setDescription('Login on t411') + ->setHelp("The %command.name% "); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $configLoader = new ConfigLoader(); + + if ($configLoader->configExists()) { + $continue = $dialog->askConfirmation( + $output, + $dialog->getQuestion( + 'The configuration file already exists. Do you want to continue', + 'yes', + '?' + ), + true + ); + + if (!$continue) { + $output->writeln('Aborded.'); + + return; + } + } + } +}