Skip to content

add product variant by sku accessor #64

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
68 changes: 68 additions & 0 deletions src/Api/Models/Product/ProductVariantSkuAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Commercetools\Api\Models\Product;

use Commercetools\Exception\InvalidArgumentException;
use stdClass;

class ProductVariantSkuAccessor
{
/**
* @var ProductVariantCollection
*/
private $variants;

/**
* @psalm-var array<string, int>
*/
private $skuIndex = [];

/**
* @psalm-assert ProductProjection|ProductData $product
* @psalm-param mixed $product
* @param $product
* @throws InvalidArgumentException
*/
private function __construct($product)
{
if (!($product instanceof ProductProjection || $product instanceof ProductData)) {
throw new InvalidArgumentException("");
}
$variants = $product->getVariants();
$variants = $variants != null ? $variants->toArray() ?? [] : [];
$masterVariant = $product->getMasterVariant();
if ($masterVariant != null) {
array_unshift($variants, $masterVariant);
}

/**
* @var array<int, stdClass|ProductVariant> $variants
*/
$this->variants = new ProductVariantCollection($variants);

/**
* @var ProductVariant $variant
*/
foreach ($this->variants as $index => $variant) {
$this->skuIndex[(string)$variant->getSku()] = (int)$index;
}
}

public function getBySku(string $sku): ?ProductVariant
{
if (!isset($this->skuIndex[$sku])) {
return null;
}
return $this->variants->at($this->skuIndex[$sku]);
}

/**
* @psalm-return callable(ProductProjection|ProductData): self
*/
public static function of()
{
return function ($productProjection): ProductVariantSkuAccessor {
return new self($productProjection);
};
}
}
29 changes: 18 additions & 11 deletions test/unit/ProductProjectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

namespace Commercetools\UnitTest;

use Commercetools\Api\Client\ApiRequestBuilder;
use Commercetools\Api\Models\Product\Attribute;
use Commercetools\Api\Models\Product\AttributeAccessor;
use Commercetools\Api\Models\Product\ProductProjection;
use Commercetools\Api\Models\Product\ProductProjectionModel;
use Commercetools\Api\Models\Product\ProductProjectionPagedQueryResponse;
use Commercetools\Api\Models\Product\ProductProjectionPagedQueryResponseModel;
use GuzzleHttp\ClientInterface;
use Commercetools\Api\Models\Product\ProductProjectionVariantSkuAccessor;
use Commercetools\Api\Models\Product\ProductVariantSkuAccessor;
use PHPUnit\Framework\TestCase;

class ProductProjectionsTest extends TestCase
Expand All @@ -19,12 +16,7 @@ public function testAttributeMapping()
$productsJson = json_decode(file_get_contents(__DIR__ . "/products.json"));
$products = ProductProjectionPagedQueryResponseModel::fromStdClass($productsJson)->getResults();

// /** @var ClientInterface $client */
// $builder = new ApiRequestBuilder($client);
// $productResponse = $builder->withProjectKey("")->productProjections()->get()->execute();
// $products = $productResponse->getResults();

// /** @var ProductProjection $product */
/** @var ProductProjection $product */
foreach ($products as $product) {
foreach ($product->getMasterVariant()->getAttributes() as $attribute) {
/** @var AttributeAccessor $attrAccessor */
Expand All @@ -39,4 +31,19 @@ public function testAttributeMapping()
}
}
}

public function testSkuMapping()
{
$productsJson = json_decode(file_get_contents(__DIR__ . "/products.json"));
$products = ProductProjectionPagedQueryResponseModel::fromStdClass($productsJson)->getResults();

$variantsAccessor = $products[0]->with(ProductVariantSkuAccessor::of());
self::assertSame(1, $variantsAccessor->getBySku("prod1-abc")->getId());
self::assertNull($variantsAccessor->getBySku("non-existant"));
self::assertSame(2, $variantsAccessor->getBySku("prod1-def")->getId());

$variantsAccessor = $products[1]->with(ProductVariantSkuAccessor::of());
self::assertSame(1, $variantsAccessor->getBySku("prod2-abc")->getId());
self::assertSame(2, $variantsAccessor->getBySku("prod2-def")->getId());
}
}
36 changes: 34 additions & 2 deletions test/unit/products.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"masterVariant": {
"id": 1,
"sku": "prod1-abc",
"prices": [],
"images": [],
"attributes": [
Expand All @@ -33,7 +34,24 @@
}
]
},
"variants": [],
"variants": [
{
"id": 2,
"sku": "prod1-def",
"prices": [],
"images": [],
"attributes": [
{
"name": "text1",
"value": {
"it": "italian1",
"de": "german1",
"en": "englisch1"
}
}
]
}
],
"searchKeywords": {},
"hasStagedChanges": false,
"published": true,
Expand Down Expand Up @@ -62,6 +80,7 @@
"masterVariant": {
"id": 1,
"prices": [],
"sku": "prod2-abc",
"images": [],
"attributes": [
{
Expand All @@ -70,7 +89,20 @@
}
]
},
"variants": [],
"variants": [
{
"id": 2,
"prices": [],
"sku": "prod2-def",
"images": [],
"attributes": [
{
"name": "aboolean",
"value": true
}
]
}
],
"searchKeywords": {},
"hasStagedChanges": true,
"published": true,
Expand Down