Test Failed
Push — master ( 1c5165...b7ef6a )
by Kirill
03:37
created

Hydrogen::getEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen;
11
12
use Doctrine\ORM\EntityManager;
13
use Doctrine\ORM\EntityRepository;
14
use RDS\Hydrogen\Processor\DatabaseProcessor;
15
use RDS\Hydrogen\Processor\ProcessorInterface;
16
17
/**
18
 * Trait Hydrogen
19
 * @property-read Query|$this $query
20
 */
21
trait Hydrogen
22
{
23
    /**
24
     * @var ProcessorInterface
25
     */
26
    private $processor;
27
28
    /**
29
     * @return ProcessorInterface
30
     */
31 30
    public function getProcessor(): ProcessorInterface
32
    {
33 30
        if ($this->processor === null) {
34 30
            $this->processor = new DatabaseProcessor($this, $this->getEntityManager());
35
        }
36
37 30
        return $this->processor;
38
    }
39
40
    /**
41
     * @return EntityManager
42
     */
43 30
    public function getEntityManager(): EntityManager
44
    {
45 30
        return parent::getEntityManager();
46
    }
47
48
    /**
49
     * @return Query|$this
50
     * @throws \LogicException
51
     */
52 20
    public function query(): Query
53
    {
54 20
        if (! $this instanceof EntityRepository) {
55 4
            $error = 'Could not use %s under non-repository class, but %s given';
56 4
            throw new \LogicException(\sprintf($error, Hydrogen::class, static::class));
57
        }
58
59 16
        return Query::new()->from($this);
60
    }
61
62
    /**
63
     * @param string $name
64
     * @return null|Query
65
     * @throws \LogicException
66
     */
67 18
    public function __get(string $name)
68
    {
69
        switch ($name) {
70 18
            case 'query':
71 18
                return $this->query();
72
        }
73
74
        return null;
75
    }
76
}
77