Completed
Push — master ( 489545...8e653b )
by Basil
04:40
created

Importer::run()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
3
namespace luya\console;
4
5
use luya\console\interfaces\ImportControllerInterface;
6
use yii\base\BaseObject;
7
use luya\base\Module;
8
9
/**
10
 * Base class for all Importer classes.
11
 *
12
 * Abstract importer class provides basic funcionality to access the
13
 * helper class via `getImporter()`. Each importer class must have run()
14
 * method where the basic logic of the class will be executed.
15
 *
16
 * ```php
17
 * class XyzImporter extends \luya\console\Importer
18
 * {
19
 *     public function run()
20
 *     {
21
 *         $this->addLog('XyzImporter have been started');
22
 *
23
 *         // importer logic goes here
24
 *     }
25
 * }
26
 * ```
27
 *
28
 * @property \luya\console\interfaces\ImportControllerInterface $importer Importer object.
29
 * @property \luya\base\Module $module The module context object.
30
 *
31
 * @author Basil Suter <[email protected]>
32
 * @since 1.0.0
33
 */
34
abstract class Importer extends BaseObject
35
{
36
    const QUEUE_POSITION_FIRST = 0;
37
38
    const QUEUE_POSITION_MIDDLE = 50;
39
40
    const QUEUE_POSITION_LAST = 100;
41
42
    /**
43
     * @var int The priority between 0 and 100 where to Import command should be queued.
44
     * + 0 = First
45
     * + 100 = Last
46
     */
47
    public $queueListPosition = self::QUEUE_POSITION_MIDDLE;
48
49
    /**
50
     * @var \luya\console\interfaces\ImportControllerInterface Read only property contains the importer object.
51
     */
52
    private $_importer;
53
54
    /**
55
     * @var \luya\base\Module Read only module object property.
56
     */
57
    private $_module;
58
    
59
    /**
60
     * Class constructor containing the importer object from where its called.
61
     *
62
     * @param \luya\console\interfaces\ImportControllerInterface $importer Import Object `\luya\commands\ImportController`.
63
     */
64
    public function __construct(ImportControllerInterface $importer, Module $module, $config = [])
65
    {
66
        $this->_importer = $importer;
67
        $this->_module = $module;
68
        
69
        parent::__construct($config);
70
    }
71
72
    /**
73
     * Returns the import object to use the importers methods.
74
     *
75
     * @return \luya\console\interfaces\ImportControllerInterface The importer object.
76
     */
77
    public function getImporter()
78
    {
79
        return $this->_importer;
80
    }
81
    
82
    /**
83
     * Returns the module object where the command has been found.
84
     * 
85
     * @return \luya\base\Module
86
     * @since 1.0.8
87
     */
88
    public function getModule()
89
    {
90
        return $this->_module;
91
    }
92
93
    /**
94
     * Add something to the output. Wrapper method from importer.
95
     *
96
     * ```php
97
     * $this->addLog('new block <ID> have been found and added to database');
98
     * ```
99
     *
100
     * @param string $value The value to be written for the log output.
101
     */
102
    public function addLog($value)
103
    {
104
        $this->getImporter()->addLog(get_called_class(), $value);
105
    }
106
107
    /**
108
     * Each Importer Class must contain a run method.
109
     */
110
    abstract public function run();
111
}
112