ImportTask   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 19
c 0
b 0
f 0
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A output() 0 4 2
A run() 0 16 3
1
<?php
2
3
namespace Dynamic\Salsify\Task;
4
5
use Dynamic\Salsify\Model\Importer;
6
use Dynamic\Salsify\Traits\Yieldable;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\BuildTask;
11
12
/**
13
 * Class ImportTask
14
 * @package Dynamic\Salsify\Task
15
 * @mixin Yieldable
16
 */
17
class ImportTask extends BuildTask
18
{
19
    use Yieldable;
20
21
    /**
22
     * @var string
23
     */
24
    private static $segment = 'SalsifyImportTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var string
28
     */
29
    protected $title = 'Import products from salsify';
30
31
    /**
32
     * @var string
33
     */
34
    protected $description = 'Imports products from salsify into silverstripe';
35
36
    /**
37
     * @var bool
38
     */
39
    protected $enabled = true;
40
41
    /**
42
     * @var
43
     */
44
    private static $lineEnding;
45
46
    /**
47
     * @var bool
48
     */
49
    private static $output = true;
0 ignored issues
show
introduced by
The private property $output is not used, and could be removed.
Loading history...
50
51
    /**
52
     * @param \SilverStripe\Control\HTTPRequest $request
53
     */
54
    public function run($request)
55
    {
56
        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...
57
58
        // gets all importers
59
        $injectorConfig = array_keys(Config::inst()->get(Injector::class));
60
        $importers = array_filter(
61
            $injectorConfig,
62
            function ($element) {
63
                return strncmp($element, Importer::class, strlen(Importer::class)) === 0;
64
            }
65
        );
66
67
        foreach ($this->yieldSingle($importers) as $importerClass) {
68
            $importer = Injector::inst()->create($importerClass);
69
            $importer->run();
70
        }
71
    }
72
73
    /**
74
     * @param $string
75
     */
76
    public static function output($string)
77
    {
78
        if (static::config()->get('output')) {
79
            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...
80
        }
81
    }
82
}
83