Skip to content

Commit 10496b2

Browse files
authored
Moved AbstractHTTPProvider to separate package (#685)
* Moved AbstractHTTPProvider to separate package * cs * Bugfix
0 parents  commit 10496b2

File tree

7 files changed

+269
-0
lines changed

7 files changed

+269
-0
lines changed

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
sudo: false
3+
4+
php: 7.0
5+
6+
install:
7+
- composer update --prefer-stable --prefer-dist
8+
9+
script:
10+
- composer test
11+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2011 — William Durand <william.durand1@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Provider/AbstractHttpProvider.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Http\Provider;
14+
15+
use Geocoder\Exception\InvalidCredentials;
16+
use Geocoder\Exception\InvalidServerResponse;
17+
use Geocoder\Exception\QuotaExceeded;
18+
use Geocoder\Provider\AbstractProvider;
19+
use Http\Message\MessageFactory;
20+
use Http\Discovery\MessageFactoryDiscovery;
21+
use Http\Client\HttpClient;
22+
23+
/**
24+
* @author William Durand <william.durand1@gmail.com>
25+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
26+
*/
27+
abstract class AbstractHttpProvider extends AbstractProvider
28+
{
29+
/**
30+
* @var HttpClient
31+
*/
32+
private $client;
33+
34+
/**
35+
* @var MessageFactory
36+
*/
37+
private $messageFactory;
38+
39+
/**
40+
* @param HttpClient $client
41+
* @param MessageFactory|null $factory
42+
*/
43+
public function __construct(HttpClient $client, MessageFactory $factory = null)
44+
{
45+
$this->client = $client;
46+
$this->messageFactory = $factory;
47+
}
48+
49+
/**
50+
* Get URL and return contents. If content is empty, an exception will be thrown.
51+
*
52+
* @param string $url
53+
*
54+
* @return string
55+
*
56+
* @throws InvalidServerResponse
57+
*/
58+
protected function getUrlContents($url): string
59+
{
60+
$request = $this->getMessageFactory()->createRequest('GET', $url);
61+
$response = $this->getHttpClient()->sendRequest($request);
62+
63+
$statusCode = $response->getStatusCode();
64+
if (401 === $statusCode || 403 === $statusCode) {
65+
throw new InvalidCredentials();
66+
} elseif (429 === $statusCode) {
67+
throw new QuotaExceeded();
68+
} elseif ($statusCode >= 300) {
69+
throw InvalidServerResponse::create($url, $statusCode);
70+
}
71+
72+
$body = (string) $response->getBody();
73+
if (empty($body)) {
74+
throw InvalidServerResponse::emptyResponse($url);
75+
}
76+
77+
return $body;
78+
}
79+
80+
/**
81+
* Returns the HTTP adapter.
82+
*
83+
* @return HttpClient
84+
*/
85+
protected function getHttpClient(): HttpClient
86+
{
87+
return $this->client;
88+
}
89+
90+
/**
91+
* @return MessageFactory
92+
*/
93+
protected function getMessageFactory(): MessageFactory
94+
{
95+
if ($this->messageFactory === null) {
96+
$this->messageFactory = MessageFactoryDiscovery::find();
97+
}
98+
99+
return $this->messageFactory;
100+
}
101+
102+
/**
103+
* @param HttpClient $client
104+
*/
105+
public function setClient(HttpClient $client)
106+
{
107+
$this->client = $client;
108+
}
109+
110+
/**
111+
* @param MessageFactory $messageFactory
112+
*/
113+
public function setMessageFactory(MessageFactory $messageFactory)
114+
{
115+
$this->messageFactory = $messageFactory;
116+
}
117+
}

Readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Common classes for the Geocoder
2+
[![Build Status](https://travis-ci.org/geocoder-php/php-common.svg?branch=master)](http://travis-ci.org/geocoder-php/php-common)
3+
[![Latest Stable Version](https://poser.pugx.org/willdurand/geocoder/v/stable)](https://packagist.org/packages/willdurand/geocoder)
4+
[![Total Downloads](https://poser.pugx.org/willdurand/geocoder/downloads)](https://packagist.org/packages/willdurand/geocoder)
5+
[![Monthly Downloads](https://poser.pugx.org/willdurand/geocoder/d/monthly.png)](https://packagist.org/packages/willdurand/geocoder)
6+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
7+
8+
9+
### Contribute
10+
11+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
12+
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Http\Provider\Tests;
12+
13+
use Geocoder\Http\Provider\AbstractHttpProvider;
14+
use Http\Client\HttpClient;
15+
use Http\Mock\Client;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class AbstractHttpProviderTest extends TestCase
19+
{
20+
public function testHttpClientGetter()
21+
{
22+
$client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock();
23+
$provider = new DummyProvider($client);
24+
$this->assertSame($client, $provider->getHttpClient());
25+
}
26+
}
27+
28+
class DummyProvider extends AbstractHttpProvider
29+
{
30+
public function getHttpClient(): HttpClient
31+
{
32+
return parent::getHttpClient();
33+
}
34+
}

composer.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "geocoder-php/common-http",
3+
"type": "library",
4+
"description": "Common files for HTTP based Geocoders",
5+
"keywords": ["http geocoder"],
6+
"homepage": "http://geocoder-php.org",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Tobias Nyholm",
11+
"email": "tobias.nyholm@gmail.com"
12+
}
13+
],
14+
"require": {
15+
"php": "^7.0",
16+
"willdurand/geocoder": "^4.0",
17+
"php-http/httplug": "^1.0",
18+
"psr/http-message": "^1.0",
19+
"php-http/message-factory": "^1.0.2",
20+
"psr/http-message-implementation": "^1.0",
21+
"php-http/client-implementation": "^1.0",
22+
"php-http/discovery": "^1.0"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^6.1",
26+
"symfony/stopwatch": "~2.5",
27+
"php-http/message": "^1.0",
28+
"php-http/mock-client": "^1.0",
29+
"nyholm/psr7": "^0.2.2"
30+
},
31+
"autoload": {
32+
"psr-4": { "Geocoder\\Http\\": "" },
33+
"exclude-from-classmap": [
34+
"/Tests/"
35+
]
36+
},
37+
"scripts": {
38+
"test": "vendor/bin/phpunit"
39+
},
40+
"minimum-stability": "dev",
41+
"extra": {
42+
"branch-alias": {
43+
"dev-master": "4.0-dev"
44+
}
45+
}
46+
}

phpunit.xml.dist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
<php>
10+
<ini name="error_reporting" value="-1" />
11+
</php>
12+
13+
<testsuites>
14+
<testsuite name="Geocoder Test Suite">
15+
<directory>./Tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory>./</directory>
22+
<exclude>
23+
<directory>./Tests</directory>
24+
<directory>./vendor</directory>
25+
</exclude>
26+
</whitelist>
27+
</filter>
28+
</phpunit>

0 commit comments

Comments
 (0)