public function synchronize($wishlistId, $countryCode) { $this->amazonCrawler->setWishlistId($wishlistId); $this->amazonCrawler->setCountryCode($countryCode); $items = $this->amazonCrawler->crawlItems(); $itemsToFetch = array(); $products = array(); foreach ($items as $item) { $product = $this->productService->getProductByAsin($item['asin']); if (null === $product) { $itemsToFetch[] = $item['asin']; } else { $products[] = $product; } } if (0 === count($items)) { echo 'no products found' . PHP_EOL; return; } $apaiIoResultSet = $this->apaiIOWrapper->getByASINS($itemsToFetch, $item['tld']); foreach ($apaiIoResultSet as $itemDetails) { $product = $this->productService->createProductByXml($itemDetails); $products[] = $product; $this->productService->getEntityManager()->persist($product); } $wishList = $this->wishlistService->findByWishlistID($wishlistId); if (null === $wishList) { $wishList = new Wishlist(); $wishList->setName('IDK'); $wishList->setWishlistOwnerName('IDK'); $wishList->setAmazonId($wishlistId); $wishList->setTld($countryCode); //@todo owner & so on } $wishList->setProducts($products); $this->wishlistService->persistWishList($wishList); $this->wishlistService->getEntityManager()->flush(); }
/** * fetch wishlist and write CSV */ public function execute() { $amazonCrawler = new AmazonCrawler($this->client, $this->logger); $amazonCrawler->setCountryCode($this->countryCode); $amazonCrawler->setWishlistId($this->wishlistId); $items = $amazonCrawler->crawlItems(); $items = array_merge([['Name', 'Price', 'Url', 'Image']], $items); // Saving each item row to the file $fh = fopen($this->pathToSave, 'w'); foreach ($items as $item) { fputcsv($fh, $item); } fclose($fh); }
public function testExecuteOnUkWithMultiplePagesOfItems() { $responseMock = $this->getMock('\\GuzzleHttp\\Message\\ResponseInterface'); $responseMock->expects($this->exactly(4))->method('getStatusCode')->will($this->returnValue(200)); $responseMock->expects($this->exactly(4))->method('getBody')->will($this->onConsecutiveCalls($this->responseContentPage1Fixture, $this->responseContentPage2Fixture, $this->responseContentPage3Fixture, $this->responseContentPage3Fixture)); $this->clientMock->expects($this->exactly(4))->method('get')->will($this->returnValue($responseMock)); $this->loggerMock->expects($this->exactly(6))->method('log')->withAnyParameters(); $crawler = new AmazonCrawler($this->clientMock, $this->loggerMock, 'ABC123', 'UK'); $result = $crawler->crawlItems(); $expectedResult = [['name' => 'Product 1', 'price' => 1.99, 'url' => 'http://www.amazon.co.uk/product-1', 'image' => 'test://product-1.png'], ['name' => 'Product 2', 'price' => 2.99, 'url' => 'http://www.amazon.co.uk/product-2', 'image' => 'test://product-2.png'], ['name' => 'Product 3', 'price' => 3.99, 'url' => 'http://www.amazon.co.uk/product-3', 'image' => 'test://product-3.png'], ['name' => 'Product 4', 'price' => 4.99, 'url' => 'http://www.amazon.co.uk/product-4', 'image' => 'test://product-4.png'], ['name' => 'Product 5', 'price' => 5.99, 'url' => 'http://www.amazon.co.uk/product-5', 'image' => 'test://product-5.png'], ['name' => 'Product 6', 'price' => 6.99, 'url' => 'http://www.amazon.co.uk/product-6', 'image' => 'test://product-6.png'], ['name' => 'Product 7', 'price' => 7.99, 'url' => 'http://www.amazon.co.uk/product-7', 'image' => 'test://product-7.png'], ['name' => 'Product 8', 'price' => 8.99, 'url' => 'http://www.amazon.co.uk/product-8', 'image' => 'test://product-8.png'], ['name' => 'Product 9', 'price' => 9.99, 'url' => 'http://www.amazon.co.uk/product-9', 'image' => 'test://product-9.png']]; $this->assertEquals($expectedResult, $result); }