TemplatesSettings   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 99
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 39 4
A getSettings() 0 12 1
A getAppConfig() 0 4 1
A getTemplateNames() 0 18 4
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Templates\Package;
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\Application\ApplicationConfigurationInterface as A;
22
use Limoncello\Contracts\Settings\Packages\TemplatesSettingsInterface;
23
use RecursiveDirectoryIterator;
24
use RecursiveIteratorIterator;
25
use SplFileInfo;
26
use function assert;
27
use function call_user_func;
28
use function fnmatch;
29
use function glob;
30
use function is_string;
31
use function iterator_to_array;
32
use function realpath;
33
use function str_replace;
34
35
/**
36
 * @package Limoncello\Templates
37
 */
38
class TemplatesSettings implements TemplatesSettingsInterface
39
{
40
    /**
41
     * @var array
42
     */
43
    private $appConfig;
44
45
    /**
46
     * @inheritdoc
47
     */
48 4
    final public function get(array $appConfig): array
49
    {
50 4
        $this->appConfig = $appConfig;
51
52 4
        $defaults = $this->getSettings();
53
54 4
        $templatesFolder   = $defaults[static::KEY_TEMPLATES_FOLDER] ?? null;
55 4
        $templatesFileMask = $defaults[static::KEY_TEMPLATES_FILE_MASK] ?? null;
56 4
        $cacheFolder       = $defaults[static::KEY_CACHE_FOLDER] ?? null;
57 4
        $appRootFolder     = $defaults[static::KEY_APP_ROOT_FOLDER] ?? null;
58
59 4
        assert(
60 4
            $templatesFolder !== null && empty(glob($templatesFolder)) === false,
61 4
            "Invalid Templates folder `$templatesFolder`."
62
        );
63 4
        assert(empty($templatesFileMask) === false, "Invalid Templates file mask `$templatesFileMask`.");
64 4
        assert(
65 4
            $cacheFolder !== null && empty(glob($cacheFolder)) === false,
66 4
            "Invalid Cache folder `$cacheFolder`."
67
        );
68 4
        assert(
69 4
            $appRootFolder !== null && empty(glob($appRootFolder)) === false,
70 4
            "Invalid App root folder `$appRootFolder`."
71
        );
72
73 4
        $realTemplatesFolder = realpath($templatesFolder);
74 4
        $realCacheFolder     = realpath($cacheFolder);
75 4
        $realAppRootFolder   = realpath($appRootFolder);
76
77 4
        $defaults[static::KEY_TEMPLATES_FOLDER] = $realTemplatesFolder;
78 4
        $defaults[static::KEY_CACHE_FOLDER]     = $realCacheFolder;
79 4
        $defaults[static::KEY_APP_ROOT_FOLDER]  = $realAppRootFolder;
80
81 4
        assert(is_string($templatesFileMask));
82
83
        return $defaults + [
84 4
                static::KEY_TEMPLATES_LIST => $this->getTemplateNames($realTemplatesFolder, $templatesFileMask),
85
            ];
86
    }
87
88
    /**
89
     * @return array
90
     */
91 4
    protected function getSettings(): array
92
    {
93 4
        $appConfig = $this->getAppConfig();
94
95 4
        $isDebug = (bool)($appConfig[A::KEY_IS_DEBUG] ?? false);
96
97
        return [
98 4
            static::KEY_IS_DEBUG            => $isDebug,
99 4
            static::KEY_IS_AUTO_RELOAD      => $isDebug,
100 4
            static::KEY_TEMPLATES_FILE_MASK => '*.twig',
101
        ];
102
    }
103
104
    /**
105
     * @return mixed
106
     */
107 4
    protected function getAppConfig()
108
    {
109 4
        return $this->appConfig;
110
    }
111
112
    /**
113
     * @param string $templatesFolder
114
     * @param string $templatesFileMask
115
     *
116
     * @return array
117
     */
118 4
    private function getTemplateNames(string $templatesFolder, string $templatesFileMask): array
119
    {
120
        return iterator_to_array(call_user_func(function () use ($templatesFolder, $templatesFileMask) {
121
            $flags    =
122
                RecursiveDirectoryIterator::SKIP_DOTS |
123
                RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
124 4
                RecursiveDirectoryIterator::CURRENT_AS_FILEINFO;
125 4
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($templatesFolder, $flags));
126 4
            foreach ($iterator as $found) {
127
                /** @var SplFileInfo $found */
128 4
                if ($found->isFile() === true && fnmatch($templatesFileMask, $found->getFilename()) === true) {
129 4
                    $fullFileName = $found->getPath() . DIRECTORY_SEPARATOR . $found->getFilename();
130 4
                    $templateName = str_replace($templatesFolder . DIRECTORY_SEPARATOR, '', $fullFileName);
131 4
                    yield $templateName;
132
                }
133
            }
134 4
        }));
135
    }
136
}
137