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.

TransactionCsvStorage::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Awin\ReportTask\Bundle\ReportBundle\Model;
4
5
use Awin\ReportTask\Bundle\ReportBundle\Exception\ReportFileSaveFailureException;
6
use League\Csv\Reader;
7
use League\Csv\Writer;
8
use Symfony\Component\Config\Definition\Exception\Exception;
9
10
/**
11
 * The class to read data from CSV file, it is implemented from TransactionStorageInterface
12
 * and other implementation could introduced, for example, database, elasticsearch, API, etc
13
 *
14
 * @date       24/06/2017
15
 * @time       15:20
16
 * @author     Peng Yue <[email protected]>
17
 * @copyright  2004-2017 Peng Yue
18
 */
19
20
class TransactionCsvStorage implements TransactionStorageInterface
21
{
22
    /**
23
     * The source csv file path
24
     *
25
     * @var string
26
     */
27
    private $csvFilePath;
28
29
    /**
30
     * The data to be save into storage
31
     *
32
     * @var array
33
     */
34
    private $data = [];
35
36
    /**
37
     * TransactionCsvStorage constructor.
38
     *
39
     * @param string $csvFilePath
40
     */
41 8
    public function __construct($csvFilePath)
42
    {
43 8
        $this->csvFilePath  = $csvFilePath;
44
45 8
        if (!ini_get("auto_detect_line_endings")) {
46 2
            ini_set("auto_detect_line_endings", '1');
47
        }
48 8
    }
49
50
    /**
51
     * Get the all the data from source csv
52
     * There is no pagination for simplicity
53
     *
54
     * @param int $offset
55
     * @param int $limit
56
     *
57
     * @return array
58
     */
59 6
    public function getData($offset, $limit)
60
    {
61 6
        $csv = Reader::createFromPath($this->csvFilePath);
62 6
        $csv->setDelimiter(';');
63 6
        $data = $csv->setOffset($offset)->setLimit($limit)->fetchAll();
64
65 6
        return $data;
66
    }
67
68
    /**
69
     * Set the data for saving into storage
70
     *
71
     * @param array $data
72
     *
73
     * @return $this
74
     */
75 3
    public function setData(array $data)
76
    {
77 3
        $this->data = $data;
78
79 3
        return $this;
80
    }
81
82
    /**
83
     * Save the data into storage
84
     *
85
     * @param string $targetCsvFile
86
     *
87
     * @return bool
88
     */
89 2
    public function save($targetCsvFile)
90
    {
91
        try {
92 2
            if (file_exists($targetCsvFile)) {
93 2
                @unlink($targetCsvFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

93
                /** @scrutinizer ignore-unhandled */ @unlink($targetCsvFile);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
94
            }
95
96 2
            $csv = Writer::createFromPath($targetCsvFile, 'a+');
97
98 2
            $csv->insertOne([
99 2
                'Merchant ID',
100
                'Date',
101
                'Original Transaction',
102
                'Currency Symbol',
103
                'Transaction Amount',
104
                'Transaction In GBP',
105
                'Transaction In EUR',
106
                'Transaction In USD',
107
            ]);
108
109 1
            $csv->insertAll($this->data);
110 1
        } catch (\Exception $e) {
111 1
            throw new ReportFileSaveFailureException($targetCsvFile);
112
        }
113
114 1
        return true;
115
    }
116
}