Completed
Pull Request — master (#28)
by
unknown
19:12 queued 09:24
created

Driver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 4
dl 0
loc 77
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 4 1
A constructPdoDsn() 0 12 4
A getDatabasePlatform() 0 4 1
A getSchemaManager() 0 4 1
A getName() 0 4 1
A getDatabase() 0 4 1
A createDatabasePlatformForVersion() 0 8 2
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
namespace Crate\DBAL\Driver\PDOCrate;
23
24
use Crate\DBAL\Platforms\Crate057Platform;
25
use Crate\DBAL\Platforms\CratePlatform;
26
use Doctrine\DBAL\VersionAwarePlatformDriver;
27
28
class Driver implements \Doctrine\DBAL\Driver, VersionAwarePlatformDriver
29
{
30
    const VERSION = '0.2.1';
31
    const NAME = 'crate';
32
    const SCHEMA_MIN_VERSION = '0.57.0';
33 6
34
    /**
35 6
     * {@inheritDoc}
36
     * @return PDOConnection The database connection.
37
     */
38
    public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
39
    {
40
        return new PDOConnection($this->constructPdoDsn($params), $username, $password, $driverOptions);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Crate\DBAL\D...sword, $driverOptions); (Crate\DBAL\Driver\PDOCrate\PDOConnection) is incompatible with the return type declared by the interface Doctrine\DBAL\Driver::connect of type Doctrine\DBAL\Driver\Connection.

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...
41
    }
42
43 6
    /**
44
     * Constructs the Crate PDO DSN.
45 6
     *
46 6
     * @return string The DSN.
47 6
     */
48 6
    private function constructPdoDsn(array $params)
49
    {
50
        $dsn = self::NAME . ':';
51 6
        if (isset($params['host']) && $params['host'] != '') {
52
            $dsn .= $params['host'];
53 6
        } else {
54 1
            $dsn .= 'localhost';
55
        }
56
        $dsn .= ':' . (isset($params['port']) ? $params['port'] : '4200');
57
58
        return $dsn;
59 6
    }
60
61 6
    /**
62
     * {@inheritDoc}
63
     */
64
    public function getDatabasePlatform()
65
    {
66
        return new \Crate\DBAL\Platforms\CratePlatform();
67 2
    }
68
69 2
    /**
70
     * {@inheritDoc}
71
     */
72
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
73
    {
74
        return new \Crate\DBAL\Schema\CrateSchemaManager($conn);
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function getName()
81
    {
82
        return self::NAME;
83 2
    }
84
85 2
    /**
86
     * {@inheritDoc}
87
     */
88
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
89
    {
90
        return null;
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function createDatabasePlatformForVersion($version)
97
    {
98
        if (version_compare($version, self::SCHEMA_MIN_VERSION, ">=")) {
99
            return new Crate057Platform();
100
        } else {
101
            return new CratePlatform();
102
        }
103
    }
104
}
105