Completed
Pull Request — master (#61)
by Eric
31:37
created

TranslatableRepositoryTrait::getRootAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 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\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\RepositoryTrait;
20
use Lug\Component\Translation\Context\LocaleContextInterface;
21
22
/**
23
 * WARNING - This trait should only be used with a class extending an EntityRepository.
24
 *
25
 * @author GeLo <[email protected]>
26
 */
27
trait TranslatableRepositoryTrait
28
{
29
    use RepositoryTrait {
30
        __construct as private constructFromRepositoryTrait;
31
        createQueryBuilder as private createQueryBuilderFromRepositoryTrait;
32
        getProperty as private getPropertyFromRepositoryTrait;
33
    }
34
35
    /**
36
     * @var LocaleContextInterface
37
     */
38
    private $localeContext;
39
40
    /**
41
     * @var string[]
42
     */
43
    private $cache;
44
45
    /**
46
     * @param EntityManager          $em
47
     * @param ClassMetadata          $class
48
     * @param ResourceInterface      $resource
49
     * @param LocaleContextInterface $localeContext
50
     */
51
    public function __construct(
52
        $em,
53
        ClassMetadata $class,
54
        ResourceInterface $resource,
55
        LocaleContextInterface $localeContext
56
    ) {
57
        $this->constructFromRepositoryTrait($em, $class, $resource);
0 ignored issues
show
Bug introduced by
It seems like constructFromRepositoryTrait() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
58
59
        $this->localeContext = $localeContext;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function createQueryBuilder($alias = null, $indexBy = null)
66
    {
67
        $queryBuilder = $this->createQueryBuilderFromRepositoryTrait($alias, $indexBy);
0 ignored issues
show
Bug introduced by
The method createQueryBuilderFromRepositoryTrait() does not exist on Lug\Component\Translatio...slatableRepositoryTrait. Did you maybe mean createQueryBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
        $queryBuilder
69
            ->addSelect($alias = $this->getTranslationAlias($queryBuilder))
70
            ->leftJoin($this->getProperty('translations', $queryBuilder), $alias);
71
72
        return $queryBuilder;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function createQueryBuilderForCollection($alias = null, $indexBy = null)
79
    {
80
        $queryBuilder = $this->createQueryBuilderFromRepositoryTrait($alias, $indexBy);
0 ignored issues
show
Bug introduced by
The method createQueryBuilderFromRepositoryTrait() does not exist on Lug\Component\Translatio...slatableRepositoryTrait. Did you maybe mean createQueryBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
81
        $queryBuilder
82
            ->addSelect($alias = $this->getTranslationAlias($queryBuilder))
83
            ->innerJoin(
84
                $this->getProperty('translations', $queryBuilder),
85
                $alias,
86
                Join::WITH,
87
                $queryBuilder->expr()->in($this->getProperty('locale', $queryBuilder), $this->getLocales())
88
            );
89
90
        return $queryBuilder;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getProperty($property, $root = null)
97
    {
98
        if ($this->cache === null) {
99
            $translatableFields = $this->getClassMetadata()->getFieldNames();
0 ignored issues
show
Bug introduced by
It seems like getClassMetadata() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
100
            $translationFields = $this->getEntityManager()
0 ignored issues
show
Bug introduced by
It seems like getEntityManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
101
                ->getClassMetadata($this->getClassMetadata()->getAssociationMapping('translations')['targetEntity'])
0 ignored issues
show
Bug introduced by
It seems like getClassMetadata() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
102
                ->getFieldNames();
103
104
            $this->cache = array_diff($translationFields, $translatableFields);
105
        }
106
107
        if (($pos = strpos($property, '.')) !== false) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
108
109
        }
110
111
        if (in_array($property, $this->cache, true)) {
112
            $root = $this->getTranslationAlias($root);
113
        }
114
115
        return $this->getPropertyFromRepositoryTrait($property, $root);
0 ignored issues
show
Bug introduced by
The method getPropertyFromRepositoryTrait() does not exist on Lug\Component\Translatio...slatableRepositoryTrait. Did you maybe mean getProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
116
    }
117
118
    /**
119
     * @param QueryBuilder|string|null $root
120
     *
121
     * @return string
122
     */
123
    private function getTranslationAlias($root)
124
    {
125
        return $this->getRootAlias($root).'_translation';
126
    }
127
128
    /**
129
     * @param QueryBuilder|string|null $root
130
     *
131
     * @return string
132
     */
133
    private function getRootAlias($root)
134
    {
135
        if ($root instanceof QueryBuilder) {
136
            $root = $root->getRootAliases()[0];
137
        }
138
139
        return (string) $root;
140
    }
141
142
    /**
143
     * @return string[]
144
     */
145 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...
146
    {
147
        $locales = $this->localeContext->getLocales();
148
149
        if (($fallbackLocale = $this->localeContext->getFallbackLocale()) !== null) {
150
            $locales[] = $fallbackLocale;
151
        }
152
153
        return $locales;
154
    }
155
}
156