Skip to content

Commit

Permalink
Break out Http request code + fixup phpunit + composer autload
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Littlemore committed Jul 13, 2017
1 parent 969a091 commit f4f4e3b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
9 changes: 7 additions & 2 deletions composer.json
Expand Up @@ -25,5 +25,10 @@
"psr-4": {
"MarcL\\": "src/"
}
}
}
},
"autoload-dev": {
"psr-4": {
"MarcL\\": "src/",
"tests\\": "tests/"
}
}}
4 changes: 2 additions & 2 deletions examples.php
@@ -1,7 +1,7 @@
<?php

// Include the AmazonAPI code
include_once('./src/AmazonAPI.php');
require('vendor/autoload.php');

use MarcL\AmazonAPI;

// Should load these from environment variables
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
<testsuite name="AmazonAPI Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
36 changes: 1 addition & 35 deletions src/AmazonAPI.php
Expand Up @@ -9,41 +9,7 @@

namespace MarcL;

interface IHttpRequest {
public function execute($url);
}

class CurlHttpRequest implements IHttpRequest {
private $url = NULL;
private $error = NULL;

public function __construct() {
if (!function_exists('curl_init'))
{
throw new \Exception('Curl not found');
}
}

public function execute($url) {
// Use curl to retrieve data from Amazon
$session = curl_init($url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);

if ($response === false) {
$this->error = curl_error($session);
}

curl_close($session);

if (!empty($error)) {
throw new \Exception($error);
}

return($response);
}
}
use MarcL\CurlHttpRequest;

class AmazonAPI
{
Expand Down
41 changes: 41 additions & 0 deletions src/CurlHttpRequest.php
@@ -0,0 +1,41 @@
<?php

namespace MarcL;

interface IHttpRequest {
public function execute($url);
}

class CurlHttpRequest implements IHttpRequest {
private $url = NULL;
private $error = NULL;

public function __construct() {
if (!function_exists('curl_init'))
{
throw new \Exception('Curl not found');
}
}

public function execute($url) {
// Use curl to retrieve data from Amazon
$session = curl_init($url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);

if ($response === false) {
$this->error = curl_error($session);
}

curl_close($session);

if (!empty($error)) {
throw new \Exception($error);
}

return($response);
}
}

?>

0 comments on commit f4f4e3b

Please sign in to comment.