GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — develop (#17)
by Aleh
04:13 queued 01:51
created

RdsActionInspector   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 147
Duplicated Lines 12.24 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 18
loc 147
ccs 67
cts 67
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A update() 0 30 5
A initDb() 0 16 2
A dropDb() 0 11 2
A getQueryBuilder() 0 4 1
A getSelectQuery() 0 8 1
A getUpdateQuery() 8 8 1
A getInsertQuery() 10 10 1
A getConnection() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Scheduler\ActionInspector;
4
5
use Scheduler\Action\ActionInterface;
6
use Scheduler\Exception\SchedulerException;
7
use Doctrine\DBAL\DBALException;
8
9
/**
10
 * Class RdsActionInspector
11
 * @package Scheduler\ActionInspector
12
 * @author Aleh Hutnikau, <[email protected]>
13
 */
14
class RdsActionInspector extends AbstractActionInspector
15
{
16
    const TABLE_NAME = 'scheduler_action_inspector';
17
    const COLUMN_ID = 'id';
18
    const COLUMN_REPORT = 'report';
19
    const COLUMN_STATE = 'state';
20
21
    /** @var \Doctrine\DBAL\Connection */
22
    private $connection;
23
24
    /**
25
     * RdsActionInspector constructor.
26
     * @param \Doctrine\DBAL\Connection $connection
27
     */
28 1
    public function __construct(\Doctrine\DBAL\Connection $connection)
29
    {
30 1
        $this->connection = $connection;
31 1
    }
32
33
    /**
34
     * @param ActionInterface $action
35
     * @return boolean returns `false` if action is already exists in this state in the log
36
     * @throws SchedulerException
37
     */
38 1
    public function update(ActionInterface $action)
39
    {
40 1
        $actionId = $action->getId();
41 1
        $actionState = $action->getState();
42 1
        $result = false;
43 1
        $previousState = null;
44
        try {
45 1
            $selectQb = $this->getSelectQuery($actionId);
46
47 1
            if ($dbResult = $selectQb->execute()->fetch(\PDO::FETCH_ASSOC)) {
48 1
                $previousState = $dbResult[self::COLUMN_STATE];
49 1
                $qb = $this->getUpdateQuery($actionState, $actionId);
50
            } else {
51 1
                $qb = $this->getInsertQuery($actionState, $actionId);
52
            }
53
54 1
            if ($actionState === ActionInterface::STATE_FINISHED) {
55 1
                $qb->set(self::COLUMN_REPORT, ':report')
56 1
                    ->setParameter(self::COLUMN_REPORT, $action->getReport());
57
            }
58
59 1
            if ($this->isStateAllowed($action, $previousState)) {
60 1
                $result = $qb->execute() === 1;
61
            }
62 1
        } catch (DBALException $e) {
63 1
            $result = false;
64
        }
65
66 1
        return $result;
67
    }
68
69
    /**
70
     * @return \Doctrine\DBAL\Query\QueryBuilder
71
     */
72 1
    protected function getQueryBuilder()
73
    {
74 1
        return $this->getConnection()->createQueryBuilder();
75
    }
76
77
    /**
78
     * @param $actionId
79
     * @return \Doctrine\DBAL\Query\QueryBuilder
80
     */
81 1
    protected function getSelectQuery($actionId)
82
    {
83 1
        return $this->getQueryBuilder()
84 1
            ->select(implode(',', [self::COLUMN_REPORT, self::COLUMN_STATE, self::COLUMN_ID]))
85 1
            ->from(self::TABLE_NAME)
86 1
            ->andWhere(self::COLUMN_ID . ' = ?')
87 1
            ->setParameter(0, $actionId);
88
    }
89
90
    /**
91
     * @param $actionState
92
     * @param $actionId
93
     * @return \Doctrine\DBAL\Query\QueryBuilder
94
     */
95 1 View Code Duplication
    protected function getUpdateQuery($actionState, $actionId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97 1
        return $this->getQueryBuilder()
98 1
            ->update(self::TABLE_NAME)
99 1
            ->set(self::COLUMN_STATE, ':state')
100 1
            ->where(self::COLUMN_ID . ' = :id')
101 1
            ->setParameters(['state' => $actionState, 'id' => $actionId]);
102
    }
103
104
    /**
105
     * @param $actionState
106
     * @param $actionId
107
     * @return \Doctrine\DBAL\Query\QueryBuilder
108
     */
109 1 View Code Duplication
    protected function getInsertQuery($actionState, $actionId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111 1
        return $this->getQueryBuilder()
112 1
            ->insert(self::TABLE_NAME)
113 1
            ->values([
114 1
                self::COLUMN_ID => ':id',
115 1
                self::COLUMN_STATE => ':state'
116
            ])
117 1
            ->setParameters(['id' => $actionId, 'state' => $actionState]);
118
    }
119
120
    /**
121
     * @return \Doctrine\DBAL\Connection
122
     */
123 1
    private function getConnection()
124
    {
125 1
        return $this->connection;
126
    }
127
128
    /**
129
     * @param \Doctrine\DBAL\Connection $connection
130
     * @throws \Doctrine\DBAL\DBALException
131
     */
132 3
    public static function initDb(\Doctrine\DBAL\Connection $connection)
133
    {
134 3
        $schemaManager = $connection->getSchemaManager();
135 3
        $schema = $schemaManager->createSchema();
136 3
        $fromSchema = clone $schema;
137 3
        $table = $schema->createTable(self::TABLE_NAME);
138 2
        $table->addColumn(static::COLUMN_ID, 'string', ['length' => 255, 'notnull' => true]);
139 2
        $table->addColumn(static::COLUMN_STATE, 'string', ['length' => 255, 'notnull' => true]);
140 2
        $table->addColumn(static::COLUMN_REPORT, 'text', ['notnull' => false]);
141 2
        $table->setPrimaryKey([static::COLUMN_ID]);
142 2
        $table->addIndex([static::COLUMN_ID], 'IDX_' . static::TABLE_NAME . '_' . static::COLUMN_ID);
143 2
        $queries = $fromSchema->getMigrateToSql($schema, $connection->getDatabasePlatform());
144 2
        foreach ($queries as $query) {
145 2
            $connection->exec($query);
146
        }
147 2
    }
148
149 2
    public static function dropDb(\Doctrine\DBAL\Connection $connection)
150
    {
151 2
        $schemaManager = $connection->getSchemaManager();
152 2
        $schema = $schemaManager->createSchema();
153 2
        $fromSchema = clone $schema;
154 2
        $schema->dropTable(self::TABLE_NAME);
155 2
        $queries = $fromSchema->getMigrateToSql($schema, $connection->getDatabasePlatform());
156 2
        foreach ($queries as $query) {
157 2
            $connection->exec($query);
158
        }
159
    }
160
}