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

ReplaceRoot::convertExpression()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.2
cc 4
eloc 7
nc 3
nop 1
crap 4.074
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 ReplaceRoot extends BaseStage\ReplaceRoot
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 6
    public function __construct(Builder $builder, DocumentManager $documentManager, ClassMetadata $class, $expression = null)
24
    {
25 6
        $this->dm = $documentManager;
26 6
        $this->class = $class;
27
28 6
        parent::__construct($builder, $expression);
29 6
    }
30
31 3
    protected function convertExpression($expression)
32
    {
33 3
        if (is_array($expression)) {
34
            return array_map([$this, 'convertExpression'], $expression);
35 3
        } elseif (is_string($expression) && substr($expression, 0, 1) === '$') {
36 2
            return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
37
        } else {
38 1
            return Type::convertPHPToDatabaseValue(parent::convertExpression($expression));
39
        }
40
    }
41
42
    /**
43
     * @return \Doctrine\ODM\MongoDB\Persisters\DocumentPersister
44
     */
45 2
    private function getDocumentPersister()
46
    {
47 2
        return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
48
    }
49
}
50