Completed
Push — master ( 74c3c7...591b88 )
by Eric
08:09
created

createQueryBuilderForCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Translation\Repository\Doctrine\MongoDB;
13
14
use Doctrine\ODM\MongoDB\DocumentManager;
15
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
16
use Doctrine\ODM\MongoDB\UnitOfWork;
17
use Lug\Component\Resource\Model\ResourceInterface;
18
use Lug\Component\Resource\Repository\Doctrine\MongoDB\Repository;
19
use Lug\Component\Translation\Context\LocaleContextInterface;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class TranslatableRepository extends Repository
25
{
26
    /**
27
     * @var LocaleContextInterface
28
     */
29
    private $localeContext;
30
31
    /**
32
     * @var string[]
33
     */
34
    private $cache;
35
36
    /**
37
     * @param DocumentManager        $dm
38
     * @param UnitOfWork             $uow
39
     * @param ClassMetadata          $class
40
     * @param ResourceInterface      $resource
41
     * @param LocaleContextInterface $localeContext
42
     */
43 2
    public function __construct(
44
        DocumentManager $dm,
45
        UnitOfWork $uow,
46
        ClassMetadata $class,
47
        ResourceInterface $resource,
48
        LocaleContextInterface $localeContext
49
    ) {
50 2
        parent::__construct($dm, $uow, $class, $resource);
51
52 2
        $this->localeContext = $localeContext;
53 2
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function createQueryBuilderForCollection()
59
    {
60 1
        return parent::createQueryBuilderForCollection()->field($this->getProperty('locale'))->in($this->getLocales());
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1 View Code Duplication
    public function getProperty($property, $root = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68 1
        if ($this->cache === null) {
69 1
            $translatableFields = $this->getClassMetadata()->getFieldNames();
70 1
            $translationFields = $this->getDocumentManager()
71 1
                ->getClassMetadata($this->getClassMetadata()->getAssociationTargetClass($this->getTranslationAlias()))
72 1
                ->getFieldNames();
73
74 1
            $this->cache = array_diff($translationFields, $translatableFields);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_diff($translationF...s, $translatableFields) of type array<integer,integer|string> is incompatible with the declared type array<integer,string> of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75 1
        }
76
77 1
        if (in_array($property, $this->cache, true)) {
78 1
            $root = $this->getTranslationAlias();
79 1
        }
80
81 1
        return parent::getProperty($property, $root);
82
    }
83
84
    /**
85
     * @return string
86
     */
87 1
    private function getTranslationAlias()
88
    {
89 1
        return 'translations';
90
    }
91
92
    /**
93
     * @return string[]
94
     */
95 1 View Code Duplication
    private function getLocales()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97 1
        $locales = $this->localeContext->getLocales();
98
99 1
        if (($fallbackLocale = $this->localeContext->getFallbackLocale()) !== null) {
100 1
            $locales[] = $fallbackLocale;
101 1
        }
102
103 1
        return $locales;
104
    }
105
}
106