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

TranslatableRepository::getProperty()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 17
loc 17
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 10
nc 4
nop 2
crap 3
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
            $translatableFields = $this->getClassMetadata()->getFieldNames();
92 3
            $translationFields = $this->getEntityManager()
93 3
                ->getClassMetadata($this->getClassMetadata()->getAssociationMapping('translations')['targetEntity'])
94 3
                ->getFieldNames();
95
96 3
            $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...
97 3
        }
98
99 3
        if (in_array($property, $this->cache, true)) {
100 1
            $root = $this->getTranslationAlias($root);
101 1
        }
102
103 3
        return parent::getProperty($property, $root);
104
    }
105
106
    /**
107
     * @param QueryBuilder|string|null $root
108
     *
109
     * @return string
110
     */
111 3
    private function getTranslationAlias($root)
112
    {
113 3
        return $this->getRootAlias($root).'_translation';
114
    }
115
116
    /**
117
     * @param QueryBuilder|string|null $root
118
     *
119
     * @return string
120
     */
121 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...
122
    {
123 3
        if ($root instanceof QueryBuilder) {
124 3
            $root = $root->getRootAliases()[0];
125 3
        }
126
127 3
        return (string) $root;
128
    }
129
130
    /**
131
     * @return string[]
132
     */
133 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...
134
    {
135 1
        $locales = $this->localeContext->getLocales();
136
137 1
        if (($fallbackLocale = $this->localeContext->getFallbackLocale()) !== null) {
138 1
            $locales[] = $fallbackLocale;
139 1
        }
140
141 1
        return $locales;
142
    }
143
}
144