Completed
Pull Request — master (#61)
by Eric
33:00 queued 25:59
created

TranslatableRepositoryTrait::getTranslationAlias()   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\RepositoryTrait;
19
use Lug\Component\Translation\Context\LocaleContextInterface;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
trait TranslatableRepositoryTrait
25
{
26
    use RepositoryTrait {
27
        __construct as private constructFromRepositoryTrait;
28
        createQueryBuilderForCollection as private createQueryBuilderForCollectionFromRepositoryTrait;
29
        getProperty as private getPropertyFromRepositoryTrait;
30
    }
31
32
    /**
33
     * @var LocaleContextInterface
34
     */
35
    private $localeContext;
36
37
    /**
38
     * @var string[]
39
     */
40
    private $cache;
41
42
    /**
43
     * @param DocumentManager        $dm
44
     * @param UnitOfWork             $uow
45
     * @param ClassMetadata          $class
46
     * @param ResourceInterface      $resource
47
     * @param LocaleContextInterface $localeContext
48
     */
49 3
    public function __construct(
50
        DocumentManager $dm,
51
        UnitOfWork $uow,
52
        ClassMetadata $class,
53
        ResourceInterface $resource,
54
        LocaleContextInterface $localeContext
55
    ) {
56 3
        $this->constructFromRepositoryTrait($dm, $uow, $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...
57
58 3
        $this->localeContext = $localeContext;
59 3
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1
    public function createQueryBuilderForCollection()
65
    {
66 1
        return $this->createQueryBuilderForCollectionFromRepositoryTrait()
0 ignored issues
show
Bug introduced by
The method createQueryBuilderForCol...onFromRepositoryTrait() does not exist on Lug\Component\Translatio...slatableRepositoryTrait. Did you maybe mean createQueryBuilderForCollection()?

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...
67 1
            ->field($this->getProperty('locale'))
68 1
            ->in($this->getLocales());
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 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...
75
    {
76 1
        if ($this->cache === null) {
77 1
            $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...
78 1
            $translationFields = $this->getDocumentManager()
0 ignored issues
show
Bug introduced by
It seems like getDocumentManager() 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...
79 1
                ->getClassMetadata($this->getClassMetadata()->getAssociationTargetClass($this->getTranslationAlias()))
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...
80 1
                ->getFieldNames();
81
82 1
            $this->cache = array_diff($translationFields, $translatableFields);
83 1
        }
84
85 1
        if (in_array($property, $this->cache, true)) {
86 1
            $root = $this->getTranslationAlias();
87 1
        }
88
89 1
        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...
90
    }
91
92
    /**
93
     * @return string
94
     */
95 1
    private function getTranslationAlias()
96
    {
97 1
        return 'translations';
98
    }
99
100
    /**
101
     * @return string[]
102
     */
103 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...
104
    {
105 1
        $locales = $this->localeContext->getLocales();
106
107 1
        if (($fallbackLocale = $this->localeContext->getFallbackLocale()) !== null) {
108 1
            $locales[] = $fallbackLocale;
109 1
        }
110
111 1
        return $locales;
112
    }
113
}
114