Test Failed
Push — master ( 6c6167...82c1a8 )
by Fabrice
02:45
created

PdoUniqueKeyExtractor   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 3.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 3
loc 98
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __destruct() 0 7 2
B fetchRecords() 3 27 6
A getPaginatedQuery() 0 12 3

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
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Extractors;
11
12
/**
13
 * class PdoUniqueKeyExtractor
14
 */
15
class PdoUniqueKeyExtractor extends UniqueKeyExtractorAbstract
16
{
17
    use PdoExtractorTrait;
18
19
    /**
20
     * generic extraction from tables with unique (composite) key
21
     *
22
     * @param \PDO         $pdo
23
     * @param string       $extractQuery
24
     * @param array|string $uniqueKey    can be either a unique key name as
25
     *                                   string
26
     *                                   '(table.)compositeKeyName' // ('id' by default)
27
     *
28
     *                      or an array :
29
     *                      ['(table.)compositeKey1'] // single unique key
30
     *                      ['(table.)compositeKey1', '(table.)compositeKey2', ] // composite unique key
31
     *
32
     *                      or an associative array in case you are using aliases :
33
     *                      [
34
     *                          '(table.)compositeKey1' => 'aliasNameAsInRecord',
35
     *                      ]
36
     *
37
     *                      and :
38
     *                      [
39
     *                          '(table.)compositeKey1' => 'aliasNameAsInRecord1',
40
     *                          '(table.)compositeKey2' => 'aliasNameAsInRecord2',
41
     *                          // ...
42
     *                      ]
43
     */
44
    public function __construct(\PDO $pdo, $extractQuery = null, $uniqueKey = 'id')
45
    {
46
        $this->configurePdo($pdo);
47
        parent::__construct($extractQuery, $uniqueKey);
48
    }
49
50
    /**
51
     * leave no trace
52
     * implement here to allow easier overidding
53
     */
54
    public function __destruct()
55
    {
56
        if ($this->driverBufferedQuery) {
57
            // set driver state back to where we met
58
            $this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
59
        }
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function fetchRecords()
66
    {
67
        $extractQuery = $this->getPaginatedQuery();
68
69
        $query = $this->pdo->prepare($extractQuery);
70 View Code Duplication
        if (!$query->execute(!empty($this->queryBindings) ? $this->queryBindings : null)) {
71
            return false;
72
        }
73
74
        $this->extracted = isset($this->joinFrom) ? [] : new \SplDoublyLinkedList;
75
        $hasRecord       = false;
76
        while ($record = $query->fetch(\PDO::FETCH_ASSOC)) {
77
            if (isset($this->joinFrom)) {
78
                $this->extracted[$record[$this->uniqueKeyName]] = $record;
79
                $hasRecord                                      = true;
80
                continue;
81
            }
82
83
            $this->extracted->push($record);
84
            $hasRecord = true;
85
        }
86
87
        $query->closeCursor();
88
        unset($query);
89
90
        return $hasRecord;
91
    }
92
93
    /**
94
     * This method sets offset and limit in the query
95
     * WARNING : if you set an offset without limit,
96
     * the limit will be set to  $this->maxdefaultLimit
97
     *
98
     * @return string the paginated query with current offset and limit
99
     */
100
    protected function getPaginatedQuery()
101
    {
102
        if ($this->joinFrom) {
103
            $this->queryBindings = array_values($this->uniqueKeyValues);
104
105
            $whereOrAndStr = stripos($this->extractQuery, 'WHERE') !== false ? 'AND' : 'WHERE';
106
107
            return $this->extractQuery . " $whereOrAndStr $this->uniqueKeyName IN (" . implode(',', array_fill(0, count($this->uniqueKeyValues), '?')) . ')';
108
        }
109
110
        return $this->extractQuery . $this->getLimitOffsetBit();
111
    }
112
}
113