<commit_before><?php

namespace CultuurNet\UDB3\Offer\Security;

use CultuurNet\UDB3\Role\ReadModel\Constraints\UserConstraintsReadRepositoryInterface;
use CultuurNet\UDB3\Role\ValueObjects\Permission;
use CultuurNet\UDB3\SearchAPI2\ResultSetPullParser;
use CultuurNet\UDB3\SearchAPI2\SearchServiceInterface;
use ValueObjects\String\String as StringLiteral;

class UserPermissionMatcher implements UserPermissionMatcherInterface
{
    /**
     * @var UserConstraintsReadRepositoryInterface
     */
    private $userConstraintsReadRepository;

    /**
     * @var SearchQueryFactoryInterface
     */
    private $searchQueryFactory;

    /**
     * @var SearchServiceInterface
     */
    private $searchService;

    /**
     * @var ResultSetPullParser
     */
    private $resultSetPullParser;

    /**
     * ConstraintsOfferFilter constructor.
     * @param UserConstraintsReadRepositoryInterface $userConstraintsReadRepository
     * @param SearchQueryFactoryInterface $searchQueryFactory
     * @param SearchServiceInterface $searchService
     * @param ResultSetPullParser $resultSetPullParser
     */
    public function __construct(
        UserConstraintsReadRepositoryInterface $userConstraintsReadRepository,
        SearchQueryFactoryInterface $searchQueryFactory,
        SearchServiceInterface $searchService,
        ResultSetPullParser $resultSetPullParser
    ) {
        $this->userConstraintsReadRepository = $userConstraintsReadRepository;
        $this->searchQueryFactory = $searchQueryFactory;
        $this->searchService = $searchService;
        $this->resultSetPullParser = $resultSetPullParser;
    }

    /**
     * @inheritdoc
     */
    public function itMatchesOffer(
        StringLiteral $userId,
        Permission $permission,
        StringLiteral $offerId
    ) {
        $constraints = $this->userConstraintsReadRepository->getByUserAndPermission(
            $userId,
            $permission
        );
        if (count($constraints) < 1) {
            return false;
        }

        $query = $this->searchQueryFactory->createFromConstraints(
            $constraints,
            $offerId
        );
        $response = $this->searchService->search([$query]);
        if ($response->getStatusCode() != 200) {
            return false;
        }

        $result = $this->resultSetPullParser->getResultSet($response->getBody());
        return ($result->getTotalItems()->toNative() === 1);
    }
}<commit_msg>III-1196 Hotfix: Group was needed as query parameter to get a single result.<commit_after><?php

namespace CultuurNet\UDB3\Offer\Security;

use CultuurNet\Search\Parameter\Group;
use CultuurNet\UDB3\Role\ReadModel\Constraints\UserConstraintsReadRepositoryInterface;
use CultuurNet\UDB3\Role\ValueObjects\Permission;
use CultuurNet\UDB3\SearchAPI2\ResultSetPullParser;
use CultuurNet\UDB3\SearchAPI2\SearchServiceInterface;
use ValueObjects\String\String as StringLiteral;

class UserPermissionMatcher implements UserPermissionMatcherInterface
{
    /**
     * @var UserConstraintsReadRepositoryInterface
     */
    private $userConstraintsReadRepository;

    /**
     * @var SearchQueryFactoryInterface
     */
    private $searchQueryFactory;

    /**
     * @var SearchServiceInterface
     */
    private $searchService;

    /**
     * @var ResultSetPullParser
     */
    private $resultSetPullParser;

    /**
     * ConstraintsOfferFilter constructor.
     * @param UserConstraintsReadRepositoryInterface $userConstraintsReadRepository
     * @param SearchQueryFactoryInterface $searchQueryFactory
     * @param SearchServiceInterface $searchService
     * @param ResultSetPullParser $resultSetPullParser
     */
    public function __construct(
        UserConstraintsReadRepositoryInterface $userConstraintsReadRepository,
        SearchQueryFactoryInterface $searchQueryFactory,
        SearchServiceInterface $searchService,
        ResultSetPullParser $resultSetPullParser
    ) {
        $this->userConstraintsReadRepository = $userConstraintsReadRepository;
        $this->searchQueryFactory = $searchQueryFactory;
        $this->searchService = $searchService;
        $this->resultSetPullParser = $resultSetPullParser;
    }

    /**
     * @inheritdoc
     */
    public function itMatchesOffer(
        StringLiteral $userId,
        Permission $permission,
        StringLiteral $offerId
    ) {
        $constraints = $this->userConstraintsReadRepository->getByUserAndPermission(
            $userId,
            $permission
        );
        if (count($constraints) < 1) {
            return false;
        }

        $query = $this->searchQueryFactory->createFromConstraints(
            $constraints,
            $offerId
        );
        $response = $this->searchService->search([$query, new Group(true)]);
        if ($response->getStatusCode() != 200) {
            return false;
        }

        $result = $this->resultSetPullParser->getResultSet($response->getBody());
        return ($result->getTotalItems()->toNative() === 1);
    }
}
{'fraction_non_alphanumeric': 0.09313432835820895, 'fraction_numerical': 0.005572139303482587, 'mean_word_length': 3.1129296235679216, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, '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': '7049450', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1283, 'n_words': 222}