Test Failed
Push — master ( b17eba...69b436 )
by Kirill
06:37
created

Hydrogen   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 48
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getProcessor() 0 8 2
A query() 0 9 2
A __get() 0 9 2
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\EntityRepository;
13
use RDS\Hydrogen\Processor\DatabaseProcessor;
14
use RDS\Hydrogen\Processor\ProcessorInterface;
15
16
/**
17
 * Trait Hydrogen
18
 * @property-read Query|$this $query
19
 */
20
trait Hydrogen
21
{
22
    /**
23
     * @var ProcessorInterface
24
     */
25
    private $processor;
26
27
    /**
28
     * @return ProcessorInterface
29
     */
30
    public function getProcessor(): ProcessorInterface
31
    {
32
        if ($this->processor === null) {
33
            $this->processor = new DatabaseProcessor($this, $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...
34
        }
35
36
        return $this->processor;
37
    }
38
39
    /**
40
     * @return Query|$this
41
     * @throws \LogicException
42
     */
43
    public function query(): Query
44
    {
45
        if (! $this instanceof EntityRepository) {
46
            $error = 'Could not use %s under non-repository class, but %s given';
47
            throw new \LogicException(\sprintf($error, Hydrogen::class, static::class));
48
        }
49
50
        return Query::new()->from($this);
51
    }
52
53
    /**
54
     * @param string $name
55
     * @return null|Query
56
     * @throws \LogicException
57
     */
58
    public function __get(string $name)
59
    {
60
        switch ($name) {
61
            case 'query':
62
                return $this->query();
63
        }
64
65
        return null;
66
    }
67
}
68