<commit_before><?php
/**
 */

namespace Commercetools\Symfony\ReviewBundle\Model\Repository;

use Commercetools\Core\Builder\Request\RequestBuilder;
use Commercetools\Core\Model\Review\Review;
use Commercetools\Core\Model\Review\ReviewDraft;
use Commercetools\Symfony\CtpBundle\Model\QueryParams;
use Commercetools\Symfony\CtpBundle\Service\MapperFactory;
use Commercetools\Symfony\CtpBundle\Model\Repository;
use Commercetools\Core\Client;
use Psr\Cache\CacheItemPoolInterface;

class ReviewRepository extends Repository
{
    public function __construct(
        $enableCache,
        CacheItemPoolInterface $cache,
        Client $client,
        MapperFactory $mapperFactory
    ) {
        parent::__construct($enableCache, $cache, $client, $mapperFactory);
    }

    public function getReviewById($locale, $reviewId, QueryParams $params = null)
    {
        $request = RequestBuilder::of()->reviews()->getById($reviewId);

        return $this->executeRequest($request, $locale, $params);
    }

    public function getReviewsByProductId($locale, $productId, QueryParams $params = null)
    {
        $predicate = 'target(id = "' . $productId . '") and target(typeId="product")';
        $request = RequestBuilder::of()->reviews()->query()->where($predicate)->sort('createdAt desc');

        return $this->executeRequest($request, $locale, $params);
    }

    public function createReviewForProduct($locale, $productReference, $customerReference, $text, $rating)
    {
        $reviewDraft = ReviewDraft::of()
            ->setText($text)
            ->setRating($rating)
            ->setTarget($productReference)
            ->setLocale($locale);
        if (!is_null($customerReference)) {
            $reviewDraft->setCustomer($customerReference);
        }
        $request = RequestBuilder::of()->reviews()->create($reviewDraft);

        return $this->executeRequest($request, $locale);
    }

    public function update(Review $review, array $actions, QueryParams $params = null)
    {
        $client = $this->getClient();
        $request = RequestBuilder::of()->reviews()->update($review)->setActions($actions);

        if(!is_null($params)){
            foreach ($params->getParams() as $param) {
                $request->addParamObject($param);
            }
        }

        $response = $request->executeWithClient($client);
        $review = $request->mapFromResponse($response);

        return $review;

    }
}<commit_msg>WIP: review_bundle: type safe create review fnc at repository<commit_after><?php
/**
 */

namespace Commercetools\Symfony\ReviewBundle\Model\Repository;

use Commercetools\Core\Builder\Request\RequestBuilder;
use Commercetools\Core\Model\Customer\CustomerReference;
use Commercetools\Core\Model\Review\Review;
use Commercetools\Core\Model\Review\ReviewDraft;
use Commercetools\Symfony\CtpBundle\Model\QueryParams;
use Commercetools\Symfony\CtpBundle\Service\MapperFactory;
use Commercetools\Symfony\CtpBundle\Model\Repository;
use Commercetools\Core\Client;
use Psr\Cache\CacheItemPoolInterface;

class ReviewRepository extends Repository
{
    public function __construct(
        $enableCache,
        CacheItemPoolInterface $cache,
        Client $client,
        MapperFactory $mapperFactory
    ) {
        parent::__construct($enableCache, $cache, $client, $mapperFactory);
    }

    public function getReviewById($locale, $reviewId, QueryParams $params = null)
    {
        $request = RequestBuilder::of()->reviews()->getById($reviewId);

        return $this->executeRequest($request, $locale, $params);
    }

    public function getReviewsByProductId($locale, $productId, QueryParams $params = null)
    {
        $predicate = 'target(id = "' . $productId . '") and target(typeId="product")';
        $request = RequestBuilder::of()->reviews()->query()->where($predicate)->sort('createdAt desc');

        return $this->executeRequest($request, $locale, $params);
    }

    public function createReviewForProduct($locale, $productReference, CustomerReference $customerReference = null, $text, $rating)
    {
        $reviewDraft = ReviewDraft::of()
            ->setText($text)
            ->setRating($rating)
            ->setTarget($productReference)
            ->setLocale($locale);

        if (!is_null($customerReference)) {
            $reviewDraft->setCustomer($customerReference);
        }

        $request = RequestBuilder::of()->reviews()->create($reviewDraft);

        return $this->executeRequest($request, $locale);
    }

    public function update(Review $review, array $actions, QueryParams $params = null)
    {
        $client = $this->getClient();
        $request = RequestBuilder::of()->reviews()->update($review)->setActions($actions);

        if(!is_null($params)){
            foreach ($params->getParams() as $param) {
                $request->addParamObject($param);
            }
        }

        $response = $request->executeWithClient($client);
        $review = $request->mapFromResponse($response);

        return $review;

    }
}
{'fraction_non_alphanumeric': 0.1380952380952381, 'fraction_numerical': 0.0, 'mean_word_length': 3.4064685314685317, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4819547', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1394, 'n_words': 242}