TemplatesCommand::getArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Templates\Commands;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Contracts\Commands\CommandInterface;
22
use Limoncello\Contracts\Commands\IoInterface;
23
use Limoncello\Contracts\FileSystem\FileSystemInterface;
24
use Limoncello\Contracts\Settings\SettingsProviderInterface;
25
use Limoncello\Templates\Contracts\TemplatesCacheInterface;
26
use Limoncello\Templates\Package\TemplatesSettings;
27
use Psr\Container\ContainerExceptionInterface;
28
use Psr\Container\ContainerInterface;
29
use Psr\Container\NotFoundExceptionInterface;
30
31
/**
32
 * @package Limoncello\Templates
33
 */
34
class TemplatesCommand implements CommandInterface
35
{
36
    /**
37
     * Command name.
38
     */
39
    const NAME = 'l:templates';
40
41
    /** Argument name */
42
    const ARG_ACTION = 'action';
43
44
    /** Command action */
45
    const ACTION_CLEAR_CACHE = 'clear-cache';
46
47
    /** Command action */
48
    const ACTION_CREATE_CACHE = 'cache';
49
50
    /**
51
     * @inheritdoc
52
     */
53 1
    public static function getName(): string
54
    {
55 1
        return static::NAME;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 1
    public static function getDescription(): string
62
    {
63 1
        return 'Creates and cleans templates cache.';
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 1
    public static function getHelp(): string
70
    {
71 1
        return 'This command creates and cleans cache for HTML templates.';
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 1
    public static function getArguments(): array
78
    {
79 1
        $cache = static::ACTION_CREATE_CACHE;
80 1
        $clear = static::ACTION_CLEAR_CACHE;
81
82
        return [
83
            [
84 1
                static::ARGUMENT_NAME        => static::ARG_ACTION,
85 1
                static::ARGUMENT_DESCRIPTION => "Action such as `$cache` or `$clear`.",
86 1
                static::ARGUMENT_MODE        => static::ARGUMENT_MODE__REQUIRED,
87
            ],
88
        ];
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 1
    public static function getOptions(): array
95
    {
96 1
        return [];
97
    }
98
99
    /**
100
     * @inheritdoc
101
     *
102
     * @SuppressWarnings(PHPMD.StaticAccess)
103
     */
104 3
    public static function execute(ContainerInterface $container, IoInterface $inOut): void
105
    {
106 3
        $action = $inOut->getArgument(static::ARG_ACTION);
107
        switch ($action) {
108 3
            case static::ACTION_CREATE_CACHE:
109 1
                (new self())->executeCache($inOut, $container);
110 1
                break;
111 2
            case static::ACTION_CLEAR_CACHE:
112 1
                (new self())->executeClear($inOut, $container);
113 1
                break;
114
            default:
115 1
                $inOut->writeError("Unsupported action `$action`." . PHP_EOL);
116 1
                break;
117
        }
118
    }
119
120
    /**
121
     * @param IoInterface        $inOut
122
     * @param ContainerInterface $container
123
     *
124
     * @return void
125
     *
126
     * @throws ContainerExceptionInterface
127
     * @throws NotFoundExceptionInterface
128
     */
129 1
    protected function executeClear(IoInterface $inOut, ContainerInterface $container): void
130
    {
131 1
        $settings    = $this->getTemplatesSettings($container);
132 1
        $cacheFolder = $settings[TemplatesSettings::KEY_CACHE_FOLDER];
133
134
        /** @var FileSystemInterface $fileSystem */
135 1
        $fileSystem = $container->get(FileSystemInterface::class);
136 1
        foreach ($fileSystem->scanFolder($cacheFolder) as $fileOrFolder) {
137 1
            $fileSystem->isFolder($fileOrFolder) === false ?: $fileSystem->deleteFolderRecursive($fileOrFolder);
138
        }
139
140 1
        $inOut->writeInfo('Cache has been cleared.' . PHP_EOL);
141
    }
142
143
    /**
144
     * @param IoInterface        $inOut
145
     * @param ContainerInterface $container
146
     *
147
     * @return void
148
     *
149
     * @throws ContainerExceptionInterface
150
     * @throws NotFoundExceptionInterface
151
     *
152
     * @SuppressWarnings(PHPMD.StaticAccess)
153
     */
154 1
    protected function executeCache(IoInterface $inOut, ContainerInterface $container): void
155
    {
156
        /** @var TemplatesCacheInterface $cache */
157 1
        $cache = $container->get(TemplatesCacheInterface::class);
158
159 1
        $settings  = $this->getTemplatesSettings($container);
160 1
        $templates = $settings[TemplatesSettings::KEY_TEMPLATES_LIST];
161
162 1
        foreach ($templates as $templateName) {
163
            // it will write template to cache
164 1
            $inOut->writeInfo(
165 1
                "Starting template caching for `$templateName`..." . PHP_EOL,
166 1
                IoInterface::VERBOSITY_VERBOSE
167
            );
168
169 1
            $cache->cache($templateName);
170
171 1
            $inOut->writeInfo(
172 1
                "Template caching finished for `$templateName`." . PHP_EOL,
173 1
                IoInterface::VERBOSITY_NORMAL
174
            );
175
        }
176
    }
177
178
    /**
179
     * @param ContainerInterface $container
180
     *
181
     * @return array
182
     *
183
     * @throws ContainerExceptionInterface
184
     * @throws NotFoundExceptionInterface
185
     */
186 2
    protected function getTemplatesSettings(ContainerInterface $container): array
187
    {
188 2
        $tplConfig = $container->get(SettingsProviderInterface::class)->get(TemplatesSettings::class);
189
190 2
        return $tplConfig;
191
    }
192
}
193