Completed
Pull Request — master (#397)
by Kristof
03:55
created

Sapi3UserPermissionMatcher::itMatchesOffer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
namespace CultuurNet\UDB3\Offer\Security;
4
5
use CultuurNet\UDB3\Role\ReadModel\Constraints\UserConstraintsReadRepositoryInterface;
6
use CultuurNet\UDB3\Role\ValueObjects\Permission;
7
use CultuurNet\UDB3\Search\SearchServiceInterface;
8
use ValueObjects\StringLiteral\StringLiteral;
9
10
/**
11
 * Implementation of the user permission matcher for SAPI3.
12
 */
13
class Sapi3UserPermissionMatcher implements UserPermissionMatcherInterface
14
{
15
    /**
16
     * @var UserConstraintsReadRepositoryInterface
17
     */
18
    private $userConstraintsReadRepository;
19
20
    /**
21
     * @var SearchQueryFactoryInterface
22
     */
23
    private $searchQueryFactory;
24
25
    /**
26
     * @var SearchServiceInterface
27
     */
28
    private $searchService;
29
30
    /**
31
     * ConstraintsOfferFilter constructor.
32
     * @param UserConstraintsReadRepositoryInterface $userConstraintsReadRepository
33
     * @param SearchQueryFactoryInterface $searchQueryFactory
34
     * @param SearchServiceInterface $searchService
35
     */
36
    public function __construct(
37
        UserConstraintsReadRepositoryInterface $userConstraintsReadRepository,
38
        SearchQueryFactoryInterface $searchQueryFactory,
39
        SearchServiceInterface $searchService
40
    ) {
41
        $this->userConstraintsReadRepository = $userConstraintsReadRepository;
42
        $this->searchQueryFactory = $searchQueryFactory;
43
        $this->searchService = $searchService;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function itMatchesOffer(
50
        StringLiteral $userId,
51
        Permission $permission,
52
        StringLiteral $offerId
53
    ) {
54
        $constraints = $this->userConstraintsReadRepository->getByUserAndPermission(
55
            $userId,
56
            $permission
57
        );
58
        if (count($constraints) < 1) {
59
            return false;
60
        }
61
62
        $query = $this->searchQueryFactory->createFromConstraints(
63
            $constraints,
64
            $offerId
65
        );
66
67
        $results = $this->searchService->search($query->getValue());
68
69
        return ($results->getTotalItems()->toNative() === 1);
70
    }
71
}
72