Skip to content

Commit

Permalink
Add product reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
McCaulay Hudson committed Mar 20, 2020
1 parent 4de164a commit 0972961
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 5 deletions.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TRUSTPILOT_ACCESS_TOKEN=
- Consumer Profile API
- Invitation API
- Private Products API
- Private Reviews API
- Product Reviews API -> Conversations, Invitation Link
- Resources API
- Service Reviews API
- BUsiness Units API (Private Reviews)
Expand Down Expand Up @@ -81,6 +81,48 @@ $reviews = Trustpilot::businessUnit()
->get()
->pluck('title');

// Get the the first 10 product reviews of your business that are 4 or 5 stars for the specific products.
$productReviews = Trustpilot::businessUnit()
->products()->reviews()
->where('sku', [
'product_1_sku_here',
'product_2_sku_here',
])
->where('stars', [4, 5])
->get();

// Get the joined product review summary of the two specific products for your business.
$productReviewSummary = Trustpilot::businessUnit()
->products()
->reviewSummary([
'product_1_sku_here',
'product_2_sku_here',
]);

// Get the joined product review summary of the two specific products for your business by URL.
$productReviewSummary = Trustpilot::businessUnit()
->products()
->reviewSummary([], [
'https://www.example.com/product_1',
'https://www.example.com/product_2',
]);

// Get the aggregated product review summary of the two specific products for your business.
$aggregatedProductReviewSummaries = Trustpilot::businessUnit()
->products()
->reviewAggregatedSummaries([
'product_1_sku_here',
'product_2_sku_here',
]);

// Get the batched product review summary of the two specific products for your business.
$productReviewSummaries = Trustpilot::businessUnit()
->products()
->reviewBatchSummaries([
'product_1_sku_here',
'product_2_sku_here',
]);

// Get the web links of your business.
$webLinks = Trustpilot::businessUnit()->webLinks();

Expand Down
2 changes: 1 addition & 1 deletion src/API/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function request(string $method, string $path, array $query = [], arra
{
$response = $this->client->request($method, $this->endpoint . $this->path . $path, [
'query' => $query,
'form_params' => $params,
'json' => $params,
]);
$contents = (string) $response->getBody();
return json_decode($contents);
Expand Down
11 changes: 11 additions & 0 deletions src/API/BusinessUnit/BusinessUnit.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace McCaulay\Trustpilot\API\BusinessUnit;

use McCaulay\Trustpilot\API\BusinessUnit\ProductApi;
use McCaulay\Trustpilot\API\Resource;
use McCaulay\Trustpilot\Query\Builder;

Expand Down Expand Up @@ -34,6 +35,16 @@ public function reviews(): Builder
return new Builder(new Review\ReviewApi($this->id));
}

/**
* Get the products api.
*
* @return \McCaulay\Trustpilot\API\BusinessUnit\ProductApi
*/
public function products(): ProductApi
{
return new ProductApi($this->id);
}

/**
* Load the business information.
*
Expand Down
91 changes: 91 additions & 0 deletions src/API/BusinessUnit/ProductApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
namespace McCaulay\Trustpilot\API\BusinessUnit;

use McCaulay\Trustpilot\Api;
use McCaulay\Trustpilot\API\BusinessUnit\Review\Product\ProductReviewSummary;
use McCaulay\Trustpilot\Query\Builder;

class ProductApi extends Api
{
/**
* The business unit id.
*
* @var int
*/
public $businessUnitId = null;

/**
* Initialise the business unit product reviews with an optional business unit id.
* If no business unit id is given, it uses the business unit from the config.
*
* @param null|string $businessUnitId
*/
public function __construct(?string $businessUnitId = null)
{
parent::__construct();
$this->businessUnitId = $businessUnitId ?? config('trustpilot.unit_id');
}

/**
* Get the queried product reviews.
*
* @return \McCaulay\Trustpilot\Query\Builder
*/
public function reviews(): Builder
{
return new Builder(new Review\Product\ProductReviewApi($this->businessUnitId));
}

/**
* Get the joint products review summary.
*
* @param array $skus
* @param array $urls
* @return mixed
*/
public function reviewSummary(array $skus = [], array $urls = [])
{
$response = $this->get('/product-reviews/business-units/' . $this->businessUnitId, array_filter([
'sku' => $skus,
'productUrl' => $urls,
]));

return (new ProductReviewSummary())->data($response);
}

/**
* Get the aggregated business product review summaries.
*
* @param array $skus
* @param string $locale
* @return mixed
*/
public function reviewAggregatedSummaries(array $skus = [], string $locale = 'en-GB')
{
$response = $this->post('/product-reviews/business-units/' . $this->businessUnitId . '/attribute-summaries', [], array_filter([
'skus' => $skus,
'locale' => $locale,
]));

return collect($response->summaries)->map(function ($summary) {
return (new ProductReviewSummary())->data($summary);
});
}

/**
* Get the business product review summaries.
*
* @param array $skus
* @return mixed
*/
public function reviewBatchSummaries(array $skus = [])
{
$response = $this->post('/product-reviews/business-units/' . $this->businessUnitId . '/batch-summaries', [], array_filter([
'skus' => $skus,
]));

return collect($response->summaries)->map(function ($summary) {
return (new ProductReviewSummary())->data($summary);
});
}
}
9 changes: 9 additions & 0 deletions src/API/BusinessUnit/Review/Product/ProductReview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace McCaulay\Trustpilot\API\BusinessUnit\Review\Product;

use McCaulay\Trustpilot\API\Resource;

class ProductReview extends Resource
{
//
}
44 changes: 44 additions & 0 deletions src/API/BusinessUnit/Review/Product/ProductReviewApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace McCaulay\Trustpilot\API\BusinessUnit\Review\Product;

use Illuminate\Support\Collection;
use McCaulay\Trustpilot\API\BusinessUnit\Review;
use McCaulay\Trustpilot\API\ResourceApi;

class ProductReviewApi extends ResourceApi
{
/**
* The business unit id.
*
* @var int
*/
public $businessUnitId = null;

/**
* Initialise the business unit product reviews with an optional business unit id.
* If no business unit id is given, it uses the business unit from the config.
*
* @param null|string $businessUnitId
*/
public function __construct(?string $businessUnitId = null)
{
parent::__construct();
$this->businessUnitId = $businessUnitId ?? config('trustpilot.unit_id');
$this->setPath('/product-reviews/business-units/' . $this->businessUnitId);
}

/**
* Perform the query and get the results.
*
* @param array $query
* @param bool $search
* @return \Illuminate\Support\Collection;
*/
public function perform(array $query, bool $search = false): Collection
{
$response = $this->get('/reviews', $query);
return collect($response->productReviews)->map(function ($productReview) {
return (new ProductReview())->data($productReview);
});
}
}
9 changes: 9 additions & 0 deletions src/API/BusinessUnit/Review/Product/ProductReviewSummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace McCaulay\Trustpilot\API\BusinessUnit\Review\Product;

use McCaulay\Trustpilot\API\Resource;

class ProductReviewSummary extends Resource
{
//
}
2 changes: 1 addition & 1 deletion src/API/BusinessUnit/Review/Review.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace McCaulay\Trustpilot\API\BusinessUnit;
namespace McCaulay\Trustpilot\API\BusinessUnit\Review;

use McCaulay\Trustpilot\API\Resource;

Expand Down
2 changes: 1 addition & 1 deletion src/API/BusinessUnit/Review/ReviewApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace McCaulay\Trustpilot\API\BusinessUnit\Review;

use Illuminate\Support\Collection;
use McCaulay\Trustpilot\API\BusinessUnit\Review;
use McCaulay\Trustpilot\API\BusinessUnit\Review\Review;
use McCaulay\Trustpilot\API\ResourceApi;

class ReviewApi extends ResourceApi
Expand Down
2 changes: 1 addition & 1 deletion src/API/Category/CategoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class CategoryApi extends ResourceApi
{
/**
* Initialise the business unit api
* Initialise the categories api
*/
public function __construct()
{
Expand Down

0 comments on commit 0972961

Please sign in to comment.