Passed
Pull Request — master (#13)
by Vincent
07:15
created

AbstractSchemaManager::transaction()   A

Complexity

Conditions 3
Paths 21

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0146

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 25
ccs 15
cts 17
cp 0.8824
rs 9.7
cc 3
nc 21
nop 1
crap 3.0146
1
<?php
2
3
namespace Bdf\Prime\Schema;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Exception\DBALException;
7
use Bdf\Prime\Exception\PrimeException;
8
use Bdf\Prime\Platform\PlatformInterface;
9
use Bdf\Prime\Schema\Builder\TableBuilder;
10
use Bdf\Prime\Schema\Builder\TypesHelperTableBuilder;
11
use Doctrine\DBAL\DBALException as DoctrineDBALException;
12
13
/**
14
 * Class AbstractSchemaManager
15
 */
16
abstract class AbstractSchemaManager implements SchemaManagerInterface
17
{
18
    /**
19
     * The database connection instance.
20
     *
21
     * @var ConnectionInterface
22
     */
23
    protected $connection;
24
25
    /**
26
     * The connection platform.
27
     *
28
     * @var PlatformInterface
29
     */
30
    protected $platform;
31
32
    /**
33
     * The use drop flag. Allows builder to use drop command on diff
34
     *
35
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
36
     */
37
    protected $useDrop = true;
38
39
    /**
40
     * The auto flush flag. Allows builder execute query
41
     *
42
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
43
     * @internal
0 ignored issues
show
Coding Style Documentation introduced by
@internal tag is not allowed in member variable comment
Loading history...
44
     */
45
    protected $autoFlush = true;
46
47
48
    /**
49
     * Create a new schema builder.
50
     *
51
     * @param ConnectionInterface $connection
52
     * @throws PrimeException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
introduced by
Comment missing for @throws tag in function comment
Loading history...
53
     */
54 166
    public function __construct(ConnectionInterface $connection)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
55
    {
56 166
        $this->setConnection($connection);
57 166
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function getConnection()
63
    {
64 1
        return $this->connection;
65
    }
66
67
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connection should have a doc-comment as per coding-style.
Loading history...
68
     * {@inheritdoc}
69
     */
70 166
    public function setConnection(ConnectionInterface $connection)
71
    {
72 166
        $this->connection = $connection;
73 166
        $this->platform   = $connection->platform();
74
75 166
        return $this;
76
    }
77
78
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $flag should have a doc-comment as per coding-style.
Loading history...
79
     * {@inheritdoc}
80
     */
81 585
    public function useDrop($flag = true)
82
    {
83 585
        $this->useDrop = $flag;
84
85 585
        return $this;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function getUseDrop()
92
    {
93 1
        return $this->useDrop;
94
    }
95
96
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $tableName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $callback should have a doc-comment as per coding-style.
Loading history...
97
     * {@inheritdoc}
98
     */
99 207
    public function table($tableName, callable $callback)
100
    {
101 207
        $table = new TypesHelperTableBuilder(
102 207
            new TableBuilder($tableName),
103 207
            $this->platform->types()
104
        );
105
        
106 207
        $callback($table);
107
108 207
        return $this->add($table->build());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->add($table->build()) returns the type Bdf\Prime\Schema\Manager\QueryManagerInterface which is incompatible with the return type mandated by Bdf\Prime\Schema\Manager...nagerInterface::table() of Bdf\Prime\Schema\Manager\TableManagerInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
109
    }
110
111
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $table should have a doc-comment as per coding-style.
Loading history...
112
     * {@inheritdoc}
113
     */
114 785
    public function add(TableInterface $table)
115
    {
116 785
        if ($this->hasTable($table->name())) {
117 83
            return $this->push(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->push($this...Table($table->name()))) returns the type Bdf\Prime\Schema\Manager\QueryManagerInterface which is incompatible with the return type mandated by Bdf\Prime\Schema\Manager...ManagerInterface::add() of Bdf\Prime\Schema\Manager\TableManagerInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
118 83
                $this->diff($table, $this->loadTable($table->name()))
119
            );
120
        }
121
122 744
        return $this->push($this->schema($table));
0 ignored issues
show
Deprecated Code introduced by
The function Bdf\Prime\Schema\SchemaManagerInterface::schema() has been deprecated: since 1.3 Doctrine schema should be used only internally ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

122
        return $this->push(/** @scrutinizer ignore-deprecated */ $this->schema($table));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug Best Practice introduced by
The expression return $this->push($this->schema($table)) returns the type Bdf\Prime\Schema\Manager\QueryManagerInterface which is incompatible with the return type mandated by Bdf\Prime\Schema\Manager...ManagerInterface::add() of Bdf\Prime\Schema\Manager\TableManagerInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
123
    }
124
125
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $tableName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $callback should have a doc-comment as per coding-style.
Loading history...
126
     * {@inheritdoc}
127
     */
128 2
    public function change($tableName, callable $callback)
129
    {
130 2
        $table = $this->loadTable($tableName);
131 2
        $builder = TableBuilder::fromTable($table);
132
133 2
        $callback(
134 2
            new TypesHelperTableBuilder(
135 2
                $builder,
136 2
                $this->platform->types()
137
            )
138
        );
139
140 2
        return $this->push(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->push($this...lder->build(), $table)) returns the type Bdf\Prime\Schema\Manager\QueryManagerInterface which is incompatible with the return type mandated by Bdf\Prime\Schema\Manager...agerInterface::change() of Bdf\Prime\Schema\Manager\TableManagerInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
141 2
            $this->diff($builder->build(), $table)
142
        );
143
    }
144
145
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $operations should have a doc-comment as per coding-style.
Loading history...
146
     * {@inheritdoc}
147
     */
148 15
    public function simulate(\Closure $operations = null)
149
    {
150 15
        $newSchema = clone $this;
151 15
        $newSchema->autoFlush = false;
152
153 15
        if ($operations !== null) {
154 3
            $operations($newSchema);
155
        }
156
157 15
        return $newSchema;
158
    }
159
160
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $operations should have a doc-comment as per coding-style.
Loading history...
161
     * {@inheritdoc}
162
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
163 2
    public function transaction(\Closure $operations)
164
    {
165 2
        $last = $this->autoFlush;
166 2
        $this->autoFlush = false;
167
168
        try {
169 2
            $operations($this);
170
171 2
            $this->connection->beginTransaction();
0 ignored issues
show
Bug introduced by
The method beginTransaction() does not exist on Bdf\Prime\Connection\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Connection\ConnectionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

171
            $this->connection->/** @scrutinizer ignore-call */ 
172
                               beginTransaction();
Loading history...
172 2
            $this->flush();
173 1
            $this->connection->commit();
0 ignored issues
show
Bug introduced by
The method commit() does not exist on Bdf\Prime\Connection\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Connection\ConnectionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

173
            $this->connection->/** @scrutinizer ignore-call */ 
174
                               commit();
Loading history...
174 1
        } catch (DoctrineDBALException $e) {
175
            $this->connection->rollBack();
0 ignored issues
show
Bug introduced by
The method rollBack() does not exist on Bdf\Prime\Connection\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Connection\ConnectionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
            $this->connection->/** @scrutinizer ignore-call */ 
176
                               rollBack();
Loading history...
176
177
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
178 1
        } catch (\Exception $e) {
179 1
            $this->connection->rollBack();
180
181 1
            throw $e;
182 1
        } finally {
183 2
            $this->autoFlush = $last;
184 2
            $this->clear();
185
        }
186
187 1
        return $this;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 4
    public function isBuffered()
194
    {
195 4
        return !$this->autoFlush;
196
    }
197
}
198