-
Notifications
You must be signed in to change notification settings - Fork 44
/
entity-repository.php
94 lines (79 loc) · 3.37 KB
/
entity-repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require __DIR__ . '/../vendor/autoload.php';
use Vin\ShopwareSdk\Data\Context;
use Vin\ShopwareSdk\Data\Entity\Product\ProductDefinition;
use Vin\ShopwareSdk\Exception\ShopwareResponseException;
use Vin\ShopwareSdk\Factory\RepositoryFactory;
use Vin\ShopwareSdk\Data\Criteria;
use Vin\ShopwareSdk\Data\Filter\ContainsFilter;
use Vin\ShopwareSdk\Data\Aggregation\SumAggregation;
use Vin\ShopwareSdk\Data\Entity\Product\ProductEntity;
use Vin\ShopwareSdk\Data\Aggregation\CountAggregation;
use Vin\ShopwareSdk\Data\Entity\Product\ProductCollection;
use Vin\ShopwareSdk\Data\Uuid\Uuid;
use Vin\ShopwareSdk\Data\FieldSorting;
class EntityRepositoryExample {
public function execute(): void
{
require __DIR__ . '/token.php';
$context = new Context($config['shop_url'], $accessToken);
$productRepository = RepositoryFactory::create(ProductDefinition::ENTITY_NAME);
$criteria = new Criteria();
$productIds = $productRepository->searchIds($criteria, $context);
// Search api using repositories and criteria
$criteria = new Criteria();
$criteria->setLimit(1);
$criteria->addAssociation('categories');
$criteria->addSorting(new FieldSorting('name', FieldSorting::DESCENDING));
$criteria->addFilter(new ContainsFilter('name', 'A'));
$criteria->addAggregation(new SumAggregation('sumStock', 'stock'));
$criteria->addAggregation(new CountAggregation('countId', 'id'));
$products = $productRepository->search($criteria, $context);
/** @var ProductCollection $productCollection */
$productCollection = $products->getEntities();
$productId = $productCollection->first()->id;
// Repository get
/** @var ProductEntity $product */
$product = $productRepository->get($productId, $criteria, $context);
// Example update and Catch error response
try {
$productRepository->update([
'id' => $productId,
'name' => 'Edited name' . time(),
'stock' => 'abc'
], $context);
} catch (ShopwareResponseException $exception) {
// "errors" => [
// 0 => [
// "code" => "ba785a8c-82cb-4283-967c-3cf342181b40"
// "status" => "400"
// "detail" => "This value should be of type int."
// "template" => "This value should be of type {{ type }}."
// "meta" => [
// "parameters" => array:2 [
// "{{ value }}" => ""abc""
// "{{ type }}" => "int"
// ]
// ]
// "source" => [
// "pointer" => "/0/stock"
// ]
// ]
// ]
$exception->getResponse();
}
// Example Create
$productRepository->create([
'id' => Uuid::randomHex(),
'name' => 'New Product',
'taxId' => $product->taxId,
'price' => $product->price,
'productNumber' => $product->productNumber . random_int(10, 1000),
'stock' => $product->stock,
], $context);
// Example Delete
$productRepository->delete($productId, $context);
}
}
$example = new EntityRepositoryExample();
$example->execute();