From c53416805839804c23d6be1a96ba414a1aef1746 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 24 Mar 2015 02:41:41 +0300 Subject: [PATCH] Test implementation of Point API services. Login field added to the User entity. --- app/AppKernel.php | 1 + composer.json | 3 +- composer.lock | 169 +++++++++++++++++- .../Bundle/PointToolsBundle/Entity/User.php | 43 +++++ .../Resources/config/services.yml | 12 +- .../PointToolsBundle/Service/AbstractApi.php | 37 ++++ .../PointToolsBundle/Service/UserApi.php | 56 ++++++ 7 files changed, 316 insertions(+), 5 deletions(-) create mode 100644 src/Skobkin/Bundle/PointToolsBundle/Service/AbstractApi.php create mode 100644 src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php diff --git a/app/AppKernel.php b/app/AppKernel.php index e364978..2b243fd 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -16,6 +16,7 @@ class AppKernel extends Kernel new Symfony\Bundle\AsseticBundle\AsseticBundle(), new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), + new Misd\GuzzleBundle\MisdGuzzleBundle(), new Skobkin\Bundle\PointToolsBundle\SkobkinPointToolsBundle(), ); diff --git a/composer.json b/composer.json index 5d5968b..eab93ca 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "symfony/monolog-bundle": "~2.4", "sensio/distribution-bundle": "~3.0,>=3.0.12", "sensio/framework-extra-bundle": "~3.0,>=3.0.2", - "incenteev/composer-parameter-handler": "~2.0" + "incenteev/composer-parameter-handler": "~2.0", + "misd/guzzle-bundle": "~1.0" }, "require-dev": { "sensio/generator-bundle": "~2.3" diff --git a/composer.lock b/composer.lock index a6013e9..69ef40e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "73f15ad91f600008506b4294b7b7d01c", + "hash": "9b2a01fca6691fa7cf3a44dba320f9a9", "packages": [ { "name": "doctrine/annotations", @@ -708,6 +708,101 @@ ], "time": "2014-12-16 13:45:01" }, + { + "name": "guzzle/guzzle", + "version": "v3.9.3", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle3.git", + "reference": "0645b70d953bc1c067bbc8d5bc53194706b628d9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle3/zipball/0645b70d953bc1c067bbc8d5bc53194706b628d9", + "reference": "0645b70d953bc1c067bbc8d5bc53194706b628d9", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "php": ">=5.3.3", + "symfony/event-dispatcher": "~2.1" + }, + "replace": { + "guzzle/batch": "self.version", + "guzzle/cache": "self.version", + "guzzle/common": "self.version", + "guzzle/http": "self.version", + "guzzle/inflection": "self.version", + "guzzle/iterator": "self.version", + "guzzle/log": "self.version", + "guzzle/parser": "self.version", + "guzzle/plugin": "self.version", + "guzzle/plugin-async": "self.version", + "guzzle/plugin-backoff": "self.version", + "guzzle/plugin-cache": "self.version", + "guzzle/plugin-cookie": "self.version", + "guzzle/plugin-curlauth": "self.version", + "guzzle/plugin-error-response": "self.version", + "guzzle/plugin-history": "self.version", + "guzzle/plugin-log": "self.version", + "guzzle/plugin-md5": "self.version", + "guzzle/plugin-mock": "self.version", + "guzzle/plugin-oauth": "self.version", + "guzzle/service": "self.version", + "guzzle/stream": "self.version" + }, + "require-dev": { + "doctrine/cache": "~1.3", + "monolog/monolog": "~1.0", + "phpunit/phpunit": "3.7.*", + "psr/log": "~1.0", + "symfony/class-loader": "~2.1", + "zendframework/zend-cache": "2.*,<2.3", + "zendframework/zend-log": "2.*,<2.3" + }, + "suggest": { + "guzzlehttp/guzzle": "Guzzle 5 has moved to a new package name. The package you have installed, Guzzle 3, is deprecated." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.9-dev" + } + }, + "autoload": { + "psr-0": { + "Guzzle": "src/", + "Guzzle\\Tests": "tests/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Guzzle Community", + "homepage": "https://github.com/guzzle/guzzle/contributors" + } + ], + "description": "PHP HTTP client. This library is deprecated in favor of https://packagist.org/packages/guzzlehttp/guzzle", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2015-03-18 18:23:50" + }, { "name": "incenteev/composer-parameter-handler", "version": "v2.1.0", @@ -884,6 +979,78 @@ ], "time": "2014-12-12 05:04:05" }, + { + "name": "misd/guzzle-bundle", + "version": "v1.1.5", + "source": { + "type": "git", + "url": "https://github.com/misd-service-development/guzzle-bundle.git", + "reference": "555c105ef4ac66597a029fe634dee0fe4f9cb084" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/misd-service-development/guzzle-bundle/zipball/555c105ef4ac66597a029fe634dee0fe4f9cb084", + "reference": "555c105ef4ac66597a029fe634dee0fe4f9cb084", + "shasum": "" + }, + "require": { + "guzzle/guzzle": "~3.0", + "php": ">=5.3.3", + "symfony/framework-bundle": "~2.2" + }, + "conflict": { + "jms/serializer-bundle": "<0.11-dev", + "sensio/framework-extra-bundle": ">=4.0-dev" + }, + "require-dev": { + "doctrine/cache": "~1.0", + "jms/serializer-bundle": "~0.11", + "phpunit/phpunit": "~4.3", + "sensio/framework-extra-bundle": "~2.2", + "symfony/monolog-bundle": "~2.2", + "symfony/yaml": "~2.2" + }, + "suggest": { + "jms/serializer-bundle": "Serialize/deserialize objects to/from XML, JSON and YAML", + "sensio/framework-extra-bundle": "Provides a parameter converter", + "symfony/monolog-bundle": "Log requests" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Misd\\GuzzleBundle\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Chris Wilkinson", + "email": "chris.wilkinson@admin.cam.ac.uk" + } + ], + "description": "Integrates Guzzle into your Symfony2 application", + "homepage": "https://github.com/misd-service-development/guzzle-bundle", + "keywords": [ + "Guzzle", + "api", + "bundle", + "client", + "curl", + "http", + "http client", + "rest", + "web service" + ], + "time": "2014-12-01 08:29:51" + }, { "name": "monolog/monolog", "version": "1.12.0", diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php index 6f9ffb8..df47217 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php @@ -24,6 +24,13 @@ class User */ private $id; + /** + * @var string + * + * @ORM\Column(name="login", type="string", length=255) + */ + private $login; + /** * @var string * @@ -76,6 +83,42 @@ class User { return $this->id; } + + /** + * Set id of user (for API services only) + * + * @param integer $id + * @return User + */ + public function setId($id) + { + $this->id = $id; + + return $this; + } + + /** + * Set login + * + * @param string $login + * @return User + */ + public function setLogin($login) + { + $this->login = $login; + + return $this; + } + + /** + * Get login + * + * @return string + */ + public function getLogin() + { + return $this->login; + } /** * Set name diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml index d417886..8f9cc25 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml @@ -1,4 +1,10 @@ services: -# skobkin_point_tools.example: -# class: Skobkin\Bundle\PointToolsBundle\Example -# arguments: [@service_id, "plain_value", %parameter%] + skobkin_point_tools.http_client: + class: %guzzle.client.class% + arguments: [ "http://point.im/" ] + tags: + - { name: guzzle.client } + + skobkin_point_tools.api_user: + class: Skobkin\Bundle\PointToolsBundle\UserApi + arguments: [ @skobkin_point_tools.http_client ] \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/AbstractApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/AbstractApi.php new file mode 100644 index 0000000..a92e243 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/AbstractApi.php @@ -0,0 +1,37 @@ +client = $httpClient; + } + + /** + * Make GET request and return Response object + * + * @param string $pathTemplate + * @param array $parameters + * @return GuzzleResponse + */ + public function sendGetRequest($pathTemplate, array $parameters = []) + { + $path = vsprintf($pathTemplate, $parameters); + + $request = $this->client->get($path); + + return $request->send(); + } +} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php new file mode 100644 index 0000000..d407a16 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php @@ -0,0 +1,56 @@ +sendGetRequest(self::PATH_USER_SUBSCRIBERS, [$login]); + + $body = $response->getBody(true); + + // @todo use JMSSerializer + $data = json_decode($body); + + $users = []; + + if (is_array($data)) { + foreach ($data as $apiUser) { + $user = new User(); + $user->setId($apiUser->id); + $user->setLogin($apiUser->login); + $user->setName($apiUser->name); + + $users[] = $user; + } + } + + return $users; + } +}