Overview

Namespaces

  • LeadCommerce
    • Shopware
      • SDK
        • Entity
        • Exception
        • Query
        • Util

Classes

  • LeadCommerce\Shopware\SDK\Entity\Address
  • LeadCommerce\Shopware\SDK\Entity\Area
  • LeadCommerce\Shopware\SDK\Entity\Article
  • LeadCommerce\Shopware\SDK\Entity\ArticleAttribute
  • LeadCommerce\Shopware\SDK\Entity\ArticleDetail
  • LeadCommerce\Shopware\SDK\Entity\Base
  • LeadCommerce\Shopware\SDK\Entity\Billing
  • LeadCommerce\Shopware\SDK\Entity\BillingAttribute
  • LeadCommerce\Shopware\SDK\Entity\Cache
  • LeadCommerce\Shopware\SDK\Entity\Category
  • LeadCommerce\Shopware\SDK\Entity\ConfiguratorGroup
  • LeadCommerce\Shopware\SDK\Entity\ConfiguratorOption
  • LeadCommerce\Shopware\SDK\Entity\ConfiguratorSet
  • LeadCommerce\Shopware\SDK\Entity\Country
  • LeadCommerce\Shopware\SDK\Entity\Currency
  • LeadCommerce\Shopware\SDK\Entity\CustomerAttribute
  • LeadCommerce\Shopware\SDK\Entity\CustomerGroup
  • LeadCommerce\Shopware\SDK\Entity\CustomerGroupSurcharge
  • LeadCommerce\Shopware\SDK\Entity\Debit
  • LeadCommerce\Shopware\SDK\Entity\Dispatch
  • LeadCommerce\Shopware\SDK\Entity\Document
  • LeadCommerce\Shopware\SDK\Entity\DocumentAttribute
  • LeadCommerce\Shopware\SDK\Entity\DocumentType
  • LeadCommerce\Shopware\SDK\Entity\Download
  • LeadCommerce\Shopware\SDK\Entity\Image
  • LeadCommerce\Shopware\SDK\Entity\Link
  • LeadCommerce\Shopware\SDK\Entity\Locale
  • LeadCommerce\Shopware\SDK\Entity\OrderAttribute
  • LeadCommerce\Shopware\SDK\Entity\OrderDetail
  • LeadCommerce\Shopware\SDK\Entity\OrderDetailAttribute
  • LeadCommerce\Shopware\SDK\Entity\OrderStatus
  • LeadCommerce\Shopware\SDK\Entity\PaymentData
  • LeadCommerce\Shopware\SDK\Entity\PaymentInstance
  • LeadCommerce\Shopware\SDK\Entity\Price
  • LeadCommerce\Shopware\SDK\Entity\PriceGroup
  • LeadCommerce\Shopware\SDK\Entity\PropertyGroup
  • LeadCommerce\Shopware\SDK\Entity\PropertyGroupAttribute
  • LeadCommerce\Shopware\SDK\Entity\PropertyGroupOption
  • LeadCommerce\Shopware\SDK\Entity\PropertyValue
  • LeadCommerce\Shopware\SDK\Entity\Related
  • LeadCommerce\Shopware\SDK\Entity\Shipping
  • LeadCommerce\Shopware\SDK\Entity\ShippingAttribute
  • LeadCommerce\Shopware\SDK\Entity\Shop
  • LeadCommerce\Shopware\SDK\Entity\Similar
  • LeadCommerce\Shopware\SDK\Entity\State
  • LeadCommerce\Shopware\SDK\Entity\Supplier
  • LeadCommerce\Shopware\SDK\Entity\Tax
  • LeadCommerce\Shopware\SDK\Entity\Translation
  • LeadCommerce\Shopware\SDK\Entity\Version
  • LeadCommerce\Shopware\SDK\Query\AddressQuery
  • LeadCommerce\Shopware\SDK\Query\ArticleQuery
  • LeadCommerce\Shopware\SDK\Query\Base
  • LeadCommerce\Shopware\SDK\Query\CacheQuery
  • LeadCommerce\Shopware\SDK\Query\CategoriesQuery
  • LeadCommerce\Shopware\SDK\Query\CountriesQuery
  • LeadCommerce\Shopware\SDK\Query\CustomerGroupsQuery
  • LeadCommerce\Shopware\SDK\Query\CustomerQuery
  • LeadCommerce\Shopware\SDK\Query\GenerateArticleImagesQuery
  • LeadCommerce\Shopware\SDK\Query\ManufacturersQuery
  • LeadCommerce\Shopware\SDK\Query\MediaQuery
  • LeadCommerce\Shopware\SDK\Query\OrdersQuery
  • LeadCommerce\Shopware\SDK\Query\PropertyGroupsQuery
  • LeadCommerce\Shopware\SDK\Query\ShopsQuery
  • LeadCommerce\Shopware\SDK\Query\TranslationsQuery
  • LeadCommerce\Shopware\SDK\Query\VariantsQuery
  • LeadCommerce\Shopware\SDK\Query\VersionQuery
  • LeadCommerce\Shopware\SDK\ShopwareClient
  • LeadCommerce\Shopware\SDK\Util\Constants

Exceptions

  • LeadCommerce\Shopware\SDK\Exception\MethodNotAllowedException
  • Overview
  • Namespace
  • Class
  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:  * Class Base
 12:  *
 13:  * @author Alexander Mahrt <amahrt@leadcommerce.de>
 14:  * @copyright 2016 LeadCommerce <amahrt@leadcommerce.de>
 15:  */
 16: abstract class Base
 17: {
 18:     /**
 19:      * @var ShopwareClient
 20:      */
 21:     protected $client;
 22: 
 23:     /**
 24:      * @var string
 25:      */
 26:     protected $queryPath;
 27: 
 28:     /**
 29:      * @var array
 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:      * Base constructor.
 43:      *
 44:      * @param $client
 45:      */
 46:     public function __construct($client)
 47:     {
 48:         $this->client = $client;
 49:         $this->queryPath = $this->getQueryPath();
 50:     }
 51: 
 52:     /**
 53:      * Gets the query path to look for entities.
 54:      * E.G: 'variants' or 'articles'
 55:      *
 56:      * @return string
 57:      */
 58:     abstract protected function getQueryPath();
 59: 
 60:     /**
 61:      * Finds all entities.
 62:      *
 63:      * @return \LeadCommerce\Shopware\SDK\Entity\Base[]
 64:      */
 65:     public function findAll()
 66:     {
 67:         $this->validateMethodAllowed(Constants::METHOD_GET_BATCH);
 68: 
 69:         return $this->fetch($this->queryPath);
 70:     }
 71: 
 72:     /**
 73:      * Validates if the requested method is allowed.
 74:      *
 75:      * @param $method
 76:      *
 77:      * @throws MethodNotAllowedException
 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:      * Fetch and build entity.
 88:      *
 89:      * @param $uri
 90:      * @param string $method
 91:      * @param null   $body
 92:      * @param array  $headers
 93:      *
 94:      * @return array|mixed
 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:      * Creates an entity
105:      *
106:      * @param ResponseInterface $response
107:      *
108:      * @return array|mixed
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:      * Creates an entity based on the getClass method.
127:      *
128:      * @param $content
129:      *
130:      * @return \LeadCommerce\Shopware\SDK\Entity\Base
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:      * Gets the class for the entities.
147:      *
148:      * @return string
149:      */
150:     abstract protected function getClass();
151: 
152:     /**
153:      * Finds an entity by its id.
154:      *
155:      * @param $id
156:      *
157:      * @return \LeadCommerce\Shopware\SDK\Entity\Base
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:      * Creates an entity.
168:      *
169:      * @param \LeadCommerce\Shopware\SDK\Entity\Base $entity
170:      *
171:      * @throws MethodNotAllowedException
172:      *
173:      * @return \LeadCommerce\Shopware\SDK\Entity\Base
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:      * Updates an entity.
184:      *
185:      * @param \LeadCommerce\Shopware\SDK\Entity\Base $entity
186:      *
187:      * @throws MethodNotAllowedException
188:      *
189:      * @return array|mixed
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:      * Updates a batch of this entity.
200:      *
201:      * @param \LeadCommerce\Shopware\SDK\Entity\Base[] $entities
202:      *
203:      * @return \LeadCommerce\Shopware\SDK\Entity\Base[]
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:      * Deletes an entity by its id..
218:      *
219:      * @param $id
220:      *
221:      * @throws MethodNotAllowedException
222:      *
223:      * @return array|mixed
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:      * Deletes a batch of this entity given by ids.
234:      *
235:      * @param array $ids
236:      *
237:      * @throws MethodNotAllowedException
238:      *
239:      * @return array|mixed
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:      * @param $uri
250:      * @param string $method
251:      * @param null   $body
252:      * @param array  $headers
253:      *
254:      * @return mixed|ResponseInterface
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:      * Fetch as json object.
263:      *
264:      * @param $uri
265:      * @param string $method
266:      * @param null   $body
267:      * @param array  $headers
268:      *
269:      * @return false|\stdClass
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: 
shopware-sdk API documentation generated by ApiGen