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.

SongLoader   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 149
Duplicated Lines 8.05 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 12
loc 149
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 49 6
A setShowProgress() 0 5 1
A getRowMapperClass() 0 4 1
A setRowMapperClass() 12 12 2
B printProgressMarker() 0 15 5
A getRowMapper() 0 5 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
/**
4
 * Created by PhpStorm.
5
 * User: wechsler
6
 * Date: 21/08/15
7
 * Time: 06:53
8
 */
9
10
namespace Phase\TakeATicket;
11
12
use Doctrine\DBAL\Connection;
13
use Iterator;
14
use Phase\TakeATicket\DataSource\Factory;
15
use Phase\TakeATicket\SongLoader\RclKaraokeRowMapper;
16
use Phase\TakeATicket\SongLoader\RclRockBandRowMapper;
17
use Phase\TakeATicket\SongLoader\RowMapperInterface;
18
19
class SongLoader
20
{
21
    const CODE_LENGTH = 6; // min to avoid clashes
22
23
    protected $startRow = 2;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $showProgress = false;
29
30
    /**
31
     * Class to instantiate as RowMapper
32
     *
33
     * Must implement RowMapperInterface
34
     *
35
     * @var string
36
     */
37
    protected $rowMapperClass = RclRockBandRowMapper::class;
38
39
    /**
40
     * Store contents of specified XLS file to the given database handle
41
     *
42
     * @param  string $sourceFile Path to file
43
     * @param  Connection $dbConn DB connection
44
     * @return int Number of non-duplicate songs stored
45
     * @throws \PHPExcel_Exception
46
     * @throws \PHPExcel_Reader_Exception
47
     * @throws \Doctrine\DBAL\DBALException
48
     */
49
    public function run($sourceFile, Connection $dbConn)
50
    {
51
52
        $objPHPExcel = \PHPExcel_IOFactory::load($sourceFile);
53
        $dataStore = Factory::datasourceFromDbConnection($dbConn);
54
        // empty table - reset autoincrement if it has one
55
        $dataStore->resetCatalogue(); //FIXME clear tickets here too?
56
        $mapper = $this->getRowMapper($dataStore);
57
        $mapper->init();
58
59
        $iterator = $objPHPExcel->getSheet()->getRowIterator($this->startRow);
60
61
        $i = 1;
62
        foreach ($iterator as $sourceRow) {
63
            $flattenedRow = [];
64
            /**
65
             * @var \PHPExcel_Worksheet_Row $sourceRow
66
             */
67
            $cells = $sourceRow->getCellIterator();
68
            /**
69
             * @var Iterator $cells
70
             */
71
            foreach ($cells as $cell) {
72
                /**
73
                 * @var \PHPExcel_Cell $cell
74
                 */
75
                $column = $cell->getColumn();
76
                $content = $cell->getFormattedValue();
77
78
                $flattenedRow[$column] = trim($content);
79
            }
80
81
            if (implode($flattenedRow, '') !== '') {
82
                $storedOk = $mapper->storeRawRow($flattenedRow);
83
                if ($storedOk) {
84
                    $this->printProgressMarker($i);
85
                } else {
86
                    print('x');
87
                }
88
                $i++;
89
            }
90
        }
91
92
        $total = $i - 1;
93
        if ($this->showProgress) {
94
            echo "\nImported $total songs\n";
95
        }
96
        return $total;
97
    }
98
99
    /**
100
     * @param bool $showProgress
101
     * @return SongLoader
102
     */
103
    public function setShowProgress($showProgress)
104
    {
105
        $this->showProgress = $showProgress;
106
        return $this;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getRowMapperClass()
113
    {
114
        return $this->rowMapperClass;
115
    }
116
117
    /**
118
     * Set RowMapper class to use
119
     *
120
     * @param string $rowMapperClass
121
     * @return SongLoader
122
     * @throws \InvalidArgumentException If classname not valid
123
     */
124 View Code Duplication
    public function setRowMapperClass($rowMapperClass)
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...
125
    {
126
        $class = new \ReflectionClass($rowMapperClass);
127
        if ($class->isSubclassOf(RowMapperInterface::class)) {
128
            $this->rowMapperClass = $rowMapperClass;
129
        } else {
130
            throw new \InvalidArgumentException(
131
                "$rowMapperClass must implement " . RowMapperInterface::class
132
            );
133
        }
134
        return $this;
135
    }
136
137
    /**
138
     * @param $i
139
     */
140
    protected function printProgressMarker($i)
141
    {
142
        if ($this->showProgress) {
143
            if (!($i % 100)) {
144
                echo $i;
145
            } else {
146
                if (!($i % 10)) {
147
                    echo '.';
148
                }
149
            }
150
            if (!($i % 1000)) {
151
                echo "\n";
152
            }
153
        }
154
    }
155
156
    /**
157
     * Get the currently configured RowMapper
158
     *
159
     * @param $dataStore
160
     * @return RowMapperInterface
161
     */
162
    protected function getRowMapper($dataStore)
163
    {
164
        $rowMapperClass = $this->getRowMapperClass();
165
        return new $rowMapperClass($dataStore);
166
    }
167
}
168