Completed
Pull Request — master (#1654)
by Andreas
08:52
created

BucketAuto::convertExpression()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 10
ccs 0
cts 6
cp 0
rs 9.2
cc 4
eloc 7
nc 3
nop 1
crap 20
1
<?php
2
3
namespace Doctrine\ODM\MongoDB\Aggregation\Stage;
4
5
use Doctrine\MongoDB\Aggregation\Builder;
6
use Doctrine\MongoDB\Aggregation\Stage as BaseStage;
7
use Doctrine\ODM\MongoDB\DocumentManager;
8
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
9
use Doctrine\ODM\MongoDB\Types\Type;
10
11 View Code Duplication
class BucketAuto extends BaseStage\BucketAuto
0 ignored issues
show
Duplication introduced by
This class 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...
12
{
13
    /**
14
     * @var DocumentManager
15
     */
16
    private $dm;
17
18
    /**
19
     * @var ClassMetadata
20
     */
21
    private $class;
22
23
    public function __construct(Builder $builder, DocumentManager $documentManager, ClassMetadata $class)
24
    {
25
        $this->dm = $documentManager;
26
        $this->class = $class;
27
28
        parent::__construct($builder);
29
    }
30
31
    protected function convertExpression($expression)
32
    {
33
        if (is_array($expression)) {
34
            return array_map([$this, 'convertExpression'], $expression);
35
        } elseif (is_string($expression) && substr($expression, 0, 1) === '$') {
36
            return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
37
        } else {
38
            return Type::convertPHPToDatabaseValue(parent::convertExpression($expression));
39
        }
40
    }
41
42
    /**
43
     * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister
44
     */
45
    private function getDocumentPersister()
46
    {
47
        return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
48
    }
49
}
50