Completed
Push — master ( 252438...82275d )
by Markus
03:16
created

DoctrineQueryStorageProvider::resolveQueries()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 7
Bugs 2 Features 0
Metric Value
c 7
b 2
f 0
dl 0
loc 33
ccs 0
cts 25
cp 0
rs 5.3846
cc 8
eloc 20
nc 6
nop 1
crap 72
1
<?php
2
namespace Mathielen\ImportEngine\Storage\Provider;
3
4
use Doctrine\ORM\EntityManagerInterface;
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\QueryBuilder;
7
use Mathielen\ImportEngine\Storage\Provider\Connection\ConnectionFactoryInterface;
8
use Mathielen\ImportEngine\ValueObject\StorageSelection;
9
use Mathielen\ImportEngine\Storage\DoctrineStorage;
10
11
class DoctrineQueryStorageProvider implements \IteratorAggregate, StorageProviderInterface
12
{
13
14
    /**
15
     *
16
     * @var ConnectionFactoryInterface
17
     */
18
    private $connectionFactory;
19
20
    /**
21
     * @var QueryBuilder[]
22
     */
23
    private $queries;
24
25
    public function __construct(ConnectionFactoryInterface $connectionFactory, $classNamesOrQueries)
26
    {
27
        $this->connectionFactory = $connectionFactory;
28
        $this->resolveQueries($classNamesOrQueries);
29
    }
30
31
    private function resolveQueries($classNamesOrQueries)
32
    {
33
        if ( !is_array( $classNamesOrQueries ) && !$classNamesOrQueries instanceof \Traversable ) {
34
            throw new \InvalidArgumentException("classNamesOrQueries must be an array or Traversable");
35
        }
36
37
        //TODO what about different entity providers ?
38
        /** @var EntityManagerInterface $entityManager */
39
        $entityManager = $this->connectionFactory->getConnection();
40
41
        $this->queries = array();
42
        foreach ($classNamesOrQueries as $k=>$classNameOrQuery) {
43
            if (is_string($classNameOrQuery) && class_exists($classNameOrQuery)) {
44
                $query = $entityManager->createQueryBuilder()
45
                    ->select('o')
46
                    ->from($classNameOrQuery, 'o')
47
                    ->getQuery();
48
                $selection = new StorageSelection($query, $classNameOrQuery, $classNameOrQuery);
49
50
            } elseif (is_string($classNameOrQuery)) {
51
                $query = $entityManager->createQuery($classNameOrQuery);
52
                $selection = new StorageSelection($query, $query->getDQL(), $query->getDQL());
53
54
            } elseif ($classNameOrQuery instanceof Query) {
55
                $selection = new StorageSelection($classNameOrQuery, $classNameOrQuery->getDQL(), $classNameOrQuery->getDQL());
56
57
            } else {
58
                throw new \InvalidArgumentException("Only strings or QueryBuilder are allowed!");
59
            }
60
61
            $this->queries[$k] = $selection;
62
        }
63
    }
64
65
    public function getIterator()
66
    {
67
        return new \ArrayIterator($this->queries);
68
    }
69
70
    /**
71
     * (non-PHPdoc)
72
     * @see \Mathielen\ImportEngine\Storage\Provider\StorageProviderInterface::storage()
73
     */
74
    public function storage(StorageSelection $selection)
75
    {
76
        $connection = $this->connectionFactory->getConnection($selection);
77
78
        return new DoctrineStorage($connection, $selection->getName(), $selection->getImpl());
79
    }
80
81
    /**
82
     * (non-PHPdoc)
83
     * @see \Mathielen\ImportEngine\Storage\Provider\StorageProviderInterface::select()
84
     */
85
    public function select($id = null)
86
    {
87
        if ($id instanceof StorageSelection) {
88
            return $id;
89
        } else {
90
            return $this->queries[$id];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queries[$id]; (Doctrine\ORM\QueryBuilder) is incompatible with the return type declared by the interface Mathielen\ImportEngine\S...oviderInterface::select of type Mathielen\ImportEngine\V...Object\StorageSelection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
91
        }
92
    }
93
94
}
95