TSVGoogleShoppingFeedController::convertToCSV()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 9
nop 5
dl 0
loc 24
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Sunnysideup\EcommerceGoogleShoppingFeed\Controllers;
4
5
use SilverStripe\Core\Config\Config;
6
7
/**
8
 * Controller for displaying the xml feed.
9
 *
10
 * <code>
11
 * http://site.com/shoppingfeed.txt
12
 * </code>
13
 *
14
 */
15
class TSVGoogleShoppingFeedController extends GoogleShoppingFeedController
16
{
17
    /**
18
     * @var array
19
     */
20
    private static $allowed_actions = [
21
        'index',
22
    ];
23
24
    public function TSVOutput()
25
    {
26
        $apiClass = Config::inst()->get(GoogleShoppingFeedController::class, 'api_class');
27
        $apiClass = new $apiClass();
28
29
        $data = $apiClass->getTSVData();
30
31
        return $this->convertToCSV($data, "\t");
32
    }
33
34
    protected function getExtension(): string
35
    {
36
        return 'txt';
37
    }
38
39
    protected function getContentType()
40
    {
41
        return 'text/tab-separated-values; charset="utf-8"';
42
    }
43
44
    protected function convertToCSV($rows, $delimiter = ';', $enclosure = '"', $encloseAll = false, $nullToMysqlNull = false)
0 ignored issues
show
Unused Code introduced by
The parameter $nullToMysqlNull is not used and could be removed. ( Ignorable by Annotation )

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

44
    protected function convertToCSV($rows, $delimiter = ';', $enclosure = '"', $encloseAll = false, /** @scrutinizer ignore-unused */ $nullToMysqlNull = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        $delimiter_esc = preg_quote($delimiter, '/');
47
        $enclosure_esc = preg_quote($enclosure, '/');
48
        $string = '';
49
        foreach ($rows as $row) {
50
            if ($string) {
51
                $string .= "\r\n";
52
            }
53
            $output = [];
54
            foreach ($row as $field) {
55
                if (! $field) {
56
                    $output[] = $enclosure . $field . $enclosure;
57
                } elseif ($encloseAll || preg_match("/(?:{$delimiter_esc}|{$enclosure_esc}|\\s)/", $field)) {
58
                    $output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure;
59
                } else {
60
                    $output[] = $field;
61
                }
62
            }
63
            $string .= implode($delimiter, $output);
64
            unset($output);
65
        }
66
67
        return $string;
68
    }
69
}
70