Completed
Push — master ( 6dd7fd...f4371c )
by Eric
15:39 queued 08:32
created

TranslatableRepository::getLocales()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
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\ORM;
13
14
use Doctrine\ORM\EntityManager;
15
use Doctrine\ORM\Mapping\ClassMetadata;
16
use Doctrine\ORM\Query\Expr\Join;
17
use Doctrine\ORM\QueryBuilder;
18
use Lug\Component\Resource\Model\ResourceInterface;
19
use Lug\Component\Resource\Repository\Doctrine\ORM\Repository;
20
use Lug\Component\Translation\Context\LocaleContextInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class TranslatableRepository extends Repository
26
{
27
    /**
28
     * @var LocaleContextInterface
29
     */
30
    private $localeContext;
31
32
    /**
33
     * @var string[]
34
     */
35
    private $cache;
36
37
    /**
38
     * @param EntityManager          $em
39
     * @param ClassMetadata          $class
40
     * @param ResourceInterface      $resource
41
     * @param LocaleContextInterface $localeContext
42
     */
43 4
    public function __construct(
44
        $em,
45
        ClassMetadata $class,
46
        ResourceInterface $resource,
47
        LocaleContextInterface $localeContext
48
    ) {
49 4
        parent::__construct($em, $class, $resource);
50
51 4
        $this->localeContext = $localeContext;
52 4
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function createQueryBuilder($alias = null, $indexBy = null)
58
    {
59 2
        $queryBuilder = parent::createQueryBuilder($alias, $indexBy);
60
        $queryBuilder
61 2
            ->addSelect($alias = $this->getTranslationAlias($queryBuilder))
62 2
            ->leftJoin($this->getProperty('translations', $queryBuilder), $alias);
63
64 2
        return $queryBuilder;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    public function createQueryBuilderForCollection($alias = null, $indexBy = null)
71
    {
72 1
        $queryBuilder = parent::createQueryBuilder($alias, $indexBy);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (createQueryBuilder() instead of createQueryBuilderForCollection()). Are you sure this is correct? If so, you might want to change this to $this->createQueryBuilder().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
73
        $queryBuilder
74 1
            ->addSelect($alias = $this->getTranslationAlias($queryBuilder))
75 1
            ->innerJoin(
76 1
                $this->getProperty('translations', $queryBuilder),
77 1
                $alias,
78 1
                Join::WITH,
79 1
                $queryBuilder->expr()->in($this->getProperty('locale', $queryBuilder), $this->getLocales())
80 1
            );
81
82 1
        return $queryBuilder;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 3 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...
89
    {
90 3
        if ($this->cache === null) {
91 3
            $translationMetadata = $this
92 3
                ->getEntityManager()
93 3
                ->getClassMetadata($this->getClassMetadata()->getAssociationMapping('translations')['targetEntity']);
94
95 3
            $this->cache = array_diff(
0 ignored issues
show
Documentation Bug introduced by
It seems like array_diff($this->getPro...s->getClassMetadata())) 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...
96 3
                $this->getProperties($translationMetadata),
97 3
                $this->getProperties($this->getClassMetadata())
98 3
            );
99 3
        }
100
101 3
        if (in_array($this->getRootProperty($property), $this->cache, true)) {
102 1
            $root = $this->getTranslationAlias($root);
103 1
        }
104
105 3
        return parent::getProperty($property, $root);
106
    }
107
108
    /**
109
     * @param QueryBuilder|string|null $root
110
     *
111
     * @return string
112
     */
113 3
    private function getTranslationAlias($root)
114
    {
115 3
        return $this->getRootAlias($root).'_translation';
116
    }
117
118
    /**
119
     * @param QueryBuilder|string|null $root
120
     *
121
     * @return string
122
     */
123 3
    private function getRootAlias($root)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
124
    {
125 3
        if ($root instanceof QueryBuilder) {
126 3
            $root = $root->getRootAliases()[0];
127 3
        }
128
129 3
        return (string) $root;
130
    }
131
132
    /**
133
     * @param ClassMetadata $metadata
134
     *
135
     * @return string[]
136
     */
137 3
    private function getProperties(ClassMetadata $metadata)
138
    {
139 3
        return array_merge(
140 3
            $metadata->getFieldNames(),
141 3
            array_keys($metadata->embeddedClasses)
142 3
        );
143
    }
144
145
    /**
146
     * @param string $property
147
     *
148
     * @return string
149
     */
150 3
    private function getRootProperty($property)
151
    {
152 3
        return ($pos = strpos($property, '.')) !== false ? substr($property, 0, $pos) : $property;
153
    }
154
155
    /**
156
     * @return string[]
157
     */
158 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...
159
    {
160 1
        $locales = $this->localeContext->getLocales();
161
162 1
        if (($fallbackLocale = $this->localeContext->getFallbackLocale()) !== null) {
163 1
            $locales[] = $fallbackLocale;
164 1
        }
165
166 1
        return $locales;
167
    }
168
}
169