Skip to content

Commit 966131a

Browse files
authored
Major update (#2)
* Added phpunit, base test class. Renamed PrintfulClient to PrintfulApiClient. API URL is now public property, can be overwritten for testing purposes. * Discarded PrintfulApi namespace part * Mockup generator printfile method, tests. Option to override api client URL in tests by setting it in Credentials. * Printfile grouping by unique placements and templates * Multiple placement/printfile grouping test * canRotate() field, getRatio helper method. * Wrappers for tax/rates and tax/countries endpoints * Tax rate retrieval. Additional test * API changes * Tax rates. Added isShippingRequired boolean * PSR auto format * Mockup generation. Tests. * Mockup generation. Extra mockup field for MockupItem * Mockup generation. Added option to provide format JPG or PNG * Updated user agent * Increase version
1 parent 0901e1b commit 966131a

30 files changed

+1246
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor/
22
composer.lock
33
.idea
4+
tests/Credentials.php

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@
1313
"ext-curl": "*",
1414
"ext-json": "*"
1515
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^5.7"
18+
},
1619
"autoload": {
17-
"psr-0": {
18-
"Printful\\PrintfulApi": "src/"
20+
"psr-4": {
21+
"Printful\\": "src/"
22+
}
23+
},
24+
"autoload-dev": {
25+
"psr-4": {
26+
"Printful\\Tests\\": "tests/"
1927
}
2028
},
2129
"minimum-stability": "stable"

examples/orders.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
2-
use Printful\PrintfulApi\Exceptions\PrintfulApiException;
3-
use Printful\PrintfulApi\Exceptions\PrintfulException;
4-
use Printful\PrintfulApi\PrintfulClient;
2+
use Printful\Exceptions\PrintfulApiException;
3+
use Printful\Exceptions\PrintfulException;
4+
use Printful\PrintfulApiClient;
55

66
require_once __DIR__ . '../vendor/autoload.php';
77

88
// Replace this with your API key
99
$apiKey = '';
1010

11-
$pf = new PrintfulClient($apiKey);
11+
$pf = new PrintfulApiClient($apiKey);
1212

1313
try {
1414

@@ -66,7 +66,7 @@
6666
$order = $pf->post('orders', [
6767
'recipient' => [
6868
'name' => 'John Doe',
69-
'address1' => '172 W Providencia Ave #105',
69+
'address1' => '172 W Street Ave #105',
7070
'city' => 'Burbank',
7171
'state_code' => 'CA',
7272
'country_code' => 'US',
@@ -122,7 +122,7 @@
122122
[
123123
'recipient' => [
124124
'name' => 'John Doe',
125-
'address1' => '172 W Providencia Ave #105',
125+
'address1' => '172 W Street Ave #105',
126126
'city' => 'Burbank',
127127
'state_code' => 'CA',
128128
'country_code' => 'US',

examples/products.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
2-
use Printful\PrintfulApi\Exceptions\PrintfulApiException;
3-
use Printful\PrintfulApi\Exceptions\PrintfulException;
4-
use Printful\PrintfulApi\PrintfulClient;
2+
use Printful\Exceptions\PrintfulApiException;
3+
use Printful\Exceptions\PrintfulException;
4+
use Printful\PrintfulApiClient;
55

66
require_once __DIR__ . '../vendor/autoload.php';
77

88
// Replace this with your API key
99
$apiKey = '';
1010

11-
$pf = new PrintfulClient($apiKey);
11+
$pf = new PrintfulApiClient($apiKey);
1212

1313
try {
1414
// Get information about the store

examples/shipping-rates.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
2-
use Printful\PrintfulApi\Exceptions\PrintfulApiException;
3-
use Printful\PrintfulApi\Exceptions\PrintfulException;
4-
use Printful\PrintfulApi\PrintfulClient;
2+
use Printful\Exceptions\PrintfulApiException;
3+
use Printful\Exceptions\PrintfulException;
4+
use Printful\PrintfulApiClient;
55

66
require_once __DIR__ . '../vendor/autoload.php';
77

88
// Replace this with your API key
99
$apiKey = '';
1010

11-
$pf = new PrintfulClient($apiKey);
11+
$pf = new PrintfulApiClient($apiKey);
1212

1313
try {
1414
// Calculate shipping rates for an order

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Printful Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

src/Exceptions/PrintfulApiException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Printful\PrintfulApi\Exceptions;
3+
namespace Printful\Exceptions;
44

55
/**
66
* Printful exception returned from the API

src/Exceptions/PrintfulException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Printful\PrintfulApi\Exceptions;
3+
namespace Printful\Exceptions;
44

55
use Exception;
66

src/MockupGenerator.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
4+
namespace Printful;
5+
6+
7+
use Printful\Structures\Generator\MockupGenerationFile;
8+
use Printful\Structures\Generator\MockupGenerationParameters;
9+
use Printful\Structures\Generator\MockupItem;
10+
use Printful\Structures\Generator\MockupList;
11+
use Printful\Structures\Generator\PrintfileItem;
12+
use Printful\Structures\Generator\ProductPrintfiles;
13+
use Printful\Structures\Generator\VariantPlacementGroup;
14+
use Printful\Structures\Generator\VariantPrintfileItem;
15+
16+
class MockupGenerator
17+
{
18+
/** @var PrintfulApiClient */
19+
private $printfulClient;
20+
21+
/**
22+
* @param PrintfulApiClient $printfulClient
23+
*/
24+
public function __construct(PrintfulApiClient $printfulClient)
25+
{
26+
$this->printfulClient = $printfulClient;
27+
}
28+
29+
/**
30+
* Get all available templates for specific printful product
31+
*
32+
* @param int $productId Printful product id
33+
* @return ProductPrintfiles
34+
*/
35+
public function getProductPrintfiles($productId)
36+
{
37+
$raw = $this->printfulClient->get('mockup-generator/printfiles/' . $productId);
38+
39+
$productPrintfiles = new ProductPrintfiles;
40+
41+
$productPrintfiles->productId = $raw['product_id'];
42+
$productPrintfiles->availablePlacements = $raw['available_placements'];
43+
$productPrintfiles->printfiles = [];
44+
$productPrintfiles->variantPrintfiles = [];
45+
46+
foreach ($raw['printfiles'] as $v) {
47+
$printfileItem = PrintfileItem::fromArray($v);
48+
$productPrintfiles->printfiles[$printfileItem->printfileId] = $printfileItem;
49+
}
50+
51+
foreach ($raw['variant_printfiles'] as $v) {
52+
$variantPrintfileItem = VariantPrintfileItem::fromArray($v);
53+
$productPrintfiles->variantPrintfiles[$variantPrintfileItem->variantId] = $variantPrintfileItem;
54+
}
55+
56+
return $productPrintfiles;
57+
}
58+
59+
/**
60+
* Merge variants in to unique printfile + placement groups.
61+
* This group can be used for positioning, that covers a list of variants
62+
*
63+
* @param ProductPrintfiles $productPrintfiles
64+
* @return VariantPlacementGroup[]
65+
*/
66+
public function groupPrintfiles(ProductPrintfiles $productPrintfiles)
67+
{
68+
$re = [];
69+
70+
foreach ($productPrintfiles->variantPrintfiles as $v) {
71+
foreach ($v->placements as $k2 => $v2) {
72+
$key = $k2 . '|' . $v2;
73+
74+
$item = isset($re[$key]) ? $re[$key] : new VariantPlacementGroup;
75+
$item->placement = $k2;
76+
$item->variantIds[] = $v->variantId;
77+
$item->printfile = $productPrintfiles->printfiles[$v2];
78+
79+
$re[$key] = $item;
80+
}
81+
}
82+
83+
return array_values($re);
84+
}
85+
86+
/**
87+
* Generate mockup images for given Printful product and variants.
88+
* This request can take up to multiple seconds!
89+
*
90+
* @param MockupGenerationParameters $parameters
91+
* @return MockupList
92+
*/
93+
public function generateMockups(MockupGenerationParameters $parameters)
94+
{
95+
$files = array_map(function (MockupGenerationFile $file) {
96+
return [
97+
'placement' => $file->placement,
98+
'image_url' => $file->imageUrl,
99+
];
100+
}, $parameters->getFiles());
101+
102+
$data = [
103+
'variant_ids' => $parameters->variantIds,
104+
'files' => $files,
105+
'format' => $parameters->format,
106+
];
107+
108+
$response = $this->printfulClient->post('/mockup-generator/generate/' . $parameters->productId, $data);
109+
110+
$mockupList = new MockupList;
111+
112+
$mockupList->productId = (int)$response['product_id'];
113+
114+
$mockupList->mockups = array_map(function (array $rawMockup) {
115+
return MockupItem::fromArray($rawMockup);
116+
}, $response['mockups']);
117+
118+
return $mockupList;
119+
}
120+
}

src/PrintfulClient.php renamed to src/PrintfulApiClient.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
namespace Printful\PrintfulApi;
3+
namespace Printful;
44

5-
use Printful\PrintfulApi\Exceptions\PrintfulApiException;
6-
use Printful\PrintfulApi\Exceptions\PrintfulException;
5+
use Printful\Exceptions\PrintfulApiException;
6+
use Printful\Exceptions\PrintfulException;
77

88
/**
99
* Class PrintfulClient
1010
*/
11-
class PrintfulClient
11+
class PrintfulApiClient
1212
{
1313
/**
1414
* Printful API key
@@ -20,13 +20,13 @@ class PrintfulClient
2020

2121
private $lastResponse;
2222

23-
const API_URL = 'https://api.theprintful.com/';
23+
public $url = 'https://api.theprintful.com/';
2424

25-
const USER_AGENT = 'Printful API PHP Library 1.0';
25+
const USER_AGENT = 'Printful PHP API SDK 2.0';
2626

2727
/**
2828
* @param string $key Printful Store API key
29-
* @throws PrintfulException if the library failed to initialize
29+
* @throws \Printful\Exceptions\PrintfulException if the library failed to initialize
3030
*/
3131
public function __construct($key)
3232
{
@@ -52,7 +52,7 @@ public function getItemCount()
5252
* @param string $path Request path (e.g. 'orders' or 'orders/123')
5353
* @param array $params Additional GET parameters as an associative array
5454
* @return mixed API response
55-
* @throws PrintfulApiException if the API call status code is not in the 2xx range
55+
* @throws \Printful\Exceptions\PrintfulApiException if the API call status code is not in the 2xx range
5656
* @throws PrintfulException if the API call has failed or the response is invalid
5757
*/
5858
public function get($path, $params = [])
@@ -65,8 +65,8 @@ public function get($path, $params = [])
6565
* @param string $path Request path (e.g. 'orders' or 'orders/123')
6666
* @param array $params Additional GET parameters as an associative array
6767
* @return mixed API response
68-
* @throws PrintfulApiException if the API call status code is not in the 2xx range
69-
* @throws PrintfulException if the API call has failed or the response is invalid
68+
* @throws \Printful\Exceptions\PrintfulApiException if the API call status code is not in the 2xx range
69+
* @throws \Printful\Exceptions\PrintfulException if the API call has failed or the response is invalid
7070
*/
7171
public function delete($path, $params = [])
7272
{
@@ -79,7 +79,7 @@ public function delete($path, $params = [])
7979
* @param array $data Request body data as an associative array
8080
* @param array $params Additional GET parameters as an associative array
8181
* @return mixed API response
82-
* @throws PrintfulApiException if the API call status code is not in the 2xx range
82+
* @throws \Printful\Exceptions\PrintfulApiException if the API call status code is not in the 2xx range
8383
* @throws PrintfulException if the API call has failed or the response is invalid
8484
*/
8585
public function post($path, $data = [], $params = [])
@@ -93,8 +93,8 @@ public function post($path, $data = [], $params = [])
9393
* @param array $data Request body data as an associative array
9494
* @param array $params Additional GET parameters as an associative array
9595
* @return mixed API response
96-
* @throws PrintfulApiException if the API call status code is not in the 2xx range
97-
* @throws PrintfulException if the API call has failed or the response is invalid
96+
* @throws \Printful\Exceptions\PrintfulApiException if the API call status code is not in the 2xx range
97+
* @throws \Printful\Exceptions\PrintfulException if the API call has failed or the response is invalid
9898
*/
9999
public function put($path, $data = [], $params = [])
100100
{
@@ -126,8 +126,8 @@ public function getLastResponse()
126126
* @param array $params
127127
* @param mixed $data
128128
* @return
129-
* @throws PrintfulApiException
130-
* @throws PrintfulException
129+
* @throws \Printful\Exceptions\PrintfulApiException
130+
* @throws \Printful\Exceptions\PrintfulException
131131
*/
132132
private function request($method, $path, array $params = [], $data = null)
133133
{
@@ -140,7 +140,7 @@ private function request($method, $path, array $params = [], $data = null)
140140
$url .= '?' . http_build_query($params);
141141
}
142142

143-
$curl = curl_init(self::API_URL . $url);
143+
$curl = curl_init($this->url . $url);
144144

145145
curl_setopt($curl, CURLOPT_USERPWD, $this->key);
146146
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
@@ -159,12 +159,12 @@ private function request($method, $path, array $params = [], $data = null)
159159

160160
$this->lastResponseRaw = curl_exec($curl);
161161

162-
$errno = curl_errno($curl);
162+
$errorNumber = curl_errno($curl);
163163
$error = curl_error($curl);
164164
curl_close($curl);
165165

166-
if ($errno) {
167-
throw new PrintfulException('CURL: ' . $error, $errno);
166+
if ($errorNumber) {
167+
throw new PrintfulException('CURL: ' . $error, $errorNumber);
168168
}
169169

170170
$this->lastResponse = $response = json_decode($this->lastResponseRaw, true);

0 commit comments

Comments
 (0)