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
Push — feature/support_parallel_worke... ( 35022c...7864ef )
by Aleh
01:34
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 getSelectQuery() 0 8 1
A getUpdateQuery() 8 8 1
A getInsertQuery() 10 10 1
A getConnection() 0 4 1
A getQueryBuilder() 0 4 1
A initDb() 0 16 2
A dropDb() 0 11 2

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
     * @param $actionId
71
     * @return \Doctrine\DBAL\Query\QueryBuilder
72
     */
73 1
    private function getSelectQuery($actionId)
74
    {
75 1
        return $this->getQueryBuilder()
76 1
            ->select(implode(',', [self::COLUMN_REPORT, self::COLUMN_STATE, self::COLUMN_ID]))
77 1
            ->from(self::TABLE_NAME)
78 1
            ->andWhere(self::COLUMN_ID . ' = ?')
79 1
            ->setParameter(0, $actionId);
80
    }
81
82
    /**
83
     * @param $actionState
84
     * @param $actionId
85
     * @return \Doctrine\DBAL\Query\QueryBuilder
86
     */
87 1 View Code Duplication
    private 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...
88
    {
89 1
        return $this->getQueryBuilder()
90 1
            ->update(self::TABLE_NAME)
91 1
            ->set(self::COLUMN_STATE, ':state')
92 1
            ->where(self::COLUMN_ID . ' = :id')
93 1
            ->setParameters(['state' => $actionState, 'id' => $actionId]);
94 1
    }
95
96
    /**
97
     * @param $actionState
98
     * @param $actionId
99
     * @return \Doctrine\DBAL\Query\QueryBuilder
100
     */
101 View Code Duplication
    private 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...
102 1
    {
103
        return $this->getQueryBuilder()
104 1
            ->insert(self::TABLE_NAME)
105 1
            ->values([
106 1
                self::COLUMN_ID => ':id',
107 1
                self::COLUMN_STATE => ':state'
108 1
            ])
109 1
            ->setParameters(['id' => $actionId, 'state' => $actionState]);
110
    }
111 1
112
    /**
113
     * @return \Doctrine\DBAL\Connection
114
     */
115
    private function getConnection()
116
    {
117 1
        return $this->connection;
118
    }
119 1
120
    /**
121
     * @return \Doctrine\DBAL\Query\QueryBuilder
122
     */
123
    private function getQueryBuilder()
124
    {
125
        return $this->getConnection()->createQueryBuilder();
126 3
    }
127
128 3
    /**
129 3
     * @param \Doctrine\DBAL\Connection $connection
130 3
     * @throws \Doctrine\DBAL\DBALException
131 3
     */
132 2
    public static function initDb(\Doctrine\DBAL\Connection $connection)
133 2
    {
134 2
        $schemaManager = $connection->getSchemaManager();
135 2
        $schema = $schemaManager->createSchema();
136 2
        $fromSchema = clone $schema;
137 2
        $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
        $table->addColumn(static::COLUMN_REPORT, 'text', ['notnull' => false]);
141 2
        $table->setPrimaryKey([static::COLUMN_ID]);
142
        $table->addIndex([static::COLUMN_ID], 'IDX_' . static::TABLE_NAME . '_' . static::COLUMN_ID);
143 2
        $queries = $fromSchema->getMigrateToSql($schema, $connection->getDatabasePlatform());
144
        foreach ($queries as $query) {
145 2
            $connection->exec($query);
146 2
        }
147 2
    }
148 2
149 2
    public static function dropDb(\Doctrine\DBAL\Connection $connection)
150 2
    {
151 2
        $schemaManager = $connection->getSchemaManager();
152
        $schema = $schemaManager->createSchema();
153 2
        $fromSchema = clone $schema;
154
        $schema->dropTable(self::TABLE_NAME);
155
        $queries = $fromSchema->getMigrateToSql($schema, $connection->getDatabasePlatform());
156
        foreach ($queries as $query) {
157
            $connection->exec($query);
158
        }
159
    }
160
}