Passed
Push — develop ( 86d31c...191f96 )
by nguereza
03:18
created

AbstractCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file AbstractCommand.php
34
 *
35
 *  The Base scheduler command class
36
 *
37
 *  @package    Platine\Framework\Task\Command
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Task\Command;
49
50
use InvalidArgumentException;
51
use Platine\Config\Config;
52
use Platine\Console\Command\Command;
53
use Platine\Framework\App\Application;
54
use Platine\Framework\Task\Scheduler;
55
use Platine\Framework\Task\TaskInterface;
56
57
/**
58
 * @class AbstractCommand
59
 * @package Platine\Framework\Task\Command
60
 */
61
abstract class AbstractCommand extends Command
62
{
63
    /**
64
     * The scheduler instance
65
     * @var Scheduler
66
     */
67
    protected Scheduler $scheduler;
68
69
    /**
70
     * The Application instance
71
     * @var Application
72
     */
73
    protected Application $application;
74
75
    /**
76
     * The configuration instance
77
     * @var Config<T>
78
     */
79
    protected Config $config;
80
81
    /**
82
     * Create new instance
83
     * @param Scheduler $scheduler
84
     * @param Application $application
85
     * @param Config<T> $config
86
     */
87
    public function __construct(
88
        Scheduler $scheduler,
89
        Application $application,
90
        Config $config
91
    ) {
92
        parent::__construct('scheduler', 'Command to manage scheduler tasks');
93
        $this->scheduler = $scheduler;
94
        $this->application = $application;
95
        $this->config = $config;
96
    }
97
98
    /**
99
     * Add new task
100
     * @param string|TaskInterface $task
101
     * @return void
102
     */
103
    protected function addTask($task): void
104
    {
105
        if (is_string($task)) {
106
            $task = $this->createTask($task);
107
        }
108
109
        $this->scheduler->add($task);
110
    }
111
112
    /**
113
     * Load configured tasks
114
     * @return void
115
     */
116
    protected function registerConfiguredTasks(): void
117
    {
118
        /** @var string[] $tasks */
119
        $tasks = $this->config->get('tasks', []);
120
        foreach ($tasks as $task) {
121
            $this->addTask($task);
122
        }
123
    }
124
125
    /**
126
     * Create new task
127
     * @param string $task
128
     * @return TaskInterface
129
     */
130
    protected function createTask(string $task): TaskInterface
131
    {
132
        if ($this->application->has($task)) {
133
            return $this->application->get($task);
134
        }
135
136
        if (class_exists($task)) {
137
            return new $task();
138
        }
139
140
        throw new InvalidArgumentException(
141
            sprintf('The task [%s] must be an identifier of container or class', $task)
142
        );
143
    }
144
145
    /**
146
     * Load the task list from configuration and services
147
     * providers
148
     * @return void
149
     */
150
    protected function loadTasks(): void
151
    {
152
        $this->registerConfiguredTasks();
153
154
        //Load providers tasks
155
        /** @var class-string[] $tasks */
156
        $tasks = $this->application->getProvidersTasks();
157
        foreach ($tasks as $task) {
158
            $this->addTask($task);
159
        }
160
    }
161
}
162