Passed
Push — master ( 68f2b4...691f88 )
by Pavel
03:56 queued 21s
created

CollectionPersister::getManyToManyCollection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 3.0067
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Persister;
4
5
use Bankiru\Api\Doctrine\ApiEntityManager;
6
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
7
use Bankiru\Api\Doctrine\Proxy\LazyCriteriaCollection;
8
use Bankiru\Api\Doctrine\Rpc\CrudsApiInterface;
9
use Doctrine\Common\Collections\Criteria;
10
use Doctrine\Common\Collections\Selectable;
11
12
final class CollectionPersister
13
{
14
    /**
15
     * @var array
16
     */
17
    private $association;
18
    /**
19
     * @var ApiMetadata
20
     */
21
    private $metadata;
22
    /**
23
     * @var Selectable
24
     */
25
    private $matcher;
26
27
    /**
28
     * CollectionPersister constructor.
29
     *
30
     * @param ApiMetadata $metadata
31
     * @param Selectable  $matcher
32
     * @param array       $association
33
     *
34
     * @internal param CrudsApiInterface $api
35
     */
36 1
    public function __construct(ApiMetadata $metadata, Selectable $matcher, array $association)
37
    {
38 1
        $this->association = $association;
39 1
        $this->metadata    = $metadata;
40 1
        $this->matcher     = $matcher;
41 1
    }
42
43 1
    public function getManyToManyCollection(array $identifiers)
44
    {
45 1
        if ($this->metadata->isIdentifierComposite()) {
46
            throw new \LogicException('Lazy loading entities with composite key is not supported');
47
        }
48
49 1
        $identifierFields = $this->metadata->getIdentifierFieldNames();
50 1
        $identifierField  = array_shift($identifierFields);
51
52 1
        $criteria = new Criteria();
53 1
        $criteria->andWhere(Criteria::expr()->in($identifierField, array_values($identifiers)));
54
55 1
        if (null !== $this->association['orderBy']) {
56 1
            $criteria->orderBy($this->association['orderBy']);
57 1
        }
58
59 1
        return new LazyCriteriaCollection($this->matcher, $criteria);
60
    }
61
}
62