1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the MailChimpEcommerceBundle package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 kevin92dev.es |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* Feel free to edit as you please, and have fun. |
11
|
|
|
* |
12
|
|
|
* @author Kevin Murillo <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Kevin92dev\MailChimpEcommerceBundle\Services; |
16
|
|
|
|
17
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\Entities\Product; |
18
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\Entities\ProductVariant; |
19
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\Exceptions\ProductNotFoundException; |
20
|
|
|
use Kevin92dev\MailChimpEcommerceBundle\RequestTypes; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ProductService |
24
|
|
|
* |
25
|
|
|
* @author Kevin Murillo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ProductService |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var MailChimp |
31
|
|
|
*/ |
32
|
|
|
private $mailChimp; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes ProductService |
36
|
|
|
* |
37
|
|
|
* @param MailChimp $mailChimp |
38
|
|
|
*/ |
39
|
|
|
public function __construct(MailChimp $mailChimp) |
40
|
|
|
{ |
41
|
|
|
$this->mailChimp = $mailChimp; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new product in MailChimp |
46
|
|
|
* |
47
|
|
|
* @var Product $product |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function create(Product $product) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$method = RequestTypes::$POST; |
52
|
|
|
$resource = '/products'; |
53
|
|
|
|
54
|
|
|
$body = [ |
55
|
|
|
'id' => strval($product->getId()), |
56
|
|
|
'title' => strval($product->getTitle()), |
57
|
|
|
'handle' => strval($product->getHandle()), |
58
|
|
|
'url' => strval($product->getUrl()), |
59
|
|
|
'description' => strval($product->getDescription()), |
60
|
|
|
'type' => strval($product->getType()), |
61
|
|
|
'vendor' => strval($product->getVendor()), |
62
|
|
|
'image_url' => strval($product->getImageUrl()) |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
$this->addProductVariants($product, $body); |
66
|
|
|
|
67
|
|
|
$this->mailChimp->doRequest($method, $body, $resource); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Edit a product in MailChimp |
72
|
|
|
* |
73
|
|
|
* @var Product $product |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function edit(Product $product) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$method = RequestTypes::$PATCH; |
78
|
|
|
$resource = '/products/'.$product->getId(); |
79
|
|
|
|
80
|
|
|
$body = [ |
81
|
|
|
'title' => strval($product->getTitle()), |
82
|
|
|
'handle' => strval($product->getHandle()), |
83
|
|
|
'url' => strval($product->getUrl()), |
84
|
|
|
'description' => strval($product->getDescription()), |
85
|
|
|
'type' => strval($product->getType()), |
86
|
|
|
'vendor' => strval($product->getVendor()), |
87
|
|
|
'image_url' => strval($product->getImageUrl()) |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
$this->addProductVariants($product, $body); |
91
|
|
|
|
92
|
|
|
$this->mailChimp->doRequest($method, $body, $resource); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Read products from MailChimp |
97
|
|
|
* |
98
|
|
|
* @param Product $product |
99
|
|
|
* @param int $count |
100
|
|
|
* @param int $offset |
101
|
|
|
* |
102
|
|
|
* @return null|string |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function read(Product $product = null, $count = 50, $offset = 0) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$method = RequestTypes::$GET; |
107
|
|
|
|
108
|
|
|
if ($product instanceof Product) { |
109
|
|
|
$resource = '/products/'.$product->getId(); |
110
|
|
|
} else { |
111
|
|
|
$resource = '/products'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$resource .= '?count='.$count.'&offset='.$offset; |
115
|
|
|
|
116
|
|
|
$data = []; |
117
|
|
|
|
118
|
|
|
try { |
119
|
|
|
$request = $this->mailChimp->doRequest($method, $data, $resource); |
120
|
|
|
} catch (ProductNotFoundException $e) { |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $request->getBody()->getContents(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Delete product from MailChimp |
129
|
|
|
* |
130
|
|
|
* @var Product $product |
131
|
|
|
*/ |
132
|
|
|
public function delete(Product $product) |
133
|
|
|
{ |
134
|
|
|
$method = RequestTypes::$DELETE; |
135
|
|
|
$resource = '/products/'.$product->getId(); |
136
|
|
|
|
137
|
|
|
$this->mailChimp->doRequest($method, null, $resource); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add product variants to array |
142
|
|
|
* |
143
|
|
|
* @param Product $product |
144
|
|
|
* @param array $body |
145
|
|
|
*/ |
146
|
|
|
public function addProductVariants(Product $product, &$body) |
147
|
|
|
{ |
148
|
|
|
/** |
149
|
|
|
* @var ProductVariant $productVariant |
150
|
|
|
*/ |
151
|
|
|
foreach ($product->getProductVariants() as $productVariant) { |
152
|
|
|
$body['variants'][] = [ |
153
|
|
|
'id' => strval($productVariant->getId()), |
154
|
|
|
'title' => strval($productVariant->getTitle()), |
155
|
|
|
'url' => strval($productVariant->getUrl()), |
156
|
|
|
'sku' => strval($productVariant->getSku()), |
157
|
|
|
'price' => floatval($productVariant->getPrice()), |
158
|
|
|
'inventory_quantity' => intval($productVariant->getInventoryQuantity()), |
159
|
|
|
'image_url' => strval($productVariant->getImageUrl()), |
160
|
|
|
'backorders' => strval($productVariant->getBackorders()), |
161
|
|
|
'visibility' => strval($productVariant->getVisibility()) |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.