Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added InventoryItem and InventoryLevel to SDK #46

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3bcc437
add inventory interface to api
JelleDijkhuizen Sep 16, 2020
8bd52ab
bumped default api version
JelleDijkhuizen Sep 18, 2020
163ef87
fixed typo
JelleDijkhuizen Sep 21, 2020
de5abc3
fixed wrong index on iventory_item response
JelleDijkhuizen Sep 21, 2020
39fc198
fixed array to string conversion
JelleDijkhuizen Sep 21, 2020
e6c497f
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
c6f4de3
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
3fa3f8c
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
d188b05
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
7b32c74
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
4c34d37
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
9462f4c
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
5ba88ea
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
03ab477
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
ac03598
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
5f35683
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
b57d419
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
5328c2c
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
9e6fe1e
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
79c283a
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
46ee0f7
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
7d72fbd
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
36d5b5f
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
d50731c
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
0be3658
Update AbstractService.php
JelleDijkhuizen Sep 22, 2020
2fe7899
Fixed some typo's
JelleDijkhuizen Oct 13, 2020
dbc0415
Merge pull request #1 from Dijkma/debug
JelleDijkhuizen Oct 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

abstract class AbstractApi implements ApiInterface
{
const DEFAULT_API_VERSION = '2019-04';
const DEFAULT_API_VERSION = '2020-07';

/**
* Domain of the Shopify store
Expand Down
2 changes: 1 addition & 1 deletion src/Enum/Fields/CollectionListingFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Shopify\Enum\Fields;

class CollectionListingFields extends AbstractObject
class CollectionListingFields extends AbstractObjectEnum
{
const COLLECTION_ID = 'collection_id';
const BODY_HTML = 'body_html';
Expand Down
30 changes: 30 additions & 0 deletions src/Enum/Fields/InventoryItemFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Shopify\Enum\Fields;

class InventoryItemFields extends AbstractObjectEnum
{
const COST = 'cost'; //The unit cost of the inventory item.
const COUNTRY_CODE_OF_ORIGIN = 'country_code_of_origin'; //The two-digit code for the country where the inventory item was made.
const COUNTRY_HARMONIZED_SYSTEM_CODES = 'country_harmonized_system_codes'; //An array of country-specific Harmonized System (HS) codes for the item. Used to determine duties when shipping the inventory item to certain countries.
const CREATED_AT = 'created_at'; //The date and time (ISO 8601 format) when the inventory item was created.
const HARMONIZED_SYSTEM_CODE = 'harmonized_system_code'; //The general Harmonized System (HS) code for the inventory item. Used if a country-specific HS code is not available.
const ID = 'id'; //The ID of the inventory item.
const PROVINCE_CODE_OF_ORIGIN = 'province_code_of_origin'; //The two-digit code for the province where the inventory item was made. Used only if the shipping provider for the inventory item is Canada Post.
const SKU = 'sku'; //The unique SKU (stock keeping unit) of the inventory item.
const TRACKED = 'tracked'; //Whether the inventory item is tracked. If true, then inventory quantity changes are tracked by Shopify.

public function getFieldTypes()
{
return array(
'cost' => 'integer',
'country_code_of_origin' => 'string',
'country_harmonized_system_codes' => 'array',
'harmonized_system_code' => 'integer',
'id' => 'integer',
'province_code_of_origin' => 'string',
'sku' => 'string',
'tracked' => 'boolean',
);
}
}
23 changes: 23 additions & 0 deletions src/Enum/Fields/InventoryLevelFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Shopify\Enum\Fields;

class InventoryLevelFields extends AbstractObjectEnum
{
const INVENTORY_ITEM_ID = 'inventory_item_id'; //The ID for the inventory item.
const LOCATION_ID = 'location_id'; //The ID of the location that the inventory level belongs to. To find the ID of the location, use the Location resource.
const AVAILABLE = 'available'; //Sets the available inventory quantity.
const AVAILABLE_ADJUSTMENT = 'available_adjustment'; //The amount to adjust the available inventory quantity. Send negative values to subtract from the current available quantity. For example, "available_adjustment": 2 increases the current available quantity by 2, and "available_adjustment": -3 decreases the current available quantity by 3.
const DISCONNECT_IF_NECESSARY = 'disconnect_if_necessary'; //Whether inventory for any previously connected locations will be set to 0 and the locations disconnected. This property is ignored when no fulfillment service is involved. For more information, see Inventory levels and fulfillment service locations. (default: false)

public function getFieldTypes()
{
return array(
'inventory_item_id' => 'integer',
'location_id' => 'integer',
'available' => 'integer',
'available_adjustment' => 'integer',
'disconnect_if_necessary' => 'boolean',
);
}
}
6 changes: 3 additions & 3 deletions src/Object/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct()
$this->data = array_fill_keys($enum->getFields(), null);
}

public function __get($key)
public function &__get($key)
{
if (!array_key_exists($key, $this->data)) {
throw new \InvalidArgumentException(
Expand All @@ -61,7 +61,7 @@ public function __get($key)
return $this->data[$key];
}

public function __set($key, $value)
public function &__set($key, $value)
{
if (!array_key_exists($key, $this->data)) {
return $this;
Expand All @@ -73,7 +73,7 @@ public function __set($key, $value)
}
if (!is_null($value) && !$this->isValidValue($key, $value)) {
throw new \InvalidArgumentException(
"Invalid type for property '{$key}'"
"Invalid type for property '{$key}', should be a ".$this->types[$key]." received ".print_r($value,1)
);
}
$this->data[$key] = $value;
Expand Down
44 changes: 44 additions & 0 deletions src/Object/InventoryItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Shopify\Object\InventoryItem
*
* Object representing a single Inventory Item for a product
*
* MIT License
*
* Copyright (c) Jelle Dijkhuizen 2020
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @package Shopify
* @author Jelle Dijkhuizen <[email protected]>
* @license MIT
*/

namespace Shopify\Object;

use Shopify\Enum\Fields\InventoryItemFields;

class InventoryItem extends AbstractObject
{
public static function getFieldsEnum()
{
return InventoryItemFields::getInstance();
}
}
44 changes: 44 additions & 0 deletions src/Object/InventoryLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Shopify\Object\InventoryLevel
*
* Object representing a single Inventory Level for an Inventory item of a product
*
* MIT License
*
* Copyright (c) Jelle Dijkhuizen 2020
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @package Shopify
* @author Jelle Dijkhuizen <[email protected]>
* @license MIT
*/

namespace Shopify\Object;

use Shopify\Enum\Fields\InventoryLevelFields;

class InventoryLevel extends AbstractObject
{
public static function getFieldsEnum()
{
return InventoryLevelFields::getInstance();
}
}
37 changes: 29 additions & 8 deletions src/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Shopify\Service;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
Expand All @@ -21,9 +22,9 @@ abstract class AbstractService
*/
private $lastResponse;

const REQUEST_METHOD_GET = 'GET';
const REQUEST_METHOD_POST = 'POST';
const REQUEST_METHOD_PUT = 'PUT';
const REQUEST_METHOD_GET = 'GET';
const REQUEST_METHOD_POST = 'POST';
const REQUEST_METHOD_PUT = 'PUT';
const REQUEST_METHOD_DELETE = 'DELETE';

public static function factory(ApiInterface $api)
Expand Down Expand Up @@ -78,17 +79,37 @@ public function getLastResponse()

public function send(Request $request, array $params = array())
{
$args = array();
//Set the empty args array
$args = [];

//If the method is get we need to send the args as a query other wise the args need to be send as json
if ($request->getMethod() === 'GET') {
$args['query'] = $params;
} else {
$args['json'] = $params;
}
else {
$args['json'] = $params;
}

//Load the response in a variable
$this->lastResponse = $this->client->send($request, $args);
return json_decode(

//Decode the json string and save to a varibale.
//We do need return this derict so we can check for an json error.
$return = json_decode(
$this->lastResponse->getBody()->getContents(),
true
);

//Check if there are any json error.
//if there is an error throw an exeption
//TOD: make costum exeption.
$json_error = json_last_error();
if($json_error === JSON_ERROR_NONE){
return $return;
}
else{
throw new Exception($json_error);
}
}

public function createObject($className, $data)
Expand All @@ -106,4 +127,4 @@ function ($object) use ($className) {
}, $data
);
}
}
}
2 changes: 1 addition & 1 deletion src/Service/CustomCollectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function update(CustomCollection &$customCollection)
*/
public function delete(CustomCollection &$customCollection)
{
$endpoint = 'custom_collections/'.$customCollection->getId().'.json';
$endpoint = 'custom_collections/'.$customCollection->id.'.json';
$this->request($endpoint, static::REQUEST_METHOD_DELETE);
return;
}
Expand Down
60 changes: 60 additions & 0 deletions src/Service/InventoryItemService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Shopify\Service;

use Shopify\Object\InventoryItem;

class InventoryItemService extends AbstractService
{
/**
* Retrieves a list of inventory items.
* Note: As of version 2019-10, this endpoint implements pagination by using links that are provided in the response header.
* Sending the page parameter will return an error.
* To learn more, see Making requests to paginated REST Admin API endpoints.
*
* @link https://shopify.dev/docs/admin-api/rest/reference/inventory/inventoryitem#index-2020-07
* @param array $params
* @return InventoryItem[]
*/
public function all(array $params = []){
$endpoint = 'inventory_items.json';
$response = $this->request($endpoint, 'GET', $params);
return $this->createCollection(InventoryItem::class, $response['inventory_item']);
}

/**
* Retrieves a single inventory item by ID
*
* @link https://shopify.dev/docs/admin-api/rest/reference/inventory/inventoryitem#show-2020-07
* @param int $inventroyItemId
* @param array $fields
* @return InventoryItem[]
*/
public function get(int $inventroyItemId, array $fields = []){
$params = [];
if (!empty($fields)) {
$params['fields'] = $fields;
}
$endpoint = 'inventory_items/'.$inventroyItemId.'.json';
$response = $this->request($endpoint, 'GET', $params);
return $this->createObject(InventoryItem::class, $response['inventory_item']);
}

/**
* Updates an existing inventory item
*
* @link https://shopify.dev/docs/admin-api/rest/reference/inventory/inventoryitem#update-2020-07
* @param InventoryItem $inventoryItem
* @return void
*/
public function update(InventoryItem &$inventoryItem){
$data = $inventoryItem->exportData();
$endpoint = 'inventory_items/'.$inventoryItem->id.'.json';
$response = $this->request(
$endpoint, 'PUT', array(
'inventory_item' => $data
)
);
$inventoryItem->setData($response['inventory_item']);
}
}
Loading