1: <?php
2:
3: namespace LeadCommerce\Shopware\SDK\Query;
4:
5: use LeadCommerce\Shopware\SDK\Exception\MethodNotAllowedException;
6: use LeadCommerce\Shopware\SDK\ShopwareClient;
7: use LeadCommerce\Shopware\SDK\Util\Constants;
8: use Psr\Http\Message\ResponseInterface;
9:
10: 11: 12: 13: 14: 15:
16: abstract class Base
17: {
18: 19: 20:
21: protected $client;
22:
23: 24: 25:
26: protected $queryPath;
27:
28: 29: 30:
31: protected $methodsAllowed = [
32: Constants::METHOD_CREATE,
33: Constants::METHOD_GET,
34: Constants::METHOD_GET_BATCH,
35: Constants::METHOD_UPDATE,
36: Constants::METHOD_UPDATE_BATCH,
37: Constants::METHOD_DELETE,
38: Constants::METHOD_DELETE_BATCH,
39: ];
40:
41: 42: 43: 44: 45:
46: public function __construct($client)
47: {
48: $this->client = $client;
49: $this->queryPath = $this->getQueryPath();
50: }
51:
52: 53: 54: 55: 56: 57:
58: abstract protected function getQueryPath();
59:
60: 61: 62: 63: 64:
65: public function findAll()
66: {
67: $this->validateMethodAllowed(Constants::METHOD_GET_BATCH);
68:
69: return $this->fetch($this->queryPath);
70: }
71:
72: 73: 74: 75: 76: 77: 78:
79: private function validateMethodAllowed($method)
80: {
81: if (!in_array($method, $this->methodsAllowed)) {
82: throw new MethodNotAllowedException('Method ' . $method . ' is not allowed for ' . get_class($this));
83: }
84: }
85:
86: 87: 88: 89: 90: 91: 92: 93: 94: 95:
96: protected function fetch($uri, $method = 'GET', $body = null, $headers = [])
97: {
98: $response = $this->client->request($uri, $method, $body, $headers);
99:
100: return $this->createEntityFromResponse($response);
101: }
102:
103: 104: 105: 106: 107: 108: 109:
110: protected function createEntityFromResponse(ResponseInterface $response)
111: {
112: $content = $response->getBody()->getContents();
113: $content = json_decode($content);
114: $content = $content->data;
115:
116: if (is_array($content)) {
117: return array_map(function ($item) {
118: return $this->createEntity($item);
119: }, $content);
120: } else {
121: return $this->createEntity($content);
122: }
123: }
124:
125: 126: 127: 128: 129: 130: 131:
132: protected function createEntity($content)
133: {
134: $class = $this->getClass();
135: $entity = new $class();
136:
137: if ($entity instanceof \LeadCommerce\Shopware\SDK\Entity\Base) {
138: $content = json_decode(json_encode($content), true);
139: $entity->setEntityAttributes($content);
140: }
141:
142: return $entity;
143: }
144:
145: 146: 147: 148: 149:
150: abstract protected function getClass();
151:
152: 153: 154: 155: 156: 157: 158:
159: public function findOne($id)
160: {
161: $this->validateMethodAllowed(Constants::METHOD_GET);
162:
163: return $this->fetch($this->queryPath . '/' . $id);
164: }
165:
166: 167: 168: 169: 170: 171: 172: 173: 174:
175: public function create(\LeadCommerce\Shopware\SDK\Entity\Base $entity)
176: {
177: $this->validateMethodAllowed(Constants::METHOD_CREATE);
178:
179: return $this->fetch($this->queryPath, 'POST', $entity->getArrayCopy());
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189: 190:
191: public function update(\LeadCommerce\Shopware\SDK\Entity\Base $entity)
192: {
193: $this->validateMethodAllowed(Constants::METHOD_UPDATE);
194:
195: return $this->fetch($this->queryPath . '/' . $entity->getId(), 'PUT', $entity->getArrayCopy());
196: }
197:
198: 199: 200: 201: 202: 203: 204:
205: public function updateBatch($entities)
206: {
207: $this->validateMethodAllowed(Constants::METHOD_UPDATE_BATCH);
208: $body = [];
209: foreach ($entities as $entity) {
210: $body[] = $entity->getArrayCopy();
211: }
212:
213: return $this->fetch($this->queryPath . '/', 'PUT', $body);
214: }
215:
216: 217: 218: 219: 220: 221: 222: 223: 224:
225: public function delete($id)
226: {
227: $this->validateMethodAllowed(Constants::METHOD_DELETE);
228:
229: return $this->fetch($this->queryPath . '/' . $id, 'DELETE');
230: }
231:
232: 233: 234: 235: 236: 237: 238: 239: 240:
241: public function deleteBatch(array $ids)
242: {
243: $this->validateMethodAllowed(Constants::METHOD_DELETE_BATCH);
244:
245: return $this->fetch($this->queryPath . '/', 'DELETE', $ids);
246: }
247:
248: 249: 250: 251: 252: 253: 254: 255:
256: protected function fetchSimple($uri, $method = 'GET', $body = null, $headers = [])
257: {
258: return $this->client->request($uri, $method, $body, $headers);
259: }
260:
261: 262: 263: 264: 265: 266: 267: 268: 269: 270:
271: protected function fetchJson($uri, $method = 'GET', $body = null, $headers = [])
272: {
273: $response = $this->client->request($uri, $method, $body, $headers);
274: $response = json_decode($response->getBody()->getContents());
275:
276: return $response ? $response : null;
277: }
278: }
279: