Completed
Push — develop ( b5d1ea...bf89f2 )
by Neomerx
03:07
created

TemplatesSettings   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 98
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 35 4
A getSettings() 0 7 1
A getTemplateNames() 0 18 4
1
<?php namespace Limoncello\Templates\Package;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\Settings\SettingsInterface;
20
use RecursiveDirectoryIterator;
21
use RecursiveIteratorIterator;
22
use SplFileInfo;
23
24
/**
25
 * @package Limoncello\Templates
26
 */
27
class TemplatesSettings implements SettingsInterface
28
{
29
    /** Settings key */
30
    const KEY_TEMPLATES_FOLDER = 0;
31
32
    /** Settings key */
33
    const KEY_TEMPLATES_FILE_MASK = self::KEY_TEMPLATES_FOLDER + 1;
34
35
    /** Settings key */
36
    const KEY_CACHE_FOLDER = self::KEY_TEMPLATES_FILE_MASK + 1;
37
38
    /** Settings key */
39
    const KEY_APP_ROOT_FOLDER = self::KEY_CACHE_FOLDER + 1;
40
41
    /** Settings key */
42
    const KEY_IS_DEBUG = self::KEY_APP_ROOT_FOLDER + 1;
43
44
    /** Settings key */
45
    const KEY_TEMPLATES_LIST = self::KEY_IS_DEBUG + 1;
46
47
    /** Settings key */
48
    const KEY_LAST = self::KEY_TEMPLATES_LIST;
49
50
    /**
51
     * @inheritdoc
52
     */
53 4
    final public function get(): array
54
    {
55 4
        $defaults = $this->getSettings();
56
57 4
        $templatesFolder   = $defaults[static::KEY_TEMPLATES_FOLDER] ?? null;
58 4
        $templatesFileMask = $defaults[static::KEY_TEMPLATES_FILE_MASK] ?? null;
59 4
        $cacheFolder       = $defaults[static::KEY_CACHE_FOLDER] ?? null;
60 4
        $appRootFolder     = $defaults[static::KEY_APP_ROOT_FOLDER] ?? null;
61
62 4
        assert(
63 4
            $templatesFolder !== null && empty(glob($templatesFolder)) === false,
64 4
            "Invalid Templates folder `$templatesFolder`."
65
        );
66 4
        assert(empty($templatesFileMask) === false, "Invalid Templates file mask `$templatesFileMask`.");
67 4
        assert(
68 4
            $cacheFolder !== null && empty(glob($cacheFolder)) === false,
69 4
            "Invalid Cache folder `$cacheFolder`."
70
        );
71 4
        assert(
72 4
            $appRootFolder !== null && empty(glob($appRootFolder)) === false,
73 4
            "Invalid App root folder `$appRootFolder`."
74
        );
75
76 4
        $realTemplatesFolder = realpath($templatesFolder);
77 4
        $realCacheFolder     = realpath($cacheFolder);
78 4
        $realAppRootFolder   = realpath($appRootFolder);
79
80 4
        $defaults[static::KEY_TEMPLATES_FOLDER] = $realTemplatesFolder;
81 4
        $defaults[static::KEY_CACHE_FOLDER]     = $realCacheFolder;
82 4
        $defaults[static::KEY_APP_ROOT_FOLDER]  = $realAppRootFolder;
83
84
        return $defaults + [
85 4
                static::KEY_TEMPLATES_LIST => $this->getTemplateNames($realTemplatesFolder, $templatesFileMask),
86
            ];
87
    }
88
89
    /**
90
     * @return array
91
     */
92 4
    protected function getSettings(): array
93
    {
94
        return [
95 4
            static::KEY_IS_DEBUG            => false,
96 4
            static::KEY_TEMPLATES_FILE_MASK => '*.twig',
97
        ];
98
    }
99
100
    /**
101
     * @param string $templatesFolder
102
     * @param string $templatesFileMask
103
     *
104
     * @return array
105
     */
106
    private function getTemplateNames(string $templatesFolder, string $templatesFileMask): array
107
    {
108 4
        return iterator_to_array(call_user_func(function () use ($templatesFolder, $templatesFileMask) {
109
            $flags    =
110
                RecursiveDirectoryIterator::SKIP_DOTS |
111
                RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
112 4
                RecursiveDirectoryIterator::CURRENT_AS_FILEINFO;
113 4
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($templatesFolder, $flags));
114 4
            foreach ($iterator as $found) {
115
                /** @var SplFileInfo $found */
116 4
                if ($found->isFile() === true && fnmatch($templatesFileMask, $found->getFilename()) === true) {
117 4
                    $fullFileName = $found->getPath() . DIRECTORY_SEPARATOR . $found->getFilename();
118 4
                    $templateName = str_replace($templatesFolder . DIRECTORY_SEPARATOR, '', $fullFileName);
119 4
                    yield $templateName;
120
                }
121
            }
122 4
        }));
123
    }
124
}
125