Completed
Push — master ( c891e1...8fa5d0 )
by Beniamin
06:55
created

UnderQuery::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
c 1
b 0
f 0
rs 9.4285
ccs 4
cts 4
cp 1
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * This file is part of UnderQuery package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\UnderQuery;
13
14
use Interop\Container\ContainerInterface;
15
use Phuria\UnderQuery\Connection\ConnectionInterface;
16
use Phuria\UnderQuery\DependencyInjection\ContainerFactory;
17
use Phuria\UnderQuery\QueryBuilder\DeleteBuilder;
18
use Phuria\UnderQuery\QueryBuilder\InsertBuilder;
19
use Phuria\UnderQuery\QueryBuilder\InsertSelectBuilder;
20
use Phuria\UnderQuery\QueryBuilder\QueryBuilderFacade;
21
use Phuria\UnderQuery\QueryBuilder\SelectBuilder;
22
use Phuria\UnderQuery\QueryBuilder\UpdateBuilder;
23
use Phuria\UnderQuery\QueryCompiler\QueryCompilerInterface;
24
25
/**
26
 * @author Beniamin Jonatan Šimko <[email protected]>
27
 */
28
class UnderQuery
29
{
30
    /**
31
     * @var ContainerInterface $container
32
     */
33
    private $container;
34
35
    /**
36
     * @param ConnectionInterface|null $connection
37
     * @param ContainerInterface|null  $container
38
     */
39 2
    public function __construct(ConnectionInterface $connection = null, ContainerInterface $container = null)
40
    {
41 2
        $this->container = $container ?: (new ContainerFactory())->create();
42 2
        $this->connection = $connection;
0 ignored issues
show
Bug introduced by
The property connection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43 2
    }
44
45
    /**
46
     * @return ContainerInterface
47
     */
48 2
    public function getContainer()
49
    {
50 2
        return $this->container;
51
    }
52
53
    /**
54
     * @param string $class
55
     *
56
     * @return QueryCompilerInterface
57
     */
58 1
    private function createQueryBuilder($class)
59
    {
60 1
        return new $class(new QueryBuilderFacade(
61 1
            $this->getContainer()->get('phuria.under_query.table_factory'),
62 1
            $this->getContainer()->get('phuria.under_query.query_compiler'),
63 1
            $this->connection
64 1
        ));
65
    }
66
67
    /**
68
     * @return SelectBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
69
     */
70 1
    public function createSelect()
71
    {
72 1
        return $this->createQueryBuilder(SelectBuilder::class);
73
    }
74
75
    /**
76
     * @return UpdateBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
77
     */
78 1
    public function createUpdate()
79
    {
80 1
        return $this->createQueryBuilder(UpdateBuilder::class);
81
    }
82
83
    /**
84
     * @return DeleteBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
85
     */
86 1
    public function createDelete()
87
    {
88 1
        return $this->createQueryBuilder(DeleteBuilder::class);
89
    }
90
91
    /**
92
     * @return InsertBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
93
     */
94 1
    public function createInsert()
95
    {
96 1
        return $this->createQueryBuilder(InsertBuilder::class);
97
    }
98
99
    /**
100
     * @return InsertSelectBuilder
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryCompilerInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
101
     */
102 1
    public function createInsertSelect()
103
    {
104 1
        return $this->createQueryBuilder(InsertSelectBuilder::class);
105
    }
106
}