Passed
Pull Request — master (#18)
by Matthew
02:00
created

ImportTask::output()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Dynamic\Salsify\Task;
4
5
use Dynamic\Salsify\Model\Importer;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\BuildTask;
10
11
/**
12
 * Class ImportTask
13
 * @package Dynamic\Salsify\Task
14
 */
15
class ImportTask extends BuildTask
16
{
17
    /**
18
     * @var string
19
     */
20
    private static $segment = 'SalsifyImportTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
21
22
    /**
23
     * @var string
24
     */
25
    protected $title = 'Import products from salsify';
26
27
    /**
28
     * @var string
29
     */
30
    protected $description = 'Imports products from salsify into silverstripe';
31
32
    /**
33
     * @var bool
34
     */
35
    protected $enabled = true;
36
37
    /**
38
     * @var
39
     */
40
    private static $lineEnding;
41
42
    /**
43
     * @var bool
44
     */
45
    private static $output = true;
0 ignored issues
show
introduced by
The private property $output is not used, and could be removed.
Loading history...
46
47
    /**
48
     * @param \SilverStripe\Control\HTTPRequest $request
49
     */
50
    public function run($request)
51
    {
52
        static::$lineEnding = Director::is_cli() ? PHP_EOL : '<br />';
0 ignored issues
show
Bug introduced by
Since $lineEnding is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $lineEnding to at least protected.
Loading history...
53
54
        // gets all importers
55
        $injectorConfig = array_keys(Config::inst()->get(Injector::class));
56
        $importers = array_filter(
57
            $injectorConfig,
58
            function ($element) {
59
                return strncmp($element, Importer::class, strlen(Importer::class)) === 0;
60
            }
61
        );
62
63
        foreach ($importers as $importerClass) {
64
            $importer = Injector::inst()->create($importerClass);
65
            $importer->run();
66
        }
67
    }
68
69
    /**
70
     * @param $string
71
     */
72
    public static function output($string)
73
    {
74
        if (static::config()->get('output')) {
75
            echo $string . static::$lineEnding;
0 ignored issues
show
Bug introduced by
Since $lineEnding is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $lineEnding to at least protected.
Loading history...
76
        }
77
    }
78
}
79